1 /*
2  * Copyright 2021 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.build.dependencyTracker
18 
19 import java.io.File
20 import java.io.Serializable
21 import org.gradle.api.logging.LogLevel
22 import org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger
23 import org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext
24 import org.gradle.internal.time.Clock
25 
26 /** Gradle logger that logs to a file */
27 class FileLogger(val file: File) : Serializable {
28     @Transient var impl: OutputEventListenerBackedLogger? = null
29 
toLoggernull30     fun toLogger(): OutputEventListenerBackedLogger {
31         if (impl == null) {
32             impl =
33                 OutputEventListenerBackedLogger(
34                     "my_logger",
35                     OutputEventListenerBackedLoggerContext(Clock { System.currentTimeMillis() })
36                         .also {
37                             it.level = LogLevel.DEBUG
38                             it.setOutputEventListener { file.appendText(it.toString() + "\n") }
39                         },
40                     Clock { System.currentTimeMillis() }
41                 )
42         }
43         return impl!!
44     }
45 
lifecyclenull46     fun lifecycle(text: String) {
47         toLogger().lifecycle(text)
48     }
49 
infonull50     fun info(text: String) {
51         toLogger().info(text)
52     }
53 }
54