xref: /aosp_15_r20/external/leakcanary2/shark-log/src/main/java/shark/SharkLog.kt (revision d9e8da70d8c9df9a41d7848ae506fb3115cae6e6)
1 package shark
2 
3 /**
4  * Central Logger for all Shark artifacts. Set [logger] to change where these logs go.
5  */
6 object SharkLog {
7 
8   /**
9    * @see SharkLog
10    */
11   interface Logger {
12 
13     /**
14      * Logs a debug message formatted with the passed in arguments.
15      */
dnull16     fun d(message: String)
17 
18     /**
19      * Logs a [Throwable] and debug message formatted with the passed in arguments.
20      */
21     fun d(
22       throwable: Throwable,
23       message: String
24     )
25   }
26 
27   @Volatile var logger: Logger? = null
28 
29   /**
30    * @see Logger.d
31    */
32   inline fun d(message: () -> String) {
33     // Local variable to prevent the ref from becoming null after the null check.
34     val logger = logger ?: return
35     logger.d(message.invoke())
36   }
37 
38   /**
39    * @see Logger.d
40    */
dnull41   inline fun d(
42     throwable: Throwable,
43     message: () -> String
44   ) {
45     // Local variable to prevent the ref from becoming null after the null check.
46     val logger = logger ?: return
47     logger.d(throwable, message.invoke())
48   }
49 }
50