Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 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 | |
| 17 | package com.android.server.hdmi; |
| 18 | |
Jungshik Jang | 339227d | 2014-08-25 15:37:20 +0900 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 20 | import android.os.SystemClock; |
| 21 | import android.util.Pair; |
| 22 | import android.util.Slog; |
| 23 | |
| 24 | import 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 | */ |
| 30 | final class HdmiLogger { |
| 31 | // Logging duration for same error message. |
| 32 | private static final long ERROR_LOG_DURATTION_MILLIS = 20 * 1000; // 20s |
| 33 | |
Yuncheol Heo | 192ba20 | 2014-09-02 14:59:15 +0900 | [diff] [blame] | 34 | private static final boolean DEBUG = false; |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 35 | |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 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<>(); |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 42 | private final String mTag; |
| 43 | |
| 44 | HdmiLogger(String tag) { |
Jungshik Jang | a7221ce | 2014-08-28 16:35:30 +0900 | [diff] [blame] | 45 | mTag = "HDMI:" + tag; |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void warning(String logMessage) { |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 49 | String log = updateLog(mWarningTimingCache, logMessage); |
| 50 | if (!log.isEmpty()) { |
| 51 | Slog.w(mTag, log); |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | void error(String logMessage) { |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 56 | String log = updateLog(mErrorTimingCache, logMessage); |
| 57 | if (!log.isEmpty()) { |
| 58 | Slog.e(mTag, log); |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 62 | void debug(String logMessage) { |
| 63 | if (!DEBUG) { |
| 64 | return; |
| 65 | } |
| 66 | Slog.d(mTag, logMessage); |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 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) { |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 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 | |
Jungshik Jang | c94ac5c | 2014-08-27 13:48:37 +0900 | [diff] [blame] | 96 | private static boolean shouldLogNow(@Nullable Pair<Long, Integer> timing, long curTime) { |
Jungshik Jang | dbe6b45 | 2014-08-22 13:49:55 +0900 | [diff] [blame] | 97 | return timing == null || curTime - timing.first > ERROR_LOG_DURATTION_MILLIS; |
| 98 | } |
| 99 | } |