1 /*
2  * Copyright (C) 2014 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 com.android.testingcamera2;
18 
19 import java.util.Locale;
20 
21 import android.util.Log;
22 
23 public class TLog {
24 
25     private static Logger mLogger;
26     private static final String TAG = "TestingCamera2";
27 
setLogger(Logger logger)28     synchronized static public void setLogger(Logger logger) {
29         mLogger = logger;
30     }
31 
32     /**
33      * Log an informative message to the current log destination and to the system log.
34      * Supports formatting in the style of String.format()
35      *
36      * @param text The text to print out, with optional formatting specifiers
37      * @param args Arguments to fill in to the string
38      */
i(String text, Object... args)39     synchronized static public void i(String text, Object... args) {
40         if (args.length > 0) {
41             text = String.format(Locale.US, text, args);
42         }
43         android.util.Log.i(TAG, text);
44         if (mLogger != null) {
45             mLogger.addToLog(text, false);
46         }
47     }
48 
49     /**
50      * Log an error message to the current log destination and to the system log.
51      * Supports formatting in the style of String.format()
52      *
53      * @param text The text to print out, with optional formatting specifiers
54      * @param args Arguments to fill in to the string
55      */
e(String text, Object... args)56     synchronized static public void e(String text, Object... args) {
57         if (args.length > 0) {
58             text = String.format(Locale.US, text, args);
59         }
60         android.util.Log.e(TAG, text);
61         if (mLogger != null) {
62             mLogger.addToLog(text, true);
63         }
64     }
65 
66     /**
67      * Log an error message to the current log destination and to the system log,
68      * including the exception stack trace.
69      *
70      * <p>The trace will be added after the main error message.  Supports formatting in
71      * the style of String.format()</p>
72      *
73      * @param text The text to print out, with optional formatting specifiers
74      * @param e The throwable for the error
75      * @param args Arguments to fill in to the string
76      */
e(String text, Throwable e, Object... args)77     synchronized static public void e(String text, Throwable e, Object... args) {
78         text = String.format("%s\n%s", text, Log.getStackTraceString(e));
79         if (args.length > 0) {
80             text = String.format(Locale.US, text, args);
81         }
82         android.util.Log.e(TAG, text);
83         if (mLogger != null) {
84             mLogger.addToLog(text, true);
85         }
86     }
87 
88     public interface Logger {
addToLog(String text, boolean error)89         public void addToLog(String text, boolean error);
90     }
91 }