blob: 9690251842146b5b467ed95c6b5b78f2e51819b1 [file] [log] [blame]
Sailesh Nepal0f996eb2014-03-06 18:17:05 -08001/*
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
17package com.android.telecomm;
18
19import android.util.Log;
20
21import java.util.IllegalFormatException;
22import java.util.Locale;
23
24/**
25 * Manages logging for the entire module.
26 */
27public class LogUtil {
28
29 // Generic tag for all In Call logging
30 private static final String TAG = "Telecomm";
31
32 public static final boolean FORCE_LOGGING = true; /* STOP SHIP if true */
33 public static final boolean DEBUG = isLoggable(Log.DEBUG);
34 public static final boolean VERBOSE = isLoggable(Log.VERBOSE);
35
36 private LogUtil() {}
37
38 public static boolean isLoggable(int level) {
39 return FORCE_LOGGING || Log.isLoggable(TAG, level);
40 }
41
42 public static void d(String prefix, String format, Object... args) {
43 if (DEBUG) {
44 Log.d(TAG, buildMessage(prefix, format, args));
45 }
46 }
47
48 public static void d(Object objectPrefix, String format, Object... args) {
49 if (DEBUG) {
50 Log.d(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
51 }
52 }
53
54 public static void v(String prefix, String format, Object... args) {
55 if (VERBOSE) {
56 Log.v(TAG, buildMessage(prefix, format, args));
57 }
58 }
59
60 public static void v(Object objectPrefix, String format, Object... args) {
61 if (VERBOSE) {
62 Log.v(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
63 }
64 }
65
66 public static void w(String prefix, String format, Object... args) {
67 if (isLoggable(Log.WARN)) {
68 Log.w(TAG, buildMessage(prefix, format, args));
69 }
70 }
71
72 public static void w(Object objectPrefix, String format, Object... args) {
73 if (isLoggable(Log.WARN)) {
74 Log.w(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
75 }
76 }
77
78 public static void e(String prefix, Throwable tr, String format, Object... args) {
79 if (isLoggable(Log.ERROR)) {
80 Log.e(TAG, buildMessage(prefix, format, args), tr);
81 }
82 }
83
84 public static void e(Object objectPrefix, Throwable tr, String format, Object... args) {
85 if (isLoggable(Log.ERROR)) {
86 Log.e(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args), tr);
87 }
88 }
89
90 public static void wtf(String prefix, Throwable tr, String format, Object... args) {
91 Log.wtf(TAG, buildMessage(prefix, format, args), tr);
92 }
93
94 public static void wtf(Object objectPrefix, Throwable tr, String format, Object... args) {
95 Log.wtf(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args), tr);
96 }
97
98 public static void wtf(String prefix, String format, Object... args) {
99 Log.wtf(TAG, buildMessage(prefix, format, args));
100 }
101
102 public static void wtf(Object objectPrefix, String format, Object... args) {
103 Log.wtf(TAG, buildMessage(getPrefixFromObject(objectPrefix), format, args));
104 }
105
106 private static String getPrefixFromObject(Object obj) {
107 return obj == null ? "<null>" : obj.getClass().getSimpleName();
108 }
109
110 private static String buildMessage(String prefix, String format, Object... args) {
111 String msg;
112 try {
113 msg = (args == null || args.length == 0) ? format
114 : String.format(Locale.US, format, args);
115 } catch (IllegalFormatException ife) {
116 wtf("LogUtil", "IllegalFormatException: formatString='%s' numArgs=%d", format,
117 args.length);
118 msg = format + " (An error occurred while formatting the message.)";
119 }
120 return String.format(Locale.US, "%s: %s", prefix, msg);
121 }
122}