blob: 0b20171043585f574f8aeec3a05d71a1994d0807 [file] [log] [blame]
Jungshik Jangdbe6b452014-08-22 13:49:55 +09001/*
2 * Copyright (C) 2014 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
17package com.android.server.hdmi;
18
Jungshik Jang339227d2014-08-25 15:37:20 +090019import android.annotation.Nullable;
Jinsuk Kim4d1189e2014-11-21 10:43:51 +090020import android.os.Build;
Jungshik Jangdbe6b452014-08-22 13:49:55 +090021import android.os.SystemClock;
22import android.util.Pair;
23import android.util.Slog;
24
25import java.util.HashMap;
26
27/**
28 * A logger that prevents spammy log. For the same log message, it logs once every 20seconds.
29 * This class is not thread-safe.
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090030 * <p>
31 * For convenience, use single character prefix for all messages.
32 * Here are common acronyms
33 * <ul>
34 * <li>[T]: Timout
35 * <li>[R]: Received message
36 * <li>[S]: Sent message
37 * <li>[P]: Device polling result
38 * </ul>
Jungshik Jangdbe6b452014-08-22 13:49:55 +090039 */
40final class HdmiLogger {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090041 private static final String TAG = "HDMI";
Jungshik Jangdbe6b452014-08-22 13:49:55 +090042 // Logging duration for same error message.
43 private static final long ERROR_LOG_DURATTION_MILLIS = 20 * 1000; // 20s
44
Jinsuk Kim4d1189e2014-11-21 10:43:51 +090045 private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
Jungshik Jangc94ac5c2014-08-27 13:48:37 +090046
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090047 private static final ThreadLocal<HdmiLogger> sLogger = new ThreadLocal<>();
48
Jungshik Jangdbe6b452014-08-22 13:49:55 +090049 // Key (String): log message.
50 // Value (Pair(Long, Integer)): a pair of last log time millis and the number of logMessage.
51 // Cache for warning.
52 private final HashMap<String, Pair<Long, Integer>> mWarningTimingCache = new HashMap<>();
53 // Cache for error.
54 private final HashMap<String, Pair<Long, Integer>> mErrorTimingCache = new HashMap<>();
Jungshik Jangdbe6b452014-08-22 13:49:55 +090055
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090056 private HdmiLogger() {
Jungshik Jangdbe6b452014-08-22 13:49:55 +090057 }
58
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090059 static final void warning(String logMessage, Object... objs) {
60 getLogger().warningInternal(toLogString(logMessage, objs));
61 }
62
63 private void warningInternal(String logMessage) {
Jungshik Jangc94ac5c2014-08-27 13:48:37 +090064 String log = updateLog(mWarningTimingCache, logMessage);
65 if (!log.isEmpty()) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090066 Slog.w(TAG, log);
Jungshik Jangdbe6b452014-08-22 13:49:55 +090067 }
68 }
69
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090070 static final void error(String logMessage, Object... objs) {
71 getLogger().errorInternal(toLogString(logMessage, objs));
72 }
73
74 private void errorInternal(String logMessage) {
Jungshik Jangc94ac5c2014-08-27 13:48:37 +090075 String log = updateLog(mErrorTimingCache, logMessage);
76 if (!log.isEmpty()) {
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090077 Slog.e(TAG, log);
Jungshik Jangdbe6b452014-08-22 13:49:55 +090078 }
79 }
80
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090081 static final void debug(String logMessage, Object... objs) {
82 getLogger().debugInternal(toLogString(logMessage, objs));
83 }
84
85 private void debugInternal(String logMessage) {
Jinsuk Kim8e609fa2014-12-08 19:30:23 +090086 if (true || IS_USER_BUILD) {
Jungshik Jangc94ac5c2014-08-27 13:48:37 +090087 return;
88 }
Jungshik Jang2e8f1b62014-09-03 08:28:02 +090089 Slog.d(TAG, logMessage);
90 }
91
92 private static final String toLogString(String logMessage, Object[] objs) {
93 if (objs.length > 0) {
94 return String.format(logMessage, objs);
95 } else {
96 return logMessage;
97 }
98 }
99
100 private static HdmiLogger getLogger() {
101 HdmiLogger logger = sLogger.get();
102 if (logger == null) {
103 logger = new HdmiLogger();
104 sLogger.set(logger);
105 }
106 return logger;
Jungshik Jangdbe6b452014-08-22 13:49:55 +0900107 }
108
Jungshik Jangc94ac5c2014-08-27 13:48:37 +0900109 private static String updateLog(HashMap<String, Pair<Long, Integer>> cache, String logMessage) {
110 long curTime = SystemClock.uptimeMillis();
111 Pair<Long, Integer> timing = cache.get(logMessage);
112 if (shouldLogNow(timing, curTime)) {
113 String log = buildMessage(logMessage, timing);
114 cache.put(logMessage, new Pair<>(curTime, 1));
115 return log;
116 } else {
117 increaseLogCount(cache, logMessage);
118 }
119 return "";
120 }
121
122 private static String buildMessage(String message, @Nullable Pair<Long, Integer> timing) {
123 return new StringBuilder()
124 .append("[").append(timing == null ? 1 : timing.second).append("]:")
125 .append(message).toString();
126 }
127
128 private static void increaseLogCount(HashMap<String, Pair<Long, Integer>> cache,
129 String message) {
Jungshik Jangdbe6b452014-08-22 13:49:55 +0900130 Pair<Long, Integer> timing = cache.get(message);
131 if (timing != null) {
132 cache.put(message, new Pair<>(timing.first, timing.second + 1));
133 }
134 }
135
Jungshik Jangc94ac5c2014-08-27 13:48:37 +0900136 private static boolean shouldLogNow(@Nullable Pair<Long, Integer> timing, long curTime) {
Jungshik Jangdbe6b452014-08-22 13:49:55 +0900137 return timing == null || curTime - timing.first > ERROR_LOG_DURATTION_MILLIS;
138 }
139}