Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: base/android/java/src/org/chromium/base/Log.java

Issue 1048153002: Add an utility class to improve java logs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add overrides of the log functions to allow proguard to properly remove arguments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.base;
6
7 import org.chromium.base.annotations.NoSideEffects;
8
9 import java.util.Locale;
10
11 /**
12 * Utility class for Logging.
13 *
14 * <p>
15 * Defines logging access points for each feature. They format and forward the l ogs to
16 * {@link android.util.Log}, allowing to standardize the output, to make it easy to identify
17 * the origin of logs, and enable or disable logging in different parts of the c ode.
18 * </p>
19 * <p>
20 * Please make use of the formatting capability of the logging methods rather th an doing
21 * concatenations in the calling code. In the release builds of Chrome, debug an d verbose log
22 * calls will be stripped out of the binary. Concatenations and method calls how ever will still
23 * remain and be executed. If they can't be avoided, use {@link Log#isEnabled(in t)} to guard
24 * such calls. Another possibility is to annotate methods to be called with {@li nk NoSideEffects}.
25 * </p>
26 *
27 * Usage:
28 * <pre>
29 * Log.FEATURE.d("MyTag", "My %s message", awesome);
30 * </pre>
31 *
32 * Logcat output:
33 * <pre>
34 * D/chromium.Feature (999): [MyTag] My awesome message
35 * </pre>
36 *
37 * Set the log level for a given feature:
38 * <pre>
39 * adb shell setprop log.tag.chromium:Feature VERBOSE
40 * </pre>
41 *
42 *<p>
43 *<b>Notes:</b>
44 * <ul>
45 * <li>New features or features not having a dedicated logger: please make a new one rather
46 * than using {@link #ROOT}.</li>
47 * <li>The class exposes different versions of the log functions based on the nu mber of arguments
48 * needed. This is to allow proguard to properly remove them from the release bu ilds. The usage
49 * is exactly the same.</li>
50 * </ul>
51 * </p>
52 */
53 public class Log {
54 /**
55 * Logger for the "chromium" tag.
56 * Note: Disabling logging for that one will not disable the others.
57 */
58 public static final Log ROOT = new Log(null);
59
60 private static final String BASE_TAG = "chromium";
61
62 /**
63 * Maximum length for the feature tag.
64 *
65 * A complete tag will look like <code>chromium:FooFeature</code>. Because o f the 23 characters
66 * limit on log tags, feature tags have to be restricted to fit.
67 */
68 private static final int MAX_FEATURE_TAG_LENGTH = 23 - 1 - BASE_TAG.length() ;
69
70 @VisibleForTesting final String mTag;
71
72 /**
73 * Creates a new logging access point for the given tag.
74 * @throws IllegalArgumentException If <code>featureTag</code> is too long. The complete
75 * tag has to fit within 23 characters.
76 */
77 protected Log(String featureTag) {
78 if (featureTag == null) {
79 mTag = BASE_TAG;
80 return;
81 } else if (featureTag.length() > MAX_FEATURE_TAG_LENGTH) {
82 throw new IllegalArgumentException(
83 "The feature tag can be at most " + MAX_FEATURE_TAG_LENGTH + " characters.");
84 } else {
85 mTag = BASE_TAG + "." + featureTag;
86 }
87 }
88
89 /** Returns whether this logger is currently allowed to send logs.*/
90 public boolean isEnabled(int level) {
91 return android.util.Log.isLoggable(mTag, level);
92 }
93
94 /** Returns a formatted log message, using the supplied format and arguments .*/
95 @VisibleForTesting
96 protected String formatLog(String secondaryTag, String messageTemplate, Obje ct... params) {
97 if (params != null && params.length != 0) {
98 messageTemplate = String.format(Locale.US, messageTemplate, params);
99 }
100
101 return "[" + secondaryTag + "] " + messageTemplate;
102 }
103
104 /**
105 * Sends a {@link android.util.Log#VERBOSE} log message.
106 *
107 * For optimization purposes, use the fixed parameters versions if possible.
Yaron 2015/04/01 20:47:31 How does a client decide that? I assume this is ju
Torne 2015/04/02 10:50:05 It has a different name.
dgn 2015/04/02 12:47:20 The function names are different, so Log.X.v() ins
Yaron 2015/04/02 14:29:47 Yeesh. Sorry for my poor reading :S
108 *
109 * @param secondaryTag Used to identify the source of a log message. It usua lly identifies the
110 * class where the log call occurs.
111 * @param messageTemplate The message you would like logged. It is to be spe cified as a format
112 * string.
113 * @param args Arguments referenced by the format specifiers in the format s tring.
114 */
115 public void verbose(String secondaryTag, String messageTemplate, Object... a rgs) {
Yaron 2015/04/01 20:47:31 Do we have any examples using more than 7? Perhaps
Torne 2015/04/02 10:50:05 Yeah, I'd be inclined to make this private and if
dgn 2015/04/02 14:47:15 Done. For consistency, I removed the Log.i() varia
116 if (isEnabled(android.util.Log.VERBOSE)) {
117 android.util.Log.v(mTag, formatLog(secondaryTag, messageTemplate, ar gs));
118 }
119 }
120
121 /** Sends a {@link android.util.Log#VERBOSE} log message. 0 arg version. */
122 public void v(String secondaryTag, String message) {
Yaron 2015/04/01 20:47:31 So for release builds, all of these variants are s
dgn 2015/04/02 12:47:20 Yes. Proguard config (CL here: https://chrome-inte
123 verbose(secondaryTag, message);
124 }
125
126 /** Sends a {@link android.util.Log#VERBOSE} log message. 1 arg version. */
127 public void v(String secondaryTag, String messageTemplate, Object arg1) {
128 verbose(secondaryTag, messageTemplate, arg1);
129 }
130
131 /** Sends a {@link android.util.Log#VERBOSE} log message. 2 args version */
132 public void v(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2) {
133 verbose(secondaryTag, messageTemplate, arg1, arg2);
134 }
135
136 /** Sends a {@link android.util.Log#VERBOSE} log message. 3 args version */
137 public void v(
138 String secondaryTag, String messageTemplate, Object arg1, Object arg 2, Object arg3) {
139 verbose(secondaryTag, messageTemplate, arg1, arg2, arg3);
140 }
141
142 /** Sends a {@link android.util.Log#VERBOSE} log message. 4 args version */
143 public void v(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
144 Object arg3, Object arg4) {
145 verbose(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4);
146 }
147
148 /** Sends a {@link android.util.Log#VERBOSE} log message. 5 args version */
149 public void v(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
150 Object arg3, Object arg4, Object arg5) {
151 verbose(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5);
152 }
153
154 /** Sends a {@link android.util.Log#VERBOSE} log message. 6 args version */
155 public void v(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
156 Object arg3, Object arg4, Object arg5, Object arg6) {
157 verbose(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg 6);
158 }
159
160 /** Sends a {@link android.util.Log#VERBOSE} log message. 7 args version */
161 public void v(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
162 Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) {
163 verbose(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg 6, arg7);
164 }
165
166 /**
167 * Sends a {@link android.util.Log#DEBUG} log message.
168 *
169 * For optimization purposes, use the fixed parameters versions if possible.
170 *
171 * @param secondaryTag Used to identify the source of a log message. It usua lly identifies the
172 * class where the log call occurs.
173 * @param messageTemplate The message you would like logged. It is to be spe cified as a format
174 * string.
175 * @param args Arguments referenced by the format specifiers in the format s tring.
176 */
177 public void debug(String secondaryTag, String messageTemplate, Object... arg s) {
178 if (isEnabled(android.util.Log.DEBUG)) {
179 android.util.Log.d(mTag, formatLog(secondaryTag, messageTemplate, ar gs));
180 }
181 }
182
183 /** Sends a {@link android.util.Log#DEBUG} log message. 0 arg version. */
184 public void d(String secondaryTag, String message) {
185 debug(secondaryTag, message);
186 }
187
188 /** Sends a {@link android.util.Log#DEBUG} log message. 1 arg version. */
189 public void d(String secondaryTag, String messageTemplate, Object arg1) {
190 debug(secondaryTag, messageTemplate, arg1);
191 }
192 /** Sends a {@link android.util.Log#DEBUG} log message. 2 args version */
193 public void d(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2) {
194 debug(secondaryTag, messageTemplate, arg1, arg2);
195 }
196 /** Sends a {@link android.util.Log#DEBUG} log message. 3 args version */
197 public void d(
198 String secondaryTag, String messageTemplate, Object arg1, Object arg 2, Object arg3) {
199 debug(secondaryTag, messageTemplate, arg1, arg2, arg3);
200 }
201
202 /** Sends a {@link android.util.Log#DEBUG} log message. 4 args version */
203 public void d(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
204 Object arg3, Object arg4) {
205 debug(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4);
206 }
207
208 /** Sends a {@link android.util.Log#DEBUG} log message. 5 args version */
209 public void d(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
210 Object arg3, Object arg4, Object arg5) {
211 debug(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5);
212 }
213
214 /** Sends a {@link android.util.Log#DEBUG} log message. 6 args version */
215 public void d(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
216 Object arg3, Object arg4, Object arg5, Object arg6) {
217 debug(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6) ;
218 }
219
220 /** Sends a {@link android.util.Log#DEBUG} log message. 7 args version */
221 public void d(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
222 Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) {
223 debug(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
224 }
225
226 /**
227 * Sends an {@link android.util.Log#INFO} log message.
228 *
229 * For optimization purposes, use the fixed parameters versions if possible.
230 *
231 * @param secondaryTag Used to identify the source of a log message. It usua lly identifies the
232 * class where the log call occurs.
233 * @param messageTemplate The message you would like logged. It is to be spe cified as a format
234 * string.
235 * @param args Arguments referenced by the format specifiers in the format s tring.
236 */
237 public void info(String secondaryTag, String messageTemplate, Object... args ) {
238 if (isEnabled(android.util.Log.INFO)) {
239 android.util.Log.i(mTag, formatLog(secondaryTag, messageTemplate, ar gs));
240 }
241 }
242 /** Sends an {@link android.util.Log#INFO} log message. 0 arg version. */
243 public void i(String secondaryTag, String message) {
244 info(secondaryTag, message);
245 }
246 /** Sends an {@link android.util.Log#INFO} log message. 1 arg version. */
247 public void i(String secondaryTag, String messageTemplate, Object arg1) {
248 info(secondaryTag, messageTemplate, arg1);
249 }
250 /** Sends an {@link android.util.Log#INFO} log message. 2 args version */
251 public void i(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2) {
252 info(secondaryTag, messageTemplate, arg1, arg2);
253 }
254 /** Sends an {@link android.util.Log#INFO} log message. 3 args version */
255 public void i(
256 String secondaryTag, String messageTemplate, Object arg1, Object arg 2, Object arg3) {
257 info(secondaryTag, messageTemplate, arg1, arg2, arg3);
258 }
259
260 /** Sends an {@link android.util.Log#INFO} log message. 4 args version */
261 public void i(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
262 Object arg3, Object arg4) {
263 info(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4);
264 }
265
266 /** Sends an {@link android.util.Log#INFO} log message. 5 args version */
267 public void i(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
268 Object arg3, Object arg4, Object arg5) {
269 info(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5);
270 }
271
272 /** Sends an {@link android.util.Log#INFO} log message. 6 args version */
273 public void i(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
274 Object arg3, Object arg4, Object arg5, Object arg6) {
275 info(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6);
276 }
277
278 /** Sends an {@link android.util.Log#INFO} log message. 7 args version */
279 public void i(String secondaryTag, String messageTemplate, Object arg1, Obje ct arg2,
280 Object arg3, Object arg4, Object arg5, Object arg6, Object arg7) {
281 info(secondaryTag, messageTemplate, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
282 }
283
284 /**
285 * Sends a {@link android.util.Log#WARN} log message.
286 *
287 * @param secondaryTag Used to identify the source of a log message. It usua lly identifies the
288 * class where the log call occurs.
289 * @param messageTemplate The message you would like logged. It is to be spe cified as a format
290 * string.
291 * @param args Arguments referenced by the format specifiers in the format s tring.
292 */
293 public void w(String secondaryTag, String messageTemplate, Object... args) {
294 if (isEnabled(android.util.Log.WARN)) {
295 android.util.Log.w(mTag, formatLog(secondaryTag, messageTemplate, ar gs));
296 }
297 }
298
299 /**
300 * Sends an {@link android.util.Log#ERROR} log message.
301 *
302 * @param secondaryTag Used to identify the source of a log message. It usua lly identifies the
303 * class where the log call occurs.
304 * @param messageTemplate The message you would like logged. It is to be spe cified as a format
305 * string.
306 * @param args Arguments referenced by the format specifiers in the format s tring.
307 */
308 public void e(String secondaryTag, String messageTemplate, Object... args) {
309 if (isEnabled(android.util.Log.ERROR)) {
310 android.util.Log.e(mTag, formatLog(secondaryTag, messageTemplate, ar gs));
311 }
312 }
313 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698