1 /**
2  * Copyright (C) 2018 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.android.car.radio.util;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 /**
23  * {@link android.util.Log} wrapper that checks {@link android.util.Log#isLoggable} result first.
24  */
25 public final class Log {
26     public static final int VERBOSE = android.util.Log.VERBOSE;
27     public static final int DEBUG = android.util.Log.DEBUG;
28     public static final int INFO = android.util.Log.INFO;
29     public static final int WARN = android.util.Log.WARN;
30     public static final int ERROR = android.util.Log.ERROR;
31     public static final int ASSERT = android.util.Log.ASSERT;
32 
Log()33     private Log() {}
34 
35     /** See {@link android.util.Log#isLoggable}. */
isLoggable(@ullable String tag, int level)36     public static boolean isLoggable(@Nullable String tag, int level) {
37         return android.util.Log.isLoggable(tag, level);
38     }
39 
40     /** See {@link android.util.Log#v}. */
v(@ullable String tag, @NonNull String format, @Nullable Object...args)41     public static int v(@Nullable String tag, @NonNull String format, @Nullable Object...args) {
42         if (!isLoggable(tag, VERBOSE)) return 0;
43 
44         if (args != null) {
45             return android.util.Log.v(tag, String.format(format, args));
46         } else {
47             return android.util.Log.v(tag, format);
48         }
49     }
50 
51     /** See {@link android.util.Log#d}. */
d(@ullable String tag, @NonNull String format, @Nullable Object...args)52     public static int d(@Nullable String tag, @NonNull String format, @Nullable Object...args) {
53         if (!isLoggable(tag, DEBUG)) return 0;
54 
55         if (args != null) {
56             return android.util.Log.d(tag, String.format(format, args));
57         } else {
58             return android.util.Log.d(tag, format);
59         }
60     }
61 
62     /** See {@link android.util.Log#i}. */
i(@ullable String tag, @NonNull String msg)63     public static int i(@Nullable String tag, @NonNull String msg) {
64         if (!isLoggable(tag, INFO)) return 0;
65         return android.util.Log.i(tag, msg);
66     }
67 
68     /** See {@link android.util.Log#w}. */
w(@ullable String tag, @NonNull String msg)69     public static int w(@Nullable String tag, @NonNull String msg) {
70         if (!isLoggable(tag, WARN)) return 0;
71         return android.util.Log.w(tag, msg);
72     }
73 
74     /** See {@link android.util.Log#e}. */
e(@ullable String tag, @NonNull String msg)75     public static int e(@Nullable String tag, @NonNull String msg) {
76         if (!isLoggable(tag, ERROR)) return 0;
77         return android.util.Log.e(tag, msg);
78     }
79 
80     /** See {@link android.util.Log#e}. */
e(@ullable String tag, @Nullable String msg, @Nullable Throwable tr)81     public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
82         if (!isLoggable(tag, ERROR)) return 0;
83         return android.util.Log.e(tag, msg, tr);
84     }
85 }
86