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.ex.camera2.portability.debug;
18 
19 public class Log {
20     /**
21      * All Camera logging using this class will use this tag prefix.
22      * Additionally, the prefix itself is checked in isLoggable and
23      * serves as an override. So, to toggle all logs allowed by the
24      * current {@link Configuration}, you can set properties:
25      *
26      * adb shell setprop log.tag.CAM2PORT_ VERBOSE
27      * adb shell setprop log.tag.CAM2PORT_ ""
28      */
29     public static final String CAMERA_LOGTAG_PREFIX = "CAM2PORT_";
30     private static final Log.Tag TAG = new Log.Tag("Log");
31 
32     /**
33      * This class restricts the length of the log tag to be less than the
34      * framework limit and also prepends the common tag prefix defined by
35      * {@code CAMERA_LOGTAG_PREFIX}.
36      */
37     public static final class Tag {
38 
39         // The length limit from Android framework is 23.
40         private static final int MAX_TAG_LEN = 23 - CAMERA_LOGTAG_PREFIX.length();
41 
42         final String mValue;
43 
Tag(String tag)44         public Tag(String tag) {
45             final int lenDiff = tag.length() - MAX_TAG_LEN;
46             if (lenDiff > 0) {
47                 w(TAG, "Tag " + tag + " is " + lenDiff + " chars longer than limit.");
48             }
49             mValue = CAMERA_LOGTAG_PREFIX + (lenDiff > 0 ? tag.substring(0, MAX_TAG_LEN) : tag);
50         }
51 
52         @Override
toString()53         public String toString() {
54             return mValue;
55         }
56     }
57 
d(Tag tag, String msg)58     public static void d(Tag tag, String msg) {
59         if (isLoggable(tag, android.util.Log.DEBUG)) {
60             android.util.Log.d(tag.toString(), msg);
61         }
62     }
63 
d(Tag tag, String msg, Throwable tr)64     public static void d(Tag tag, String msg, Throwable tr) {
65         if (isLoggable(tag, android.util.Log.DEBUG)) {
66             android.util.Log.d(tag.toString(), msg, tr);
67         }
68     }
69 
e(Tag tag, String msg)70     public static void e(Tag tag, String msg) {
71         if (isLoggable(tag, android.util.Log.ERROR)) {
72             android.util.Log.e(tag.toString(), msg);
73         }
74     }
75 
e(Tag tag, String msg, Throwable tr)76     public static void e(Tag tag, String msg, Throwable tr) {
77         if (isLoggable(tag, android.util.Log.ERROR)) {
78             android.util.Log.e(tag.toString(), msg, tr);
79         }
80     }
81 
i(Tag tag, String msg)82     public static void i(Tag tag, String msg) {
83         if (isLoggable(tag, android.util.Log.INFO)) {
84             android.util.Log.i(tag.toString(), msg);
85         }
86     }
87 
i(Tag tag, String msg, Throwable tr)88     public static void i(Tag tag, String msg, Throwable tr) {
89         if (isLoggable(tag, android.util.Log.INFO)) {
90             android.util.Log.i(tag.toString(), msg, tr);
91         }
92     }
93 
v(Tag tag, String msg)94     public static void v(Tag tag, String msg) {
95         if (isLoggable(tag, android.util.Log.VERBOSE)) {
96             android.util.Log.v(tag.toString(), msg);
97         }
98     }
99 
v(Tag tag, String msg, Throwable tr)100     public static void v(Tag tag, String msg, Throwable tr) {
101         if (isLoggable(tag, android.util.Log.VERBOSE)) {
102             android.util.Log.v(tag.toString(), msg, tr);
103         }
104     }
105 
w(Tag tag, String msg)106     public static void w(Tag tag, String msg) {
107         if (isLoggable(tag, android.util.Log.WARN)) {
108             android.util.Log.w(tag.toString(), msg);
109         }
110     }
111 
w(Tag tag, String msg, Throwable tr)112     public static void w(Tag tag, String msg, Throwable tr) {
113         if (isLoggable(tag, android.util.Log.WARN)) {
114             android.util.Log.w(tag.toString(), msg, tr);
115         }
116     }
117 
isLoggable(Tag tag, int level)118     private static boolean isLoggable(Tag tag, int level) {
119         try {
120             if (LogHelper.getOverrideLevel() != 0) {
121                 // Override system log level and output. VERBOSE is smaller than
122                 // ERROR, so the comparison checks that the override value is smaller
123                 // than the desired output level. This applies to all tags.
124                 return LogHelper.getOverrideLevel() <= level;
125             } else {
126                 // The prefix can be used as an override tag to see all camera logs
127                 return android.util.Log.isLoggable(CAMERA_LOGTAG_PREFIX, level)
128                         || android.util.Log.isLoggable(tag.toString(), level);
129             }
130         } catch (IllegalArgumentException ex) {
131             e(TAG, "Tag too long:" + tag);
132             return false;
133         }
134     }
135 }
136