1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_LOGGER_H 17 #define BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_LOGGER_H 18 19 #include <cinttypes> 20 #include <functional> 21 #include <future> 22 #include <string> 23 #include <sstream> 24 25 #include "hilog/log.h" 26 27 namespace OHOS { 28 namespace AppExecFwk { 29 inline constexpr uint32_t EH_LOG_DOMAIN = 0xD001201; 30 #define EH_LOG_LIMIT_INTERVALS 10000 //ms 31 } // namespace AppExecFwk 32 } // namespace OHOS 33 34 #ifndef EH_FUNC_FMT 35 #define EH_FUNC_FMT "in %{public}s:%{public}d, " 36 #endif 37 38 #ifndef EH_FUNC_INFO 39 #define EH_FUNC_INFO __FUNCTION__, __LINE__ 40 #endif 41 42 #ifndef EH_FILE_NAME 43 #define EH_FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) 44 #endif 45 46 #ifndef EH_LINE_INFO 47 #define EH_LINE_INFO EH_FILE_NAME, __LINE__ 48 #endif 49 50 #define DEFINE_EH_HILOG_LABEL(name) \ 51 static const constexpr char* EH_LOG_LABEL = (name) 52 53 #define HILOGD(fmt, ...) do { \ 54 if (HiLogIsLoggable(OHOS::AppExecFwk::EH_LOG_DOMAIN, EH_LOG_LABEL, LOG_DEBUG)) { \ 55 ((void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, EH_LOG_DOMAIN, EH_LOG_LABEL, fmt, ##__VA_ARGS__)); \ 56 } \ 57 } while (0) 58 59 #define HILOGI(fmt, ...) do { \ 60 ((void)HILOG_IMPL(LOG_CORE, LOG_INFO, EH_LOG_DOMAIN, EH_LOG_LABEL, fmt, ##__VA_ARGS__)); \ 61 } while (0) 62 63 #define HILOGW(fmt, ...) do { \ 64 ((void)HILOG_IMPL(LOG_CORE, LOG_WARN, EH_LOG_DOMAIN, EH_LOG_LABEL, fmt, ##__VA_ARGS__)); \ 65 } while (0) 66 67 #define HILOGE(fmt, ...) do { \ 68 ((void)HILOG_IMPL(LOG_CORE, LOG_ERROR, EH_LOG_DOMAIN, EH_LOG_LABEL, fmt, ##__VA_ARGS__)); \ 69 } while (0) 70 71 #define HILOGF(fmt, ...) do { \ 72 ((void)HILOG_IMPL(LOG_CORE, LOG_FATAL, EH_LOG_DOMAIN, EH_LOG_LABEL, fmt, ##__VA_ARGS__)); \ 73 } while (0) 74 75 #define EH_PRINT_LIMIT(type, level, intervals, canPrint) \ 76 do { \ 77 static auto last = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(); \ 78 static uint32_t supressed = 0; \ 79 auto now = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()); \ 80 auto duration = now - last; \ 81 if (duration.count() >= (intervals)) { \ 82 last = now; \ 83 supressed = 0; \ 84 (canPrint) = true; \ 85 } else { \ 86 supressed++; \ 87 (canPrint) = false; \ 88 } \ 89 } while (0) 90 91 #define EH_LOGF_LIMIT(fmt, ...) \ 92 do { \ 93 bool can = true; \ 94 EH_PRINT_LIMIT(LOG_CORE, LOG_FATAL, EH_LOG_LIMIT_INTERVALS, can); \ 95 if (can) { \ 96 HILOGF(fmt, ##__VA_ARGS__); \ 97 } \ 98 } while (0) 99 100 #define EH_LOGE_LIMIT(fmt, ...) \ 101 do { \ 102 bool can = true; \ 103 EH_PRINT_LIMIT(LOG_CORE, LOG_ERROR, EH_LOG_LIMIT_INTERVALS, can); \ 104 if (can) { \ 105 HILOGE(fmt, ##__VA_ARGS__); \ 106 } \ 107 } while (0) 108 109 #define EH_LOGW_LIMIT(fmt, ...) \ 110 do { \ 111 bool can = true; \ 112 EH_PRINT_LIMIT(LOG_CORE, LOG_WARN, EH_LOG_LIMIT_INTERVALS, can); \ 113 if (can) { \ 114 HILOGW(fmt, ##__VA_ARGS__); \ 115 } \ 116 } while (0) 117 118 #define EH_LOGI_LIMIT(fmt, ...) \ 119 do { \ 120 bool can = true; \ 121 EH_PRINT_LIMIT(LOG_CORE, LOG_INFO, EH_LOG_LIMIT_INTERVALS, can); \ 122 if (can) { \ 123 HILOGI(fmt, ##__VA_ARGS__); \ 124 } \ 125 } while (0) 126 127 #define EH_LOGD_LIMIT(fmt, ...) \ 128 do { \ 129 bool can = true; \ 130 EH_PRINT_LIMIT(LOG_CORE, LOG_DEBUG, EH_LOG_LIMIT_INTERVALS, can); \ 131 if (can) { \ 132 HILOGD(fmt, ##__VA_ARGS__); \ 133 } \ 134 } while (0) 135 136 #endif // BASE_EVENTHANDLER_INTERFACES_INNER_API_EVENT_LOGGER_H 137