1f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He/*
2f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * Copyright (C) 2014 The Android Open Source Project
3f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He *
4f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * Licensed under the Apache License, Version 2.0 (the "License");
5f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * you may not use this file except in compliance with the License.
6f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * You may obtain a copy of the License at
7f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He *
8f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He *      http://www.apache.org/licenses/LICENSE-2.0
9f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He *
10f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * Unless required by applicable law or agreed to in writing, software
11f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * distributed under the License is distributed on an "AS IS" BASIS,
12f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * See the License for the specific language governing permissions and
14f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He * limitations under the License.
15f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He */
16f02d3c605f5d1d091ca7e27170010ad23cd22134Jack Hepackage com.android.music.utils;
17f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
18f02d3c605f5d1d091ca7e27170010ad23cd22134Jack Heimport android.util.Log;
19f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
20f02d3c605f5d1d091ca7e27170010ad23cd22134Jack Hepublic class LogHelper {
21f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    private static final String LOG_PREFIX = "music_";
22f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
23f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    private static final int MAX_LOG_TAG_LENGTH = 23;
24f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    private static final boolean DEBUG = true;
25f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
26f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static String makeLogTag(String str) {
27f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
28f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
29f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        }
30f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
31f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        return LOG_PREFIX + str;
32f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
33f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
34f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    /**
35f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He     * Don't use this when obfuscating class names!
36f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He     */
37f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static String makeLogTag(Class cls) {
38f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        return makeLogTag(cls.getSimpleName());
39f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
40f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
41f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static void v(String tag, Object... messages) {
42f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        // Only log VERBOSE if build type is DEBUG
43f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        if (DEBUG) {
44f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            log(tag, Log.VERBOSE, null, messages);
45f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        }
46f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
47f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
48f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static void d(String tag, Object... messages) {
49f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        // Only log DEBUG if build type is DEBUG
50f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        if (DEBUG) {
51f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            log(tag, Log.DEBUG, null, messages);
52f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        }
53f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
54f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
55f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static void i(String tag, Object... messages) {
56f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        log(tag, Log.INFO, null, messages);
57f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
58f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
59f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static void w(String tag, Object... messages) {
60f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        log(tag, Log.WARN, null, messages);
61f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
62f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
63f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static void w(String tag, Throwable t, Object... messages) {
64f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        log(tag, Log.WARN, t, messages);
65f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
66f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
67f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static void e(String tag, Object... messages) {
68f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        log(tag, Log.ERROR, null, messages);
69f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
70f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
71f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static void e(String tag, Throwable t, Object... messages) {
72f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        log(tag, Log.ERROR, t, messages);
73f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
74f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He
75f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    public static void log(String tag, int level, Throwable t, Object... messages) {
76f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        String message;
77f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        if (t == null && messages != null && messages.length == 1) {
78f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            // handle this common case without the extra cost of creating a stringbuffer:
79f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            message = messages[0].toString();
80f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        } else {
81f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            StringBuilder sb = new StringBuilder();
82f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            if (messages != null)
83f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He                for (Object m : messages) {
84f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He                    sb.append(m);
85f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He                }
86f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            if (t != null) {
87f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He                sb.append("\n").append(Log.getStackTraceString(t));
88f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            }
89f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He            message = sb.toString();
90f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        }
91f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He        Log.println(level, tag, message);
92f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He    }
93f02d3c605f5d1d091ca7e27170010ad23cd22134Jack He}
94