1 /*
2  * Copyright (C) 2024 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 package com.android.telephony.imsmedia.util;
17 
18 import android.annotation.IntDef;
19 import android.os.Build;
20 import android.support.annotation.VisibleForTesting;
21 
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 
25 /**
26  * This class provides the logging interface and utility methods
27  * that will be helpful when adding the log statement.
28  */
29 public final class Log {
30     public static final String TAG = "Log";
31     public static final String HIDDEN = "****";
32     public static final String EMPTY = "(empty)";
33     public static final String NULL = "(null)";
34     /**
35      * Logging options.
36      */
37 
38     /**
39      * Priority Level constant for Logging all level logs; use Log.v.
40      */
41     public static final int LOG_LEVEL_ALL = 0;
42 
43     /**
44      * Priority Level constant for Logging Verbose logs; use Log.v.
45      */
46     public static final int LOG_LEVEL_VERBOSE = 1;
47 
48     /**
49      * Priority Level constant for Logging debug logs; use Log.d.
50      */
51     public static final int LOG_LEVEL_DEBUG = 2;
52 
53     /**
54      * Priority Level constant for Logging Info logs; use Log.i.
55      */
56     public static final int LOG_LEVEL_INFO = 3;
57 
58     /**
59      * Priority Level constant for Logging Warning logs; use Log.w.
60      */
61     public static final int LOG_LEVEL_WARN = 4;
62 
63     /**
64      * Priority Level constant for Logging Error logs; use Log.e.
65      */
66     public static final int LOG_LEVEL_ERROR = 5;
67 
68     /** @hide */
69     @IntDef(
70         value = {
71             LOG_LEVEL_ALL,
72             LOG_LEVEL_VERBOSE,
73             LOG_LEVEL_DEBUG,
74             LOG_LEVEL_INFO,
75             LOG_LEVEL_WARN,
76             LOG_LEVEL_ERROR
77         })
78     @Retention(RetentionPolicy.SOURCE)
79     public @interface LogLevel {}
80 
81     // all levels(V / D / I / W / E)
82     public static final int DEFAULT_LOG_LEVEL = LOG_LEVEL_ALL;
83 
84     private static int sDebug = -1;
85     private static int sLogLevel = LOG_LEVEL_ALL;
86 
87     static {
88         if (Build.IS_DEBUGGABLE) {
89             sDebug = 1;
90             init(DEFAULT_LOG_LEVEL);
Log.d(TAG, "All logs are enabled")91             Log.d(TAG, "All logs are enabled");
92         } else {
93             init(LOG_LEVEL_DEBUG);
Log.d(TAG, "Verbose logs are disabled")94             Log.d(TAG, "Verbose logs are disabled");
95         }
96     }
97 
98     /**
99      * Initializes the logging configuration.
100      *
101      * @param logLevel A logging priority level.
102      */
init(@ogLevel int logLevel)103     public static void init(@LogLevel int logLevel) {
104         setLogLevel(logLevel);
105     }
106 
107     /**
108      * Returns whether the debug mode is enabled or not.
109      */
isDebuggable()110     public static boolean isDebuggable() {
111         if (sDebug == 1) {
112             return true;
113         } else if (sDebug == -1) {
114             return isLoggable(android.util.Log.DEBUG);
115         }
116 
117         return false;
118     }
119 
120     /**
121      * Checks whether android logging is enabled for the given log level.
122      *
123      * @param level log level
124      */
isLoggable(int level)125     public static boolean isLoggable(int level) {
126         return android.util.Log.isLoggable(TAG, level);
127     }
128 
129     /** Sets the debug mode from the specified flag. */
130     @VisibleForTesting
setDebuggable(boolean enableDebug)131     static void setDebuggable(boolean enableDebug) {
132         sDebug = enableDebug ? 1 : 0;
133     }
134 
135     /** Returns the log Level. */
136     @VisibleForTesting
getLogLevel()137     static int getLogLevel() {
138         return sLogLevel;
139     }
140 
141     /** Sets the log Level. */
setLogLevel(@ogLevel int logLevel)142     public static void setLogLevel(@LogLevel int logLevel) {
143         sLogLevel = logLevel;
144     }
145 
146     /** Checks whether ImsMedia logging is enabled or not for given level. */
147     @VisibleForTesting
isLogEnabled(@ogLevel int level)148     static boolean isLogEnabled(@LogLevel int level) {
149         if (sLogLevel == LOG_LEVEL_ALL) {
150             // All logs are available as default.
151             return true;
152         }
153         return sLogLevel <= level;
154     }
155 
156     /**
157      * Prints the verbose level log.
158      */
v(String tag, String msg)159     public static void v(String tag, String msg) {
160         if (isLogEnabled(LOG_LEVEL_VERBOSE)) {
161             android.util.Log.v(tag, msg);
162         }
163     }
164 
165     /**
166      * Prints the debug level log.
167      */
d(String tag, String msg)168     public static void d(String tag, String msg) {
169         if (isLogEnabled(LOG_LEVEL_DEBUG)) {
170             android.util.Log.d(tag, msg);
171         }
172     }
173 
174     /**
175      * Prints the debug level log when the debug mode is enabled.
176      */
dc(String tag, String msg)177     public static void dc(String tag, String msg) {
178         if (isDebuggable()) {
179             d(tag, msg);
180         }
181     }
182 
183     /**
184      * Prints the information level log.
185      */
i(String tag, String msg)186     public static void i(String tag, String msg) {
187         if (isLogEnabled(LOG_LEVEL_INFO)) {
188             android.util.Log.i(tag, msg);
189         }
190     }
191 
192     /**
193      * Prints the warning level log.
194      */
w(String tag, String msg)195     public static void w(String tag, String msg) {
196         if (isLogEnabled(LOG_LEVEL_WARN)) {
197             android.util.Log.w(tag, msg);
198         }
199     }
200 
201     /**
202      * Prints the error level log.
203      */
e(String tag, String msg)204     public static void e(String tag, String msg) {
205         if (isLogEnabled(LOG_LEVEL_ERROR)) {
206             android.util.Log.e(tag, msg);
207         }
208     }
209 
210     /**
211      * Prints the error level log.
212      */
e(String tag, String msg, Throwable t)213     public static void e(String tag, String msg, Throwable t) {
214         if (isLogEnabled(LOG_LEVEL_ERROR)) {
215             android.util.Log.e(tag, msg, t);
216         }
217     }
218 
219     /**
220      * Hides personal identifiable information logging string if debug mode is disabled.
221      *
222      * @param msg logging string
223      * @return hidden string if debug mode is disabled. Original string otherwise.
224      */
hidePii(String msg)225     public static String hidePii(String msg) {
226         if (msg == null) {
227             return NULL;
228         } else if (msg.isEmpty()) {
229             return EMPTY;
230         } else if (isDebuggable()) {
231             return msg;
232         }
233 
234         return HIDDEN;
235     }
236 
237 }
238