1 /*
2  * Copyright (C) 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 package com.android.adservices.shared.meta_testing;
17 
18 import com.android.adservices.shared.testing.DynamicLogger;
19 import com.android.adservices.shared.testing.LogEntry;
20 import com.android.adservices.shared.testing.Logger;
21 import com.android.adservices.shared.testing.Logger.LogLevel;
22 import com.android.adservices.shared.testing.Logger.RealLogger;
23 import com.android.adservices.shared.testing.Nullable;
24 
25 import com.google.common.collect.ImmutableList;
26 import com.google.errorprone.annotations.FormatMethod;
27 import com.google.errorprone.annotations.FormatString;
28 
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * Simple implementation of {@link RealLogger} that stores log calls for further assertions (they
34  * can be obtained by {@link #getEntries()}.
35  */
36 public final class FakeLogger implements RealLogger {
37 
38     private final List<LogEntry> mEntries = new ArrayList<>();
39 
40     private static final Logger sRealLogger =
41             new Logger(DynamicLogger.getInstance(), FakeLogger.class);
42 
43     @Override
44     @FormatMethod
log(LogLevel level, String tag, @FormatString String msgFmt, Object... msgArgs)45     public void log(LogLevel level, String tag, @FormatString String msgFmt, Object... msgArgs) {
46         addEntry(level, tag, /* throwable= */ null, msgFmt, msgArgs);
47     }
48 
49     @Override
50     @FormatMethod
log( LogLevel level, String tag, Throwable throwable, @FormatString String msgFmt, Object... msgArgs)51     public void log(
52             LogLevel level,
53             String tag,
54             Throwable throwable,
55             @FormatString String msgFmt,
56             Object... msgArgs) {
57         addEntry(level, tag, throwable, msgFmt, msgArgs);
58     }
59 
60     /** Gets all logged entries. */
getEntries()61     public ImmutableList<LogEntry> getEntries() {
62         return ImmutableList.copyOf(mEntries);
63     }
64 
65     @Override
toString()66     public String toString() {
67         return "[" + FakeLogger.class.getSimpleName() + ": " + mEntries.size() + " entries]";
68     }
69 
70     @FormatMethod
addEntry( LogLevel level, String tag, Throwable throwable, @FormatString String msgFmt, @Nullable Object... msgArgs)71     private void addEntry(
72             LogLevel level,
73             String tag,
74             Throwable throwable,
75             @FormatString String msgFmt,
76             @Nullable Object... msgArgs) {
77         String message = String.format(msgFmt, msgArgs);
78         LogEntry logEntry = new LogEntry(level, tag, message, throwable);
79 
80         // Also log "for real"
81         sRealLogger.v("Adding entry: %s", logEntry);
82         mEntries.add(logEntry);
83     }
84 }
85