1 /* <lambda>null2 * 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 17 package android.tools.common 18 19 class LoggerBuilder { 20 private var onV: (tag: String, msg: String) -> Unit = { tag, msg -> println("(V) $tag $msg") } 21 private var onD: (tag: String, msg: String) -> Unit = { tag, msg -> println("(D) $tag $msg") } 22 private var onI: (tag: String, msg: String) -> Unit = { tag, msg -> println("(I) $tag $msg") } 23 private var onW: (tag: String, msg: String) -> Unit = { tag, msg -> println("(W) $tag $msg") } 24 private var onE: (tag: String, msg: String, error: Throwable?) -> Unit = { tag, msg, error -> 25 println("(e) $tag $msg $error") 26 error?.printStackTrace() 27 } 28 private var onTracing: (name: String, predicate: () -> Any) -> Any = { name, predicate -> 29 try { 30 println("(withTracing#start) $name") 31 predicate() 32 } finally { 33 println("(withTracing#end) $name") 34 } 35 } 36 37 fun setV(predicate: (tag: String, msg: String) -> Unit): LoggerBuilder = apply { 38 onV = predicate 39 } 40 41 fun setD(predicate: (tag: String, msg: String) -> Unit): LoggerBuilder = apply { 42 onD = predicate 43 } 44 45 fun setI(predicate: (tag: String, msg: String) -> Unit): LoggerBuilder = apply { 46 onI = predicate 47 } 48 49 fun setW(predicate: (tag: String, msg: String) -> Unit): LoggerBuilder = apply { 50 onW = predicate 51 } 52 53 fun setE(predicate: (tag: String, msg: String, error: Throwable?) -> Unit): LoggerBuilder = 54 apply { 55 onE = predicate 56 } 57 58 fun setOnTracing(predicate: (name: String, predicate: () -> Any) -> Any): LoggerBuilder = 59 apply { 60 onTracing = predicate 61 } 62 63 fun build(): ILogger { 64 return object : ILogger { 65 override fun d(tag: String, msg: String) = onD(tag, msg) 66 67 override fun e(tag: String, msg: String, error: Throwable?) = onE(tag, msg, error) 68 69 override fun i(tag: String, msg: String) = onI(tag, msg) 70 71 override fun v(tag: String, msg: String) = onV(tag, msg) 72 73 override fun w(tag: String, msg: String) = onW(tag, msg) 74 75 override fun <T> withTracing(name: String, predicate: () -> T): T { 76 return onTracing(name, predicate as () -> Any) as T 77 } 78 } 79 } 80 } 81