LogUtils.java revision cf755e813f96142c194db638b46bf73fed81c92e
1/*
2 * Copyright (C) 2016 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
17package com.android.deskclock;
18
19import android.os.Build;
20import android.util.Log;
21
22public class LogUtils {
23
24    /**
25     * Default logger used for generic logging, i.eTAG. when a specific log tag isn't specified.
26     */
27    private final static Logger DEFAULT_LOGGER = new Logger("AlarmClock");
28
29    public static void v(String message, Object... args) {
30        DEFAULT_LOGGER.v(message, args);
31    }
32
33    public static void d(String message, Object... args) {
34        DEFAULT_LOGGER.d(message, args);
35    }
36
37    public static void i(String message, Object... args) {
38        DEFAULT_LOGGER.i(message, args);
39    }
40
41    public static void w(String message, Object... args) {
42        DEFAULT_LOGGER.w(message, args);
43    }
44
45    public static void e(String message, Object... args) {
46        DEFAULT_LOGGER.e(message, args);
47    }
48
49    public static void e(String message, Throwable e) {
50        DEFAULT_LOGGER.e(message, e);
51    }
52
53    public static void wtf(String message, Object... args) {
54        DEFAULT_LOGGER.wtf(message, args);
55    }
56
57    public static void wtf(Throwable e) {
58        DEFAULT_LOGGER.wtf(e);
59    }
60
61    public final static class Logger {
62
63        /**
64         * Log everything for debug builds or if running on a dev device.
65         */
66        public final static boolean DEBUG = BuildConfig.DEBUG
67                || "eng".equals(Build.TYPE)
68                || "userdebug".equals(Build.TYPE);
69
70        public final String logTag;
71
72        public Logger(String logTag) {
73            this.logTag = logTag;
74        }
75
76        public boolean isVerboseLoggable() { return DEBUG || Log.isLoggable(logTag, Log.VERBOSE); }
77        public boolean isDebugLoggable() { return DEBUG || Log.isLoggable(logTag, Log.DEBUG); }
78        public boolean isInfoLoggable() { return DEBUG || Log.isLoggable(logTag, Log.INFO); }
79        public boolean isWarnLoggable() { return DEBUG || Log.isLoggable(logTag, Log.WARN); }
80        public boolean isErrorLoggable() { return DEBUG || Log.isLoggable(logTag, Log.ERROR); }
81        public boolean isWtfLoggable() { return DEBUG || Log.isLoggable(logTag, Log.ASSERT); }
82
83        public void v(String message, Object... args) {
84            if (isVerboseLoggable()) {
85                Log.v(logTag, args == null || args.length == 0
86                        ? message : String.format(message, args));
87            }
88        }
89
90        public void d(String message, Object... args) {
91            if (isDebugLoggable()) {
92                Log.d(logTag, args == null || args.length == 0 ? message
93                        : String.format(message, args));
94            }
95        }
96
97        public void i(String message, Object... args) {
98            if (isInfoLoggable()) {
99                Log.i(logTag, args == null || args.length == 0 ? message
100                        : String.format(message, args));
101            }
102        }
103
104        public void w(String message, Object... args) {
105            if (isWarnLoggable()) {
106                Log.w(logTag, args == null || args.length == 0 ? message
107                        : String.format(message, args));
108            }
109        }
110
111        public void e(String message, Object... args) {
112            if (isErrorLoggable()) {
113                Log.e(logTag, args == null || args.length == 0 ? message
114                        : String.format(message, args));
115            }
116        }
117
118        public void e(String message, Throwable e) {
119            if (isErrorLoggable()) {
120                Log.e(logTag, message, e);
121            }
122        }
123
124        public void wtf(String message, Object... args) {
125            if (isWtfLoggable()) {
126                Log.wtf(logTag, args == null || args.length == 0 ? message
127                        : String.format(message, args));
128            }
129        }
130
131        public void wtf(Throwable e) {
132            if (isWtfLoggable()) {
133                Log.wtf(logTag, e);
134            }
135        }
136    }
137}