1 /* 2 * 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 package com.android.adservices.shared.testing; 17 18 import com.google.errorprone.annotations.FormatMethod; 19 import com.google.errorprone.annotations.FormatString; 20 21 import java.util.Objects; 22 23 /** Helper class providing convenience methods to log a message. */ 24 public final class Logger { 25 26 private final RealLogger mRealLogger; 27 private final String mTag; 28 Logger(RealLogger realLogger, Class<?> clazz)29 public Logger(RealLogger realLogger, Class<?> clazz) { 30 this(realLogger, Objects.requireNonNull(clazz).getSimpleName()); 31 } 32 Logger(RealLogger realLogger, String tag)33 public Logger(RealLogger realLogger, String tag) { 34 mRealLogger = Objects.requireNonNull(realLogger); 35 mTag = Objects.requireNonNull(tag); 36 } 37 getTag()38 public String getTag() { 39 return mTag; 40 } 41 42 /** Convenience method to log a WTF message. */ 43 @FormatMethod wtf(@ormatString String msgFmt, @Nullable Object... msgArgs)44 public void wtf(@FormatString String msgFmt, @Nullable Object... msgArgs) { 45 log(LogLevel.WTF, msgFmt, msgArgs); 46 } 47 48 /** Convenience method to log a WTF message with an exception. */ 49 @FormatMethod wtf(Throwable t, @FormatString String msgFmt, @Nullable Object... msgArgs)50 public void wtf(Throwable t, @FormatString String msgFmt, @Nullable Object... msgArgs) { 51 log(LogLevel.WTF, t, msgFmt, msgArgs); 52 } 53 54 /** Convenience method to log an error message. */ 55 @FormatMethod e(@ormatString String msgFmt, @Nullable Object... msgArgs)56 public void e(@FormatString String msgFmt, @Nullable Object... msgArgs) { 57 log(LogLevel.ERROR, msgFmt, msgArgs); 58 } 59 60 /** Convenience method to log an error message with an exception. */ 61 @FormatMethod e(Throwable t, @FormatString String msgFmt, @Nullable Object... msgArgs)62 public void e(Throwable t, @FormatString String msgFmt, @Nullable Object... msgArgs) { 63 log(LogLevel.ERROR, t, msgFmt, msgArgs); 64 } 65 66 /** Convenience method to log a warning message. */ 67 @FormatMethod w(@ormatString String msgFmt, @Nullable Object... msgArgs)68 public void w(@FormatString String msgFmt, @Nullable Object... msgArgs) { 69 log(LogLevel.WARNING, msgFmt, msgArgs); 70 } 71 72 /** Convenience method to log a warning message with an exception. */ 73 @FormatMethod w(Throwable t, @FormatString String msgFmt, @Nullable Object... msgArgs)74 public void w(Throwable t, @FormatString String msgFmt, @Nullable Object... msgArgs) { 75 log(LogLevel.WARNING, t, msgFmt, msgArgs); 76 } 77 78 /** Convenience method to log a info message. */ 79 @FormatMethod i(@ormatString String msgFmt, @Nullable Object... msgArgs)80 public void i(@FormatString String msgFmt, @Nullable Object... msgArgs) { 81 log(LogLevel.INFO, msgFmt, msgArgs); 82 } 83 84 /** Convenience method to log a debug message. */ 85 @FormatMethod d(@ormatString String msgFmt, @Nullable Object... msgArgs)86 public void d(@FormatString String msgFmt, @Nullable Object... msgArgs) { 87 log(LogLevel.DEBUG, msgFmt, msgArgs); 88 } 89 90 /** Convenience method to log a verbose message. */ 91 @FormatMethod v(@ormatString String msgFmt, @Nullable Object... msgArgs)92 public void v(@FormatString String msgFmt, @Nullable Object... msgArgs) { 93 log(LogLevel.VERBOSE, msgFmt, msgArgs); 94 } 95 96 /** Logs a message in the given level. */ 97 @FormatMethod log(LogLevel level, @FormatString String msgFmt, @Nullable Object... msgArgs)98 void log(LogLevel level, @FormatString String msgFmt, @Nullable Object... msgArgs) { 99 mRealLogger.log(level, mTag, msgFmt, msgArgs); 100 } 101 102 /** Logs a message (and an exception) in the given level. */ 103 @FormatMethod log( LogLevel level, Throwable t, @FormatString String msgFmt, @Nullable Object... msgArgs)104 void log( 105 LogLevel level, Throwable t, @FormatString String msgFmt, @Nullable Object... msgArgs) { 106 mRealLogger.log(level, mTag, t, msgFmt, msgArgs); 107 } 108 109 @Override toString()110 public String toString() { 111 return "Logger[realLogger=" + mRealLogger + ", tag=" + mTag + "]"; 112 } 113 114 // TODO(b/324919960): make it package-protected again or make sure it's unit tested. 115 /** Level of log messages. */ 116 public enum LogLevel { 117 WTF, 118 ERROR, 119 WARNING, 120 INFO, 121 DEBUG, 122 VERBOSE 123 } 124 125 /** Low-level implementation of the logger */ 126 public interface RealLogger { 127 128 /** Logs a message in the given level. */ 129 @FormatMethod log( LogLevel level, String tag, @FormatString String msgFmt, @Nullable Object... msgArgs)130 void log( 131 LogLevel level, 132 String tag, 133 @FormatString String msgFmt, 134 @Nullable Object... msgArgs); 135 136 /** Logs a message (with an exception) in the given level. */ 137 @FormatMethod log( LogLevel level, String tag, Throwable throwable, @FormatString String msgFmt, @Nullable Object... msgArgs)138 void log( 139 LogLevel level, 140 String tag, 141 Throwable throwable, 142 @FormatString String msgFmt, 143 @Nullable Object... msgArgs); 144 } 145 } 146