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