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.baselineprofile.gradle.utils
18 
19 import org.gradle.api.logging.Logger
20 
21 internal class BaselineProfilePluginLogger(private val logger: Logger) {
22 
23     private var warnings =
<lambda>null24         Warnings().apply {
25             // Note that this is a shared warning across all the plugins.
26             // By setting it by default `false` here, it will be disabled in all the plugins.
27             // Single plugins can re-enable it using the default `Warnings` configuration or the
28             // user specified one through #setWarnings. Currently only the consumer plugin supports
29             // warnings because it's the only plugin that prints any, beside this shared max agp
30             // version warning.
31             maxAgpVersion = false
32         }
33 
34     private var suppressAllWarnings: Boolean = false
35 
setWarningsnull36     fun setWarnings(warnings: Warnings) {
37         this.warnings = warnings
38     }
39 
suppressAllWarningsnull40     fun suppressAllWarnings() {
41         suppressAllWarnings = true
42     }
43 
debugnull44     fun debug(message: String) = logger.debug(message)
45 
46     fun info(message: String) = logger.info(message)
47 
48     fun warn(property: Warnings.() -> (Boolean), propertyName: String?, message: String) {
49         if (suppressAllWarnings) return
50         if (property(warnings)) {
51             logger.warn(message)
52             if (propertyName != null) {
53                 logger.warn(
54                     """
55 
56                 This warning can be disabled setting the following property:
57                 baselineProfile {
58                     warnings {
59                         $propertyName = false
60                     }
61                 }
62             """
63                         .trimIndent()
64                 )
65             }
66         }
67     }
68 
errornull69     fun error(message: String) = logger.error(message)
70 }
71