Home | History | Annotate | Download | only in managedprovisioning
      1 /*
      2  * Copyright 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 
     17 package com.android.managedprovisioning;
     18 
     19 import android.content.Context;
     20 import android.util.Log;
     21 import android.widget.Toast;
     22 
     23 /**
     24  * Utility class to centralize the logging in the Provisioning app.
     25  */
     26 public class ProvisionLogger {
     27     private static final String TAG = "ManagedProvisioning";
     28     private static final boolean LOG_ENABLED = true;
     29 
     30     // Never commit this as true.
     31     public static final boolean IS_DEBUG_BUILD = false;
     32 
     33     /**
     34      * Log the message at DEBUG level.
     35      */
     36     public static void logd(String message) {
     37         if (LOG_ENABLED) {
     38             Log.d(getTag(), message);
     39         }
     40     }
     41 
     42     /**
     43      * Log the message at DEBUG level.
     44      */
     45     public static void logd(String message, Throwable t) {
     46         if (LOG_ENABLED) {
     47             Log.d(getTag(), message, t);
     48         }
     49     }
     50 
     51     /**
     52      * Log the message at DEBUG level.
     53      */
     54     public static void logd(Throwable t) {
     55         if (LOG_ENABLED) {
     56             Log.d(getTag(), "", t);
     57         }
     58     }
     59 
     60     /**
     61      * Log the message at VERBOSE level.
     62      */
     63     public static void logv(String message) {
     64         if (LOG_ENABLED) {
     65             Log.v(getTag(), message);
     66         }
     67     }
     68 
     69     /**
     70      * Log the message at VERBOSE level.
     71      */
     72     public static void logv(String message, Throwable t) {
     73         if (LOG_ENABLED) {
     74             Log.v(getTag(), message, t);
     75         }
     76     }
     77 
     78     /**
     79      * Log the message at VERBOSE level.
     80      */
     81     public static void logv(Throwable t) {
     82         if (LOG_ENABLED) {
     83             Log.v(getTag(), "", t);
     84         }
     85     }
     86 
     87     /**
     88      * Log the message at INFO level.
     89      */
     90     public static void logi(String message) {
     91         if (LOG_ENABLED) {
     92             Log.i(getTag(), message);
     93         }
     94     }
     95 
     96     /**
     97      * Log the message at INFO level.
     98      */
     99     public static void logi(String message, Throwable t) {
    100         if (LOG_ENABLED) {
    101             Log.i(getTag(), message, t);
    102         }
    103     }
    104 
    105     /**
    106      * Log the message at INFO level.
    107      */
    108     public static void logi(Throwable t) {
    109         if (LOG_ENABLED) {
    110             Log.i(getTag(), "", t);
    111         }
    112     }
    113 
    114     /**
    115      * Log the message at WARNING level.
    116      */
    117     public static void logw(String message) {
    118         if (LOG_ENABLED) {
    119             Log.w(getTag(), message);
    120         }
    121     }
    122 
    123     /**
    124      * Log the message at WARNING level.
    125      */
    126     public static void logw(String message, Throwable t) {
    127         if (LOG_ENABLED) {
    128             Log.w(getTag(), message, t);
    129         }
    130     }
    131 
    132     /**
    133      * Log the message at WARNING level.
    134      */
    135     public static void logw(Throwable t) {
    136         if (LOG_ENABLED) {
    137             Log.w(getTag(), "", t);
    138         }
    139     }
    140 
    141     /**
    142      * Log the message at ERROR level.
    143      */
    144     public static void loge(String message) {
    145         if (LOG_ENABLED) {
    146             Log.e(getTag(), message);
    147         }
    148     }
    149 
    150     /**
    151      * Log the message at ERROR level.
    152      */
    153     public static void loge(String message, Throwable t) {
    154         if (LOG_ENABLED) {
    155             Log.e(getTag(), message, t);
    156         }
    157     }
    158 
    159     /**
    160      * Log the message at ERROR level.
    161      */
    162     public static void loge(Throwable t) {
    163         if (LOG_ENABLED) {
    164             Log.e(getTag(), "", t);
    165         }
    166     }
    167 
    168     /**
    169      * Walks the stack trace to figure out where the logging call came from.
    170      */
    171     static String getTag() {
    172         if (IS_DEBUG_BUILD) {
    173             String className = ProvisionLogger.class.getName();
    174 
    175             StackTraceElement[] trace = Thread.currentThread().getStackTrace();
    176             if (trace == null) {
    177                 return TAG;
    178             }
    179 
    180             boolean thisClassFound = false;
    181             for (StackTraceElement item : trace) {
    182                 if (item.getClassName().equals(className)) {
    183                     // we are at the current class, keep eating all items from this
    184                     // class.
    185                     thisClassFound = true;
    186                     continue;
    187                 }
    188 
    189                 if (thisClassFound) {
    190                     // This is the first instance of another class, which is most
    191                     // likely the caller class.
    192                     return TAG + String.format(
    193                             "[%s(%s): %s]", item.getFileName(), item.getLineNumber(),
    194                             item.getMethodName());
    195                 }
    196             }
    197         }
    198         return TAG;
    199     }
    200 
    201     public static void toast(Context context, String toast) {
    202         if (IS_DEBUG_BUILD) {
    203             Toast.makeText(context, toast, Toast.LENGTH_LONG).show();
    204         }
    205     }
    206 }
    207