Home | History | Annotate | Download | only in utils
      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     private static final String LOG_PREFIX = "sample_";
     24     private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
     25     private static final int MAX_LOG_TAG_LENGTH = 23;
     26 
     27     public static String makeLogTag(String str) {
     28         if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
     29             return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
     30         }
     31 
     32         return LOG_PREFIX + str;
     33     }
     34 
     35     public static void v(String tag, Object... messages) {
     36         // Only log VERBOSE if build type is DEBUG
     37         if (BuildConfig.DEBUG) {
     38             log(tag, Log.VERBOSE, null, messages);
     39         }
     40     }
     41 
     42     public static void d(String tag, Object... messages) {
     43         // Only log DEBUG if build type is DEBUG
     44         if (BuildConfig.DEBUG) {
     45             log(tag, Log.DEBUG, null, messages);
     46         }
     47     }
     48 
     49     public static void i(String tag, Object... messages) {
     50         log(tag, Log.INFO, null, messages);
     51     }
     52 
     53     public static void w(String tag, Object... messages) {
     54         log(tag, Log.WARN, null, messages);
     55     }
     56 
     57     public static void w(String tag, Throwable t, Object... messages) {
     58         log(tag, Log.WARN, t, messages);
     59     }
     60 
     61     public static void e(String tag, Object... messages) {
     62         log(tag, Log.ERROR, null, messages);
     63     }
     64 
     65     public static void e(String tag, Throwable t, Object... messages) {
     66         log(tag, Log.ERROR, t, messages);
     67     }
     68 
     69     public static void log(String tag, int level, Throwable t, Object... messages) {
     70         if (messages != null && Log.isLoggable(tag, level)) {
     71             String message;
     72             if (messages.length == 1) {
     73                 message = messages[0] == null ? null : messages[0].toString();
     74             } else {
     75                 StringBuilder sb = new StringBuilder();
     76                 for (Object m: messages) {
     77                     sb.append(m);
     78                 }
     79                 if (t != null) {
     80                     sb.append("\n").append(Log.getStackTraceString(t));
     81                 }
     82                 message = sb.toString();
     83             }
     84             Log.println(level, tag, message);
     85         }
     86     }
     87 }
     88