blob: 3a0d2eeb84bc854973c12919cb7752aea5b81a03 [file] [log] [blame]
Jack Hef02d3c62017-02-21 00:39:22 -05001/*
2 * Copyright (C) 2014 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 */
16package com.android.music.utils;
17
18import android.util.Log;
19
20public class LogHelper {
21 private static final String LOG_PREFIX = "music_";
22 private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
23 private static final int MAX_LOG_TAG_LENGTH = 23;
24 private static final boolean DEBUG = true;
25
26 public static String makeLogTag(String str) {
27 if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
28 return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
29 }
30
31 return LOG_PREFIX + str;
32 }
33
34 /**
35 * Don't use this when obfuscating class names!
36 */
37 public static String makeLogTag(Class cls) {
38 return makeLogTag(cls.getSimpleName());
39 }
40
41 public static void v(String tag, Object... messages) {
42 // Only log VERBOSE if build type is DEBUG
43 if (DEBUG) {
44 log(tag, Log.VERBOSE, null, messages);
45 }
46 }
47
48 public static void d(String tag, Object... messages) {
49 // Only log DEBUG if build type is DEBUG
50 if (DEBUG) {
51 log(tag, Log.DEBUG, null, messages);
52 }
53 }
54
55 public static void i(String tag, Object... messages) {
56 log(tag, Log.INFO, null, messages);
57 }
58
59 public static void w(String tag, Object... messages) {
60 log(tag, Log.WARN, null, messages);
61 }
62
63 public static void w(String tag, Throwable t, Object... messages) {
64 log(tag, Log.WARN, t, messages);
65 }
66
67 public static void e(String tag, Object... messages) {
68 log(tag, Log.ERROR, null, messages);
69 }
70
71 public static void e(String tag, Throwable t, Object... messages) {
72 log(tag, Log.ERROR, t, messages);
73 }
74
75 public static void log(String tag, int level, Throwable t, Object... messages) {
76 String message;
77 if (t == null && messages != null && messages.length == 1) {
78 // handle this common case without the extra cost of creating a stringbuffer:
79 message = messages[0].toString();
80 } else {
81 StringBuilder sb = new StringBuilder();
82 if (messages != null)
83 for (Object m : messages) {
84 sb.append(m);
85 }
86 if (t != null) {
87 sb.append("\n").append(Log.getStackTraceString(t));
88 }
89 message = sb.toString();
90 }
91 Log.println(level, tag, message);
92 }
93}