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