summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/util/LogUtil.java
blob: 021f39b4d5420f6fcf4919efa0913d8df13cefb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.util;

/**
 * Log utility class.
 */
public class LogUtil {
    public static final String BUGLE_TAG = "MessagingApp";
    public static final String PROFILE_TAG = "MessagingAppProf";
    public static final String BUGLE_DATABASE_TAG = "MessagingAppDb";
    public static final String BUGLE_DATABASE_PERF_TAG = "MessagingAppDbPerf";
    public static final String BUGLE_DATAMODEL_TAG = "MessagingAppDataModel";
    public static final String BUGLE_IMAGE_TAG = "MessagingAppImage";
    public static final String BUGLE_NOTIFICATIONS_TAG = "MessagingAppNotif";
    public static final String BUGLE_WIDGET_TAG = "MessagingAppWidget";

    public static final int DEBUG = android.util.Log.DEBUG;
    public static final int WARN = android.util.Log.WARN;
    public static final int VERBOSE = android.util.Log.VERBOSE;
    public static final int INFO = android.util.Log.INFO;
    public static final int ERROR = android.util.Log.ERROR;

    // If this is non-null, DEBUG and higher logs will be tracked in-memory. It will not include
    // VERBOSE logs.
    private static LogSaver sDebugLogSaver;
    private static volatile boolean sCaptureDebugLogs;

    /**
     * Read Gservices to see if logging should be enabled.
     */
    public static void refreshGservices(final BugleGservices gservices) {
        sCaptureDebugLogs = gservices.getBoolean(
                BugleGservicesKeys.ENABLE_LOG_SAVER,
                BugleGservicesKeys.ENABLE_LOG_SAVER_DEFAULT);
        if (sCaptureDebugLogs && (sDebugLogSaver == null || !sDebugLogSaver.isCurrent())) {
            // We were not capturing logs before. We are now.
            sDebugLogSaver = LogSaver.newInstance();
        } else if (!sCaptureDebugLogs && sDebugLogSaver != null) {
            // We were capturing logs. We aren't anymore.
            sDebugLogSaver = null;
        }
    }

 // This is called from FactoryImpl once the Gservices class is initialized.
    public static void initializeGservices (final BugleGservices gservices) {
        gservices.registerForChanges(new Runnable() {
            @Override
            public void run() {
                refreshGservices(gservices);
            }
        });
        refreshGservices(gservices);
    }

    /**
     * Send a {@link #VERBOSE} log message.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    public static void v(final String tag, final String msg) {
        println(android.util.Log.VERBOSE, tag, msg);
    }

    /**
     * Send a {@link #VERBOSE} log message.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr An exception to log
     */
    public static void v(final String tag, final String msg, final Throwable tr) {
        println(android.util.Log.VERBOSE, tag, msg + '\n'
                + android.util.Log.getStackTraceString(tr));
    }

    /**
     * Send a {@link #DEBUG} log message.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    public static void d(final String tag, final String msg) {
        println(android.util.Log.DEBUG, tag, msg);
    }

    /**
     * Send a {@link #DEBUG} log message and log the exception.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr An exception to log
     */
    public static void d(final String tag, final String msg, final Throwable tr) {
        println(android.util.Log.DEBUG, tag, msg + '\n'
                + android.util.Log.getStackTraceString(tr));
    }

    /**
     * Send an {@link #INFO} log message.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    public static void i(final String tag, final String msg) {
        println(android.util.Log.INFO, tag, msg);
    }

    /**
     * Send a {@link #INFO} log message and log the exception.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr An exception to log
     */
    public static void i(final String tag, final String msg, final Throwable tr) {
        println(android.util.Log.INFO, tag, msg + '\n'
                + android.util.Log.getStackTraceString(tr));
    }

    /**
     * Send a {@link #WARN} log message.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    public static void w(final String tag, final String msg) {
        println(android.util.Log.WARN, tag, msg);
    }

    /**
     * Send a {@link #WARN} log message and log the exception.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr An exception to log
     */
    public static void w(final String tag, final String msg, final Throwable tr) {
        println(android.util.Log.WARN, tag, msg);
        println(android.util.Log.WARN, tag, android.util.Log.getStackTraceString(tr));
    }

    /**
     * Send an {@link #ERROR} log message.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    public static void e(final String tag, final String msg) {
        println(android.util.Log.ERROR, tag, msg);
    }

    /**
     * Send a {@link #ERROR} log message and log the exception.
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     * @param tr An exception to log
     */
    public static void e(final String tag, final String msg, final Throwable tr) {
        println(android.util.Log.ERROR, tag, msg);
        println(android.util.Log.ERROR, tag, android.util.Log.getStackTraceString(tr));
    }

    /**
     * What a Terrible Failure: Report a condition that should never happen.
     * The error will always be logged at level ASSERT with the call stack.
     * Depending on system configuration, a report may be added to the
     * {@link android.os.DropBoxManager} and/or the process may be terminated
     * immediately with an error dialog.
     * @param tag Used to identify the source of a log message.
     * @param msg The message you would like logged.
     */
    public static void wtf(final String tag, final String msg) {
        // Make sure this goes into our log buffer
        println(android.util.Log.ASSERT, tag, "wtf\n" + msg);
        android.util.Log.wtf(tag, msg, new Exception());
    }

    /**
     * What a Terrible Failure: Report a condition that should never happen.
     * The error will always be logged at level ASSERT with the call stack.
     * Depending on system configuration, a report may be added to the
     * {@link android.os.DropBoxManager} and/or the process may be terminated
     * immediately with an error dialog.
     * @param tag Used to identify the source of a log message.
     * @param msg The message you would like logged.
     * @param tr An exception to log
     */
    public static void wtf(final String tag, final String msg, final Throwable tr) {
        // Make sure this goes into our log buffer
        println(android.util.Log.ASSERT, tag, "wtf\n" + msg + '\n' +
                android.util.Log.getStackTraceString(tr));
        android.util.Log.wtf(tag, msg, tr);
    }

    /**
     * Low-level logging call.
     * @param level The priority/type of this log message
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    private static void println(final int level, final String tag, final String msg) {
        android.util.Log.println(level, tag, msg);

        LogSaver serviceLog = sDebugLogSaver;
        if (serviceLog != null && level >= android.util.Log.DEBUG) {
            serviceLog.log(level, tag, msg);
        }
    }

    /**
     * Save logging into LogSaver only, for dumping to bug report
     *
     * @param level The priority/type of this log message
     * @param tag Used to identify the source of a log message.  It usually identifies
     *        the class or activity where the log call occurs.
     * @param msg The message you would like logged.
     */
    public static void save(final int level, final String tag, final String msg) {
        LogSaver serviceLog = sDebugLogSaver;
        if (serviceLog != null) {
            serviceLog.log(level, tag, msg);
        }
    }

    /**
     * Checks to see whether or not a log for the specified tag is loggable at the specified level.
     * See {@link android.util.Log#isLoggable(String, int)} for more discussion.
     */
    public static boolean isLoggable(final String tag, final int level) {
        return android.util.Log.isLoggable(tag, level);
    }

    /**
     * Returns text as is if {@value #BUGLE_TAG}'s log level is set to DEBUG or VERBOSE;
     * returns "--" otherwise. Useful for log statements where we don't want to log
     * various strings (e.g., usernames) with default logging to avoid leaking PII in logcat.
     */
    public static String sanitizePII(final String text) {
        if (text == null) {
            return null;
        }

        if (android.util.Log.isLoggable(BUGLE_TAG, android.util.Log.DEBUG)) {
            return text;
        } else {
            return "Redacted-" + text.length();
        }
    }

    public static void dump(java.io.PrintWriter out) {
        final LogSaver logsaver = sDebugLogSaver;
        if (logsaver != null) {
            logsaver.dump(out);
        }
    }
}