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
17package com.android.systemui.classifier;
18
19import android.app.ActivityThread;
20import android.app.Application;
21import android.os.Build;
22import android.os.SystemProperties;
23import android.util.Log;
24
25import java.io.File;
26import java.io.FileNotFoundException;
27import java.io.FileOutputStream;
28import java.io.IOException;
29import java.io.PrintWriter;
30import java.text.SimpleDateFormat;
31import java.util.ArrayDeque;
32import java.util.Date;
33import 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 */
50public 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
67    private FalsingLog() {
68    }
69
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
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
87    public static void w(String tag, String s) {
88        if (LOGCAT) {
89            Log.w(TAG, tag + "\t" + s);
90        }
91        log("W", tag, s);
92    }
93
94    public static void e(String tag, String s) {
95        if (LOGCAT) {
96            Log.e(TAG, tag + "\t" + s);
97        }
98        log("E", tag, s);
99    }
100
101    public static synchronized void log(String level, String tag, String s) {
102        if (!ENABLED) {
103            return;
104        }
105        if (sInstance == null) {
106            sInstance = new FalsingLog();
107        }
108
109        if (sInstance.mLog.size() >= MAX_SIZE) {
110            sInstance.mLog.removeFirst();
111        }
112        String entry = new StringBuilder().append(sInstance.mFormat.format(new Date()))
113            .append(" ").append(level).append(" ")
114            .append(tag).append(" ").append(s).toString();
115        sInstance.mLog.add(entry);
116    }
117
118    public static synchronized void dump(PrintWriter pw) {
119        pw.println("FALSING LOG:");
120        if (!ENABLED) {
121            pw.println("Disabled, to enable: setprop debug.falsing_log 1");
122            pw.println();
123            return;
124        }
125        if (sInstance == null || sInstance.mLog.isEmpty()) {
126            pw.println("<empty>");
127            pw.println();
128            return;
129        }
130        for (String s : sInstance.mLog) {
131            pw.println(s);
132        }
133        pw.println();
134    }
135
136    public static synchronized void wtf(String tag, String s) {
137        if (!ENABLED) {
138            return;
139        }
140        e(tag, s);
141
142        Application application = ActivityThread.currentApplication();
143        String fileMessage = "";
144        if (Build.IS_DEBUGGABLE && application != null) {
145            File f = new File(application.getDataDir(), "falsing-"
146                    + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date()) + ".txt");
147            PrintWriter pw = null;
148            try {
149                pw = new PrintWriter(f);
150                dump(pw);
151                pw.close();
152                fileMessage = "Log written to " + f.getAbsolutePath();
153            } catch (IOException e) {
154                Log.e(TAG, "Unable to write falsing log", e);
155            } finally {
156                if (pw != null) {
157                    pw.close();
158                }
159            }
160        } else {
161            Log.e(TAG, "Unable to write log, build must be debuggable.");
162        }
163
164        Log.wtf(TAG, tag + " " + s + "; " + fileMessage);
165    }
166}
167