1 /* <lambda>null2 * Copyright (C) 2016 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 @file:Suppress("unused") 18 19 package androidx.room.log 20 21 import androidx.room.vo.Warning 22 import java.util.UnknownFormatConversionException 23 import javax.annotation.processing.ProcessingEnvironment 24 import javax.lang.model.element.Element 25 import javax.tools.Diagnostic 26 import javax.tools.Diagnostic.Kind.ERROR 27 import javax.tools.Diagnostic.Kind.NOTE 28 import javax.tools.Diagnostic.Kind.WARNING 29 30 class RLog(val messager: Messager, val suppressedWarnings: Set<Warning>, 31 val defaultElement: Element?) { 32 private fun String.safeFormat(vararg args: Any): String { 33 try { 34 return format(args) 35 } catch (ex: UnknownFormatConversionException) { 36 // the input string might be from random source in which case we rather print the 37 // msg as is instead of crashing while reporting an error. 38 return this 39 } 40 } 41 42 fun d(element: Element, msg: String, vararg args: Any) { 43 messager.printMessage(NOTE, msg.safeFormat(args), element) 44 } 45 46 fun d(msg: String, vararg args: Any) { 47 messager.printMessage(NOTE, msg.safeFormat(args)) 48 } 49 50 fun e(element: Element, msg: String, vararg args: Any) { 51 messager.printMessage(ERROR, msg.safeFormat(args), element) 52 } 53 54 fun e(msg: String, vararg args: Any) { 55 messager.printMessage(ERROR, msg.safeFormat(args), defaultElement) 56 } 57 58 fun w(warning: Warning, element: Element? = null, msg: String, vararg args: Any) { 59 if (suppressedWarnings.contains(warning)) { 60 return 61 } 62 messager.printMessage(WARNING, msg.safeFormat(args), 63 element ?: defaultElement) 64 } 65 66 fun w(warning: Warning, msg: String, vararg args: Any) { 67 if (suppressedWarnings.contains(warning)) { 68 return 69 } 70 messager.printMessage(WARNING, msg.safeFormat(args), defaultElement) 71 } 72 73 interface Messager { 74 fun printMessage(kind: Diagnostic.Kind, msg: String, element: Element? = null) 75 } 76 77 class ProcessingEnvMessager(val processingEnv: ProcessingEnvironment) : Messager { 78 override fun printMessage(kind: Diagnostic.Kind, msg: String, element: Element?) { 79 processingEnv.messager.printMessage(kind, msg, element) 80 } 81 } 82 83 class CollectingMessager : Messager { 84 private val messages = mutableMapOf<Diagnostic.Kind, MutableList<Pair<String, Element?>>> () 85 override fun printMessage(kind: Diagnostic.Kind, msg: String, element: Element?) { 86 messages.getOrPut(kind, { 87 arrayListOf() 88 }).add(Pair(msg, element)) 89 } 90 91 fun hasErrors() = messages.containsKey(Diagnostic.Kind.ERROR) 92 93 fun writeTo(env: ProcessingEnvironment) { 94 messages.forEach { pair -> 95 val kind = pair.key 96 pair.value.forEach { 97 env.messager.printMessage(kind, it.first, it.second) 98 } 99 } 100 } 101 } 102 } 103