1 /*
2  * Copyright (C) 2023 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.internal.property;
18 
19 import android.car.builtin.util.Slogf;
20 import android.util.Log;
21 
22 /**
23  * A logger that would use {@link Slogf} or {@link Log} depending on whether
24  * to use system logger.
25  *
26  * @hide
27  */
28 public final class Logger {
29 
30     private final boolean mUseSystemLogger;
31     private final boolean mDbg;
32     private final String mTag;
33 
Logger(boolean useSystemLogger, String tag)34     public Logger(boolean useSystemLogger, String tag) {
35         mUseSystemLogger = useSystemLogger;
36         mTag = tag;
37         if (useSystemLogger) {
38             mDbg = Slogf.isLoggable(mTag, Log.DEBUG);
39         } else {
40             mDbg = Log.isLoggable(mTag, Log.DEBUG);
41         }
42     }
43 
44     /**
45      * Logs a debug message.
46      */
logD(String msg)47     public void logD(String msg) {
48         if (mUseSystemLogger) {
49             Slogf.d(mTag, msg);
50         } else {
51             Log.d(mTag, msg);
52         }
53     }
54 
55     /**
56      * Logs a warning message.
57      */
logW(String msg)58     public void logW(String msg) {
59         if (mUseSystemLogger) {
60             Slogf.w(mTag, msg);
61         } else {
62             Log.w(mTag, msg);
63         }
64     }
65 
66     /**
67      * Whether debug logging should be enabled.
68      */
dbg()69     public boolean dbg() {
70         return mDbg;
71     }
72 }
73