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