1 /*
2  * Copyright 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 
17 package androidx.xr.compose.platform
18 
19 import android.content.Context
20 import android.content.pm.PackageManager
21 import android.util.Log
22 import androidx.annotation.RestrictTo
23 
24 /**
25  * Logger for Subspace Compose. This is a simple wrapper around Log.v that is only enabled if the
26  * androidx.xr.compose.platform.Logger metadata flag is set in the AndroidManifest.xml.
27  */
28 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
29 public object Logger {
30     private const val METADATA_KEY = "androidx.xr.compose.platform.Logger"
31     private var isDebug: Boolean = false
32 
33     /**
34      * Initializes the logger. This method expects
35      * android:name="androidx.xr.compose.platform.Logger" to be set in a metadata tag in the
36      * AndroidManifest.xml. Typically the value of this key is set to false but can be set to true
37      * to enable debug logging. For example:
38      *
39      * <meta-data android:name="androidx.xr.compose.platform.Logger" android:value="true" />
40      *
41      * @param context The application context for the current application.
42      */
initnull43     public fun init(context: Context) {
44         isDebug =
45             context.packageManager
46                 .getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
47                 .metaData
48                 .getBoolean(METADATA_KEY, false)
49     }
50 
51     /**
52      * Logs a message to the logcat if debugging was enabled in the init method.
53      *
54      * @param tag The tag to use for the log message.
55      * @param messageGenerator This generates the message to log.
56      */
lognull57     public fun log(tag: String, messageGenerator: () -> String) {
58         if (isDebug) {
59             Log.v(tag, messageGenerator())
60         }
61     }
62 }
63