diff --git a/utils/include/window_manager_hilog.h b/utils/include/window_manager_hilog.h index e4637401d2..fae59d7eec 100644 --- a/utils/include/window_manager_hilog.h +++ b/utils/include/window_manager_hilog.h @@ -16,6 +16,7 @@ #ifndef OHOS_WM_INCLUDE_WINDOW_MANAGER_HILOG_H #define OHOS_WM_INCLUDE_WINDOW_MANAGER_HILOG_H +#include #include #include #include "hilog/log.h" @@ -80,6 +81,45 @@ enum class WmsLogTag : uint8_t { }; extern const char* g_domainContents[static_cast(WmsLogTag::END)]; + +struct TLogInfo { + uint32_t domain; + const char* content; +}; + +TLogInfo GetTLogInfo(WmsLogTag tag); + +struct WinPrintLimitState { + std::chrono::time_point last{}; + uint32_t supressed = 0; + int printCount = 0; +}; + +struct WinPrintLimitConfig { + WmsLogTag logTag; + LogLevel logLevel; + uint32_t timeIntervals; + uint32_t printFrequency; + const char* functionName; + + WinPrintLimitConfig() + : logTag(WmsLogTag::DEFAULT), + logLevel(LOG_INFO), + timeIntervals(WIN_LOG_LIMIT_MINUTE), + printFrequency(TEN_TIMES), + functionName("") {} + + WinPrintLimitConfig(WmsLogTag tag, LogLevel level, uint32_t intervals, + uint32_t frequency, const char* funcName) + : logTag(tag), + logLevel(level), + timeIntervals(intervals), + printFrequency(frequency), + functionName(funcName) {} +}; + +bool WinPrintLimit(const WinPrintLimitConfig& config, WinPrintLimitState& state); + #ifdef IS_RELEASE_VERSION #define WMS_FILE_NAME "" #define FMT_PREFIX "%{public}s%{public}s: " @@ -92,12 +132,10 @@ extern const char* g_domainContents[static_cast(WmsLogTag::END)]; #endif #define WMS_NO_FILE_NAME "" -#define PRINT_TLOG(level, tag, ...) \ - do { \ - uint32_t hilogDomain = HILOG_DOMAIN_WINDOW + static_cast(tag); \ - const char *domainContent = (tag >= WmsLogTag::DEFAULT && tag < WmsLogTag::END) ? \ - g_domainContents[static_cast(tag)] : ""; \ - HILOG_IMPL(LOG_CORE, level, hilogDomain, domainContent, ##__VA_ARGS__); \ +#define PRINT_TLOG(level, tag, fmt, ...) \ + do { \ + auto tlogInfo_ = GetTLogInfo(tag); \ + HiLogPrint(LOG_CORE, level, tlogInfo_.domain, tlogInfo_.content, fmt, ##__VA_ARGS__); \ } while (0) #define TLOGD(tag, fmt, ...) \ @@ -131,34 +169,11 @@ PRINT_TLOG(LOG_WARN, tag, FMT_PREFIX fmt, WMS_NO_FILE_NAME, C_W_FUNC, ##__VA_ARG #define TLOGNFE(tag, fmt, ...) \ PRINT_TLOG(LOG_ERROR, tag, FMT_PREFIX fmt, WMS_NO_FILE_NAME, C_W_FUNC, ##__VA_ARGS__) -#define WIN_PRINT_LIMIT(tag, level, intervals, canPrint, frequency) \ - do { \ - uint32_t hilogDomain = HILOG_DOMAIN_WINDOW + static_cast(tag); \ - const char *domainContent = ((tag) >= WmsLogTag::DEFAULT && (tag) < WmsLogTag::END) ? \ - g_domainContents[static_cast(tag)] : ""; \ - static auto last = std::chrono::time_point(); \ - static uint32_t supressed = 0; \ - static int printCount = 0; \ - auto now = std::chrono::time_point_cast(std::chrono::system_clock::now()); \ - auto duration = now - last; \ - if (duration.count() >= (intervals)) { \ - last = now; \ - uint32_t supressedCnt = supressed; \ - supressed = 0; \ - printCount = 1; \ - if (supressedCnt != 0) { \ - ((void)HILOG_IMPL(LOG_CORE, (level), hilogDomain, domainContent, \ - "%{public}s log suppressed cnt %{public}u", __func__, supressedCnt)); \ - } \ - (canPrint) = true; \ - } else { \ - if ((printCount++) < (frequency)) { \ - (canPrint) = true; \ - } else { \ - supressed++; \ - (canPrint) = false; \ - } \ - } \ +#define WIN_PRINT_LIMIT(tag, level, intervals, canPrint, frequency) \ + do { \ + static WinPrintLimitState state; \ + WinPrintLimitConfig config(tag, level, intervals, frequency, __func__); \ + (canPrint) = WinPrintLimit(config, state); \ } while (0) #define TLOGI_LIMITN_HOUR(tag, freq, fmt, ...) \ diff --git a/utils/src/window_manager_hilog.cpp b/utils/src/window_manager_hilog.cpp index 15ce7a9ddb..1801ec22f5 100644 --- a/utils/src/window_manager_hilog.cpp +++ b/utils/src/window_manager_hilog.cpp @@ -14,6 +14,7 @@ */ #include "window_manager_hilog.h" +#include namespace OHOS { namespace Rosen { @@ -50,5 +51,38 @@ const char* g_domainContents[static_cast(WmsLogTag::END)] = { "WMSRotation", "WMSAnimation", }; + +TLogInfo GetTLogInfo(WmsLogTag tag) +{ + uint32_t domain = HILOG_DOMAIN_WINDOW + static_cast(tag); + const char* content = (tag >= WmsLogTag::DEFAULT && tag < WmsLogTag::END) ? + g_domainContents[static_cast(tag)] : ""; + return {domain, content}; +} + +bool WinPrintLimit(const WinPrintLimitConfig& config, WinPrintLimitState& state) +{ + auto info = GetTLogInfo(config.logTag); + auto now = std::chrono::time_point_cast(std::chrono::system_clock::now()); + auto duration = now - state.last; + if (duration.count() >= config.timeIntervals) { + state.last = now; + uint32_t supressedCnt = state.supressed; + state.supressed = 0; + state.printCount = 1; + if (supressedCnt != 0) { + HiLogPrint(LOG_CORE, config.logLevel, info.domain, info.content, + "%{public}s log suppressed cnt %{public}u", config.functionName, supressedCnt); + } + return true; + } else { + if (state.printCount++ < config.printFrequency) { + return true; + } else { + state.supressed++; + return false; + } + } +} } // namespace OHOS } \ No newline at end of file