HdmiLogger.java revision 339227da7cf025ce4ae0c85ddc52643d63972321
1/*
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
19import android.annotation.Nullable;
20import android.os.SystemClock;
21import android.util.Pair;
22import android.util.Slog;
23
24import java.util.HashMap;
25
26/**
27 * A logger that prevents spammy log. For the same log message, it logs once every 20seconds.
28 * This class is not thread-safe.
29 */
30final class HdmiLogger {
31    // Logging duration for same error message.
32    private static final long ERROR_LOG_DURATTION_MILLIS = 20 * 1000;  // 20s
33
34    // Key (String): log message.
35    // Value (Pair(Long, Integer)): a pair of last log time millis and the number of logMessage.
36    // Cache for warning.
37    private final HashMap<String, Pair<Long, Integer>> mWarningTimingCache = new HashMap<>();
38    // Cache for error.
39    private final HashMap<String, Pair<Long, Integer>> mErrorTimingCache = new HashMap<>();
40
41    private final String mTag;
42
43    HdmiLogger(String tag) {
44        mTag = tag;
45    }
46
47    void warning(String logMessage) {
48        long curTime = SystemClock.uptimeMillis();
49        Pair<Long, Integer> timing = mWarningTimingCache.get(logMessage);
50        if (shouldLogNow(timing, curTime)) {
51            Slog.w(mTag, buildMessage(logMessage, timing));
52            mWarningTimingCache.put(logMessage, new Pair<>(curTime, 1));
53        } else {
54            increaseLogCount(mWarningTimingCache, logMessage);
55        }
56    }
57
58    void error(String logMessage) {
59        long curTime = SystemClock.uptimeMillis();
60        Pair<Long, Integer> timing = mErrorTimingCache.get(logMessage);
61        if (shouldLogNow(timing, curTime)) {
62            Slog.e(mTag, buildMessage(logMessage, timing));
63            mErrorTimingCache.put(logMessage, new Pair<>(curTime, 1));
64        } else {
65            increaseLogCount(mErrorTimingCache, logMessage);
66        }
67    }
68
69    private String buildMessage(String message, @Nullable Pair<Long, Integer> timing) {
70        return new StringBuilder()
71            .append("[").append(timing == null ? 1 : timing.second).append("]:")
72            .append(message).toString();
73    }
74
75    private void increaseLogCount(HashMap<String, Pair<Long, Integer>> cache, String message) {
76        Pair<Long, Integer> timing = cache.get(message);
77        if (timing != null) {
78            cache.put(message, new Pair<>(timing.first, timing.second + 1));
79        }
80    }
81
82    private boolean shouldLogNow(@Nullable Pair<Long, Integer> timing, long curTime) {
83        return timing == null || curTime - timing.first > ERROR_LOG_DURATTION_MILLIS;
84    }
85}
86