• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.example.android.voicemail.common.logging;
18 
19 import android.util.Log;
20 
21 /**
22  * Simplifies usage of Android logging class {@link Log} by abstracting the TAG field that is
23  * required to be passed to every logging method. Also, allows automatic insertion of the owner
24  * class name prefix to log outputs for better debugging.
25  * <p>
26  * Use {@link #getLogger(Class)} to create an instance of Logger that automatically inserts the
27  * class name as a prefix to each log output. If you do not want the class name to be prefixed to
28  * log output then use {@link #getLogger()} to create the instance of Logger.
29  */
30 public class Logger {
31     private static final String APP_TAG = "VoicemailSample";
32 
33     /**
34      * Use this method if you want your class name to be prefixed to each log output.
35      */
getLogger(Class<?> classZ)36     public static Logger getLogger(Class<?> classZ) {
37         return new Logger(classZ.getSimpleName() + ": ");
38     }
39 
40     /**
41      * Use this factory method if you DO NOT want your class name to be prefixed into the log
42      * output.
43      */
getLogger()44     public static Logger getLogger() {
45         return new Logger();
46     }
47 
48     private final String mLogPrefix;
49 
50     /** No custom log prefix used. */
Logger()51     private Logger() {
52         mLogPrefix = null;
53     }
54 
55     /** Use the supplied custom prefix in log output. */
Logger(String logPrefix)56     private Logger(String logPrefix) {
57         mLogPrefix = logPrefix;
58     }
59 
getMsg(String msg)60     private String getMsg(String msg) {
61         if (mLogPrefix != null) {
62             return mLogPrefix + msg;
63         } else {
64             return msg;
65         }
66     }
67 
i(String msg)68     public void i(String msg) {
69         Log.i(APP_TAG, getMsg(msg));
70     }
71 
i(String msg, Throwable t)72     public void i(String msg, Throwable t) {
73         Log.i(APP_TAG, getMsg(msg), t);
74     }
75 
d(String msg)76     public void d(String msg) {
77         Log.d(APP_TAG, getMsg(msg));
78     }
79 
d(String msg, Throwable t)80     public void d(String msg, Throwable t) {
81         Log.d(APP_TAG, getMsg(msg), t);
82     }
83 
w(String msg)84     public void w(String msg) {
85         Log.w(APP_TAG, getMsg(msg));
86     }
87 
w(String msg, Throwable t)88     public void w(String msg, Throwable t) {
89         Log.w(APP_TAG, getMsg(msg), t);
90     }
91 
e(String msg)92     public void e(String msg) {
93         Log.e(APP_TAG, getMsg(msg));
94     }
95 
e(String msg, Throwable t)96     public void e(String msg, Throwable t) {
97         Log.e(APP_TAG, getMsg(msg), t);
98     }
99 }
100