• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.systemui.classifier;
18 
19 import android.app.ActivityThread;
20 import android.app.Application;
21 import android.os.Build;
22 import android.os.SystemProperties;
23 import android.util.Log;
24 
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.PrintWriter;
30 import java.text.SimpleDateFormat;
31 import java.util.ArrayDeque;
32 import java.util.Date;
33 import java.util.Locale;
34 
35 /**
36  * Keeps track of interesting falsing data.
37  *
38  * By default the log only gets collected on userdebug builds. To turn it on on user:
39  *  adb shell setprop debug.falsing_log true
40  *
41  * The log gets dumped as part of the SystemUI services. To dump on demand:
42  *  adb shell dumpsys activity service com.android.systemui SystemBars | grep -A 999 FALSING | less
43  *
44  * To dump into logcat:
45  *  adb shell setprop debug.falsing_logcat true
46  *
47  * To adjust the log buffer size:
48  *  adb shell setprop debug.falsing_log_size 200
49  */
50 public class FalsingLog {
51     public static final boolean ENABLED = SystemProperties.getBoolean("debug.falsing_log",
52             Build.IS_DEBUGGABLE);
53     private static final boolean LOGCAT = SystemProperties.getBoolean("debug.falsing_logcat",
54             false);
55 
56     public static final boolean VERBOSE = false;
57 
58     private static final int MAX_SIZE = SystemProperties.getInt("debug.falsing_log_size", 100);
59 
60     private static final String TAG = "FalsingLog";
61 
62     private final ArrayDeque<String> mLog = new ArrayDeque<>(MAX_SIZE);
63     private final SimpleDateFormat mFormat = new SimpleDateFormat("MM-dd HH:mm:ss", Locale.US);
64 
65     private static FalsingLog sInstance;
66 
FalsingLog()67     private FalsingLog() {
68     }
69 
v(String tag, String s)70     public static void v(String tag, String s) {
71         if (!VERBOSE) {
72             return;
73         }
74         if (LOGCAT) {
75             Log.v(TAG, tag + "\t" + s);
76         }
77         log("V", tag, s);
78     }
79 
i(String tag, String s)80     public static void i(String tag, String s) {
81         if (LOGCAT) {
82             Log.i(TAG, tag + "\t" + s);
83         }
84         log("I", tag, s);
85     }
86 
wLogcat(String tag, String s)87     public static void wLogcat(String tag, String s) {
88         Log.w(TAG, tag + "\t" + s);
89         log("W", tag, s);
90     }
91 
w(String tag, String s)92     public static void w(String tag, String s) {
93         if (LOGCAT) {
94             Log.w(TAG, tag + "\t" + s);
95         }
96         log("W", tag, s);
97     }
98 
e(String tag, String s)99     public static void e(String tag, String s) {
100         if (LOGCAT) {
101             Log.e(TAG, tag + "\t" + s);
102         }
103         log("E", tag, s);
104     }
105 
log(String level, String tag, String s)106     public static synchronized void log(String level, String tag, String s) {
107         if (!ENABLED) {
108             return;
109         }
110         if (sInstance == null) {
111             sInstance = new FalsingLog();
112         }
113 
114         if (sInstance.mLog.size() >= MAX_SIZE) {
115             sInstance.mLog.removeFirst();
116         }
117         String entry = new StringBuilder().append(sInstance.mFormat.format(new Date()))
118             .append(" ").append(level).append(" ")
119             .append(tag).append(" ").append(s).toString();
120         sInstance.mLog.add(entry);
121     }
122 
dump(PrintWriter pw)123     public static synchronized void dump(PrintWriter pw) {
124         pw.println("FALSING LOG:");
125         if (!ENABLED) {
126             pw.println("Disabled, to enable: setprop debug.falsing_log 1");
127             pw.println();
128             return;
129         }
130         if (sInstance == null || sInstance.mLog.isEmpty()) {
131             pw.println("<empty>");
132             pw.println();
133             return;
134         }
135         for (String s : sInstance.mLog) {
136             pw.println(s);
137         }
138         pw.println();
139     }
140 
wtf(String tag, String s, Throwable here)141     public static synchronized void wtf(String tag, String s, Throwable here) {
142         if (!ENABLED) {
143             return;
144         }
145         e(tag, s);
146 
147         Application application = ActivityThread.currentApplication();
148         String fileMessage = "";
149         if (Build.IS_DEBUGGABLE && application != null) {
150             File f = new File(application.getDataDir(), "falsing-"
151                     + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt");
152             PrintWriter pw = null;
153             try {
154                 pw = new PrintWriter(f);
155                 dump(pw);
156                 pw.close();
157                 fileMessage = "Log written to " + f.getAbsolutePath();
158             } catch (IOException e) {
159                 Log.e(TAG, "Unable to write falsing log", e);
160             } finally {
161                 if (pw != null) {
162                     pw.close();
163                 }
164             }
165         } else {
166             Log.e(TAG, "Unable to write log, build must be debuggable.");
167         }
168 
169         Log.wtf(TAG, tag + " " + s + "; " + fileMessage, here);
170     }
171 }
172