1 /* 2 * Copyright (c) 2021 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 BEGET_EXT_API_H 17 #define BEGET_EXT_API_H 18 19 #ifdef __cplusplus 20 #if __cplusplus 21 extern "C" { 22 #endif 23 #endif 24 25 #if defined(ENABLE_HILOG) || defined(OHOS_LITE) 26 #include "hilog/log.h" 27 #undef LOG_DOMAIN 28 #define LOG_DOMAIN 0xD000719 29 #endif 30 31 typedef enum InitLogLevel { 32 INIT_DEBUG = 0, 33 INIT_INFO, 34 INIT_WARN, 35 INIT_ERROR, 36 INIT_FATAL 37 } InitLogLevel; 38 39 #ifndef INIT_LOG_PATH 40 #define INIT_LOG_PATH "/data/init_agent/" 41 #endif 42 43 #define FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) 44 void InitLogInit(const char *outFileName, InitLogLevel logLevel, const char *kLevel, const char *fmt, ...); 45 void InitLogAgent(const char *outFileName, InitLogLevel logLevel, const char *kLevel, const char *fmt, ...); 46 void OpenLogDevice(void); 47 48 #ifndef INIT_AGENT 49 #define InitLogPrint InitLogInit 50 #else 51 #define InitLogPrint InitLogAgent 52 #endif 53 54 #ifndef OHOS_LITE 55 #ifndef ENABLE_HILOG 56 #define STARTUP_LOGV(logFile, LABEL, fmt, ...) \ 57 do { \ 58 InitLogPrint(INIT_LOG_PATH logFile, INIT_DEBUG, LABEL, "[%s:%d)] " fmt "\n", \ 59 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 60 } while (0) 61 62 #define STARTUP_LOGI(logFile, LABEL, fmt, ...) \ 63 do { \ 64 InitLogPrint(INIT_LOG_PATH logFile, INIT_INFO, LABEL, "[%s:%d)] " fmt "\n", \ 65 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 66 } while (0) 67 68 #define STARTUP_LOGE(logFile, LABEL, fmt, ...) \ 69 do { \ 70 InitLogPrint(INIT_LOG_PATH logFile, INIT_ERROR, LABEL, "[%s:%d)] " fmt "\n", \ 71 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 72 } while (0) 73 74 #define STARTUP_LOGW(logFile, LABEL, fmt, ...) \ 75 do { \ 76 InitLogPrint(INIT_LOG_PATH logFile, INIT_WARN, LABEL, "[%s:%d)] " fmt "\n", \ 77 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 78 } while (0) 79 80 #else 81 #define STARTUP_LOGV(logFile, LABEL, fmt, ...) \ 82 do { \ 83 InitLogPrint(INIT_LOG_PATH logFile, INIT_DEBUG, LABEL, "[%s:%d)] " fmt "\n", \ 84 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 85 (void)HiLogPrint(LOG_APP, LOG_DEBUG, LOG_DOMAIN, LABEL, "[%{public}s(%{public}d)] " fmt, \ 86 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 87 } while (0) 88 89 #define STARTUP_LOGI(logFile, LABEL, fmt, ...) \ 90 do { \ 91 InitLogPrint(INIT_LOG_PATH logFile, INIT_INFO, LABEL, "[%s:%d)] " fmt "\n", \ 92 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 93 (void)HiLogPrint(LOG_APP, LOG_INFO, LOG_DOMAIN, LABEL, "[%{public}s(%{public}d)] " fmt, \ 94 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 95 } while (0) 96 97 #define STARTUP_LOGE(logFile, LABEL, fmt, ...) \ 98 do { \ 99 InitLogPrint(INIT_LOG_PATH logFile, INIT_ERROR, LABEL, "[%s:%d)] " fmt "\n", \ 100 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 101 (void)HiLogPrint(LOG_APP, LOG_ERROR, LOG_DOMAIN, LABEL, "[%{public}s(%{public}d)] " fmt, \ 102 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 103 } while (0) 104 105 #define STARTUP_LOGW(logFile, LABEL, fmt, ...) \ 106 do { \ 107 InitLogPrint(INIT_LOG_PATH logFile, INIT_WARN, LABEL, "[%s:%d)] " fmt "\n", \ 108 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 109 (void)HiLogPrint(LOG_APP, LOG_WARN, LOG_DOMAIN, LABEL, "[%{public}s(%{public}d)] " fmt, \ 110 (FILE_NAME), (__LINE__), ##__VA_ARGS__); \ 111 } while (0) 112 #endif 113 #endif 114 115 #define BEGET_LOG_FILE "begetctrl.log" 116 #define BEGET_LABEL "BEGET" 117 #define BEGET_LOGI(fmt, ...) STARTUP_LOGI(BEGET_LOG_FILE, BEGET_LABEL, fmt, ##__VA_ARGS__) 118 #define BEGET_LOGE(fmt, ...) STARTUP_LOGE(BEGET_LOG_FILE, BEGET_LABEL, fmt, ##__VA_ARGS__) 119 #define BEGET_LOGV(fmt, ...) STARTUP_LOGV(BEGET_LOG_FILE, BEGET_LABEL, fmt, ##__VA_ARGS__) 120 #define BEGET_LOGW(fmt, ...) STARTUP_LOGW(BEGET_LOG_FILE, BEGET_LABEL, fmt, ##__VA_ARGS__) 121 122 #define BEGET_ERROR_CHECK(ret, statement, format, ...) \ 123 if (!(ret)) { \ 124 BEGET_LOGE(format, ##__VA_ARGS__); \ 125 statement; \ 126 } \ 127 128 #define BEGET_INFO_CHECK(ret, statement, format, ...) \ 129 if (!(ret)) { \ 130 BEGET_LOGI(format, ##__VA_ARGS__); \ 131 statement; \ 132 } \ 133 134 #define BEGET_WARNING_CHECK(ret, statement, format, ...) \ 135 if (!(ret)) { \ 136 BEGET_LOGW(format, ##__VA_ARGS__); \ 137 statement; \ 138 } \ 139 140 #define BEGET_CHECK(ret, statement) \ 141 if (!(ret)) { \ 142 statement; \ 143 } \ 144 145 #define BEGET_CHECK_RETURN_VALUE(ret, result) \ 146 do { \ 147 if (!(ret)) { \ 148 return result; \ 149 } \ 150 } while (0) 151 152 #define BEGET_CHECK_ONLY_RETURN(ret) \ 153 do { \ 154 if (!(ret)) { \ 155 return; \ 156 } \ 157 } while (0) 158 159 #define BEGET_CHECK_ONLY_ELOG(ret, format, ...) \ 160 do { \ 161 if (!(ret)) { \ 162 BEGET_LOGE(format, ##__VA_ARGS__); \ 163 } \ 164 } while (0) 165 166 #ifdef __cplusplus 167 #if __cplusplus 168 } 169 #endif 170 #endif 171 #endif