1 /*
2  * Copyright (C) 2016, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.aidl.tests;
18 
19 import android.util.Log;
20 import java.io.PrintWriter;
21 import java.io.IOException;
22 import android.content.Context;
23 
24 public class TestLogger {
25     private static final String TAG = "TestServiceClient";
26     private PrintWriter mLogFile;
27 
TestLogger(Context context)28     public TestLogger(Context context) {
29         try {
30             mLogFile = new PrintWriter(context.openFileOutput(
31                     "test-client.log", Context.MODE_PRIVATE));
32         } catch (IOException ex) {
33             throw new RuntimeException("Failed to open log file for writing.");
34         }
35     }
36 
log(String line)37     public void log(String line) {
38         Log.i(TAG, line);
39         mLogFile.println(line);
40     }
41 
logAndThrow(String line)42     public void logAndThrow(String line) throws TestFailException {
43         Log.e(TAG, line);
44         mLogFile.println(line);
45         throw new TestFailException(line);
46     }
47 
close()48     public void close() {
49         if (mLogFile != null) {
50             mLogFile.close();
51         }
52     }
53 }
54