HdmiLogger.java revision a7221ce87683fab16603290378408ce92f02e88a
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 private static final boolean DEBUG = false; 35 36 // Key (String): log message. 37 // Value (Pair(Long, Integer)): a pair of last log time millis and the number of logMessage. 38 // Cache for warning. 39 private final HashMap<String, Pair<Long, Integer>> mWarningTimingCache = new HashMap<>(); 40 // Cache for error. 41 private final HashMap<String, Pair<Long, Integer>> mErrorTimingCache = new HashMap<>(); 42 private final String mTag; 43 44 HdmiLogger(String tag) { 45 mTag = "HDMI:" + tag; 46 } 47 48 void warning(String logMessage) { 49 String log = updateLog(mWarningTimingCache, logMessage); 50 if (!log.isEmpty()) { 51 Slog.w(mTag, log); 52 } 53 } 54 55 void error(String logMessage) { 56 String log = updateLog(mErrorTimingCache, logMessage); 57 if (!log.isEmpty()) { 58 Slog.e(mTag, log); 59 } 60 } 61 62 void debug(String logMessage) { 63 if (!DEBUG) { 64 return; 65 } 66 Slog.d(mTag, logMessage); 67 } 68 69 private static String updateLog(HashMap<String, Pair<Long, Integer>> cache, String logMessage) { 70 long curTime = SystemClock.uptimeMillis(); 71 Pair<Long, Integer> timing = cache.get(logMessage); 72 if (shouldLogNow(timing, curTime)) { 73 String log = buildMessage(logMessage, timing); 74 cache.put(logMessage, new Pair<>(curTime, 1)); 75 return log; 76 } else { 77 increaseLogCount(cache, logMessage); 78 } 79 return ""; 80 } 81 82 private static String buildMessage(String message, @Nullable Pair<Long, Integer> timing) { 83 return new StringBuilder() 84 .append("[").append(timing == null ? 1 : timing.second).append("]:") 85 .append(message).toString(); 86 } 87 88 private static void increaseLogCount(HashMap<String, Pair<Long, Integer>> cache, 89 String message) { 90 Pair<Long, Integer> timing = cache.get(message); 91 if (timing != null) { 92 cache.put(message, new Pair<>(timing.first, timing.second + 1)); 93 } 94 } 95 96 private static boolean shouldLogNow(@Nullable Pair<Long, Integer> timing, long curTime) { 97 return timing == null || curTime - timing.first > ERROR_LOG_DURATTION_MILLIS; 98 } 99} 100