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 package com.android.server.bluetooth
17 
18 import android.util.Log
19 import java.time.Instant
20 import java.time.ZoneId
21 import java.time.format.DateTimeFormatter
22 
23 private const val SYSTEM_SERVER_TAG = "BluetoothSystemServer"
24 
25 object Log {
26 
27     // Kotlin could shorten below method by having a Throwable? that is default to null but the
28     // current implementation of util.Log is behaving differently depending if it is called with
29     // 2 or 3 parameters. We do not want to change the behavior in this class, just add a common
30     // TAG to all the Bluetooth System Server logs.
31 
vnull32     @JvmStatic fun v(subtag: String, msg: String) = Log.v(SYSTEM_SERVER_TAG, "${subtag}: ${msg}")
33 
34     @JvmStatic fun d(subtag: String, msg: String) = Log.d(SYSTEM_SERVER_TAG, "${subtag}: ${msg}")
35 
36     @JvmStatic fun i(subtag: String, msg: String) = Log.i(SYSTEM_SERVER_TAG, "${subtag}: ${msg}")
37 
38     @JvmStatic fun w(subtag: String, msg: String) = Log.w(SYSTEM_SERVER_TAG, "${subtag}: ${msg}")
39 
40     @JvmStatic
41     fun w(subtag: String, msg: String, tr: Throwable) =
42         Log.w(SYSTEM_SERVER_TAG, "${subtag}: ${msg}", tr)
43 
44     @JvmStatic fun e(subtag: String, msg: String) = Log.e(SYSTEM_SERVER_TAG, "${subtag}: ${msg}")
45 
46     @JvmStatic
47     fun e(subtag: String, msg: String, tr: Throwable) =
48         Log.e(SYSTEM_SERVER_TAG, "${subtag}: ${msg}", tr)
49 
50     @JvmStatic
51     fun timeToStringWithZone(timestamp: Long) =
52         DateTimeFormatter.ofPattern("MM-dd HH:mm:ss.SSS")
53             .withZone(ZoneId.systemDefault())
54             .format(Instant.ofEpochMilli(timestamp))
55 }
56