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