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