1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.util;
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Log utility class.
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class LogUtil {
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String BUGLE_TAG = "MessagingApp";
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String PROFILE_TAG = "MessagingAppProf";
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String BUGLE_DATABASE_TAG = "MessagingAppDb";
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String BUGLE_DATABASE_PERF_TAG = "MessagingAppDbPerf";
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String BUGLE_DATAMODEL_TAG = "MessagingAppDataModel";
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String BUGLE_IMAGE_TAG = "MessagingAppImage";
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String BUGLE_NOTIFICATIONS_TAG = "MessagingAppNotif";
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String BUGLE_WIDGET_TAG = "MessagingAppWidget";
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int DEBUG = android.util.Log.DEBUG;
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int WARN = android.util.Log.WARN;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int VERBOSE = android.util.Log.VERBOSE;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int INFO = android.util.Log.INFO;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int ERROR = android.util.Log.ERROR;
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // If this is non-null, DEBUG and higher logs will be tracked in-memory. It will not include
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // VERBOSE logs.
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static LogSaver sDebugLogSaver;
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static volatile boolean sCaptureDebugLogs;
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Read Gservices to see if logging should be enabled.
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void refreshGservices(final BugleGservices gservices) {
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        sCaptureDebugLogs = gservices.getBoolean(
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                BugleGservicesKeys.ENABLE_LOG_SAVER,
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                BugleGservicesKeys.ENABLE_LOG_SAVER_DEFAULT);
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (sCaptureDebugLogs && (sDebugLogSaver == null || !sDebugLogSaver.isCurrent())) {
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // We were not capturing logs before. We are now.
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            sDebugLogSaver = LogSaver.newInstance();
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else if (!sCaptureDebugLogs && sDebugLogSaver != null) {
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // We were capturing logs. We aren't anymore.
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            sDebugLogSaver = null;
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // This is called from FactoryImpl once the Gservices class is initialized.
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void initializeGservices (final BugleGservices gservices) {
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        gservices.registerForChanges(new Runnable() {
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public void run() {
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                refreshGservices(gservices);
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        });
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        refreshGservices(gservices);
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send a {@link #VERBOSE} log message.
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void v(final String tag, final String msg) {
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.VERBOSE, tag, msg);
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send a {@link #VERBOSE} log message.
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tr An exception to log
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void v(final String tag, final String msg, final Throwable tr) {
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.VERBOSE, tag, msg + '\n'
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                + android.util.Log.getStackTraceString(tr));
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send a {@link #DEBUG} log message.
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void d(final String tag, final String msg) {
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.DEBUG, tag, msg);
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send a {@link #DEBUG} log message and log the exception.
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tr An exception to log
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void d(final String tag, final String msg, final Throwable tr) {
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.DEBUG, tag, msg + '\n'
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                + android.util.Log.getStackTraceString(tr));
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send an {@link #INFO} log message.
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void i(final String tag, final String msg) {
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.INFO, tag, msg);
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send a {@link #INFO} log message and log the exception.
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tr An exception to log
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void i(final String tag, final String msg, final Throwable tr) {
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.INFO, tag, msg + '\n'
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                + android.util.Log.getStackTraceString(tr));
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send a {@link #WARN} log message.
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void w(final String tag, final String msg) {
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.WARN, tag, msg);
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send a {@link #WARN} log message and log the exception.
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tr An exception to log
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void w(final String tag, final String msg, final Throwable tr) {
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.WARN, tag, msg);
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.WARN, tag, android.util.Log.getStackTraceString(tr));
156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send an {@link #ERROR} log message.
160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
164d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void e(final String tag, final String msg) {
165d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.ERROR, tag, msg);
166d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
167d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
168d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
169d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Send a {@link #ERROR} log message and log the exception.
170d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
171d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
172d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
173d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tr An exception to log
174d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
175d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void e(final String tag, final String msg, final Throwable tr) {
176d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.ERROR, tag, msg);
177d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.ERROR, tag, android.util.Log.getStackTraceString(tr));
178d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
179d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
180d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
181d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * What a Terrible Failure: Report a condition that should never happen.
182d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * The error will always be logged at level ASSERT with the call stack.
183d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Depending on system configuration, a report may be added to the
184d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * {@link android.os.DropBoxManager} and/or the process may be terminated
185d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * immediately with an error dialog.
186d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.
187d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
188d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
189d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void wtf(final String tag, final String msg) {
190d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Make sure this goes into our log buffer
191d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.ASSERT, tag, "wtf\n" + msg);
192d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        android.util.Log.wtf(tag, msg, new Exception());
193d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
194d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
195d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
196d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * What a Terrible Failure: Report a condition that should never happen.
197d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * The error will always be logged at level ASSERT with the call stack.
198d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Depending on system configuration, a report may be added to the
199d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * {@link android.os.DropBoxManager} and/or the process may be terminated
200d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * immediately with an error dialog.
201d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.
202d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
203d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tr An exception to log
204d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
205d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void wtf(final String tag, final String msg, final Throwable tr) {
206d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Make sure this goes into our log buffer
207d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        println(android.util.Log.ASSERT, tag, "wtf\n" + msg + '\n' +
208d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                android.util.Log.getStackTraceString(tr));
209d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        android.util.Log.wtf(tag, msg, tr);
210d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
211d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
212d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
213d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Low-level logging call.
214d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param level The priority/type of this log message
215d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
216d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
217d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
218d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
219d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static void println(final int level, final String tag, final String msg) {
220d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        android.util.Log.println(level, tag, msg);
221d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
222d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        LogSaver serviceLog = sDebugLogSaver;
223d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (serviceLog != null && level >= android.util.Log.DEBUG) {
224d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            serviceLog.log(level, tag, msg);
225d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
226d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
227d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
228d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
229d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Save logging into LogSaver only, for dumping to bug report
230d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *
231d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param level The priority/type of this log message
232d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param tag Used to identify the source of a log message.  It usually identifies
233d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *        the class or activity where the log call occurs.
234d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * @param msg The message you would like logged.
235d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
236d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void save(final int level, final String tag, final String msg) {
237d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        LogSaver serviceLog = sDebugLogSaver;
238d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (serviceLog != null) {
239d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            serviceLog.log(level, tag, msg);
240d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
241d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
242d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
243d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
244d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Checks to see whether or not a log for the specified tag is loggable at the specified level.
245d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * See {@link android.util.Log#isLoggable(String, int)} for more discussion.
246d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
247d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static boolean isLoggable(final String tag, final int level) {
248d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return android.util.Log.isLoggable(tag, level);
249d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
250d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
251d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
252d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Returns text as is if {@value #BUGLE_TAG}'s log level is set to DEBUG or VERBOSE;
253d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * returns "--" otherwise. Useful for log statements where we don't want to log
254d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * various strings (e.g., usernames) with default logging to avoid leaking PII in logcat.
255d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
256d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static String sanitizePII(final String text) {
257d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (text == null) {
258d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return null;
259d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
260d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
261d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (android.util.Log.isLoggable(BUGLE_TAG, android.util.Log.DEBUG)) {
262d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return text;
263d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
264d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return "Redacted-" + text.length();
265d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
266d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
267d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
268d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void dump(java.io.PrintWriter out) {
269d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final LogSaver logsaver = sDebugLogSaver;
270d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (logsaver != null) {
271d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            logsaver.dump(out);
272d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
273d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
274d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
275