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 INIT_LOG_H 17 #define INIT_LOG_H 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <unistd.h> 22 23 #include "beget_ext.h" 24 25 #ifdef __cplusplus 26 #if __cplusplus 27 extern "C" { 28 #endif 29 #endif 30 31 #ifndef INIT_LOG_TAG 32 #define INIT_LOG_TAG "Init" 33 #endif 34 35 #ifndef INIT_LOG_DOMAIN 36 #define INIT_LOG_DOMAIN (BASE_DOMAIN + 1) 37 #endif 38 39 typedef void (*InitCommLog)(int logLevel, uint32_t domain, const char *tag, const char *fmt, va_list vargs); 40 41 INIT_LOCAL_API void OpenLogDevice(void); 42 INIT_LOCAL_API void InitLog(int logLevel, unsigned int domain, const char *tag, const char *fmt, va_list vargs); 43 INIT_LOCAL_API void SetInitCommLog(InitCommLog logFunc); 44 45 #if defined(INIT_NO_LOG) || defined(PARAM_BASE) 46 #define EnableInitLog(level) ((void)level) 47 #define INIT_LOGV(fmt, ...) 48 #define INIT_LOGI(fmt, ...) 49 #define INIT_LOGW(fmt, ...) 50 #define INIT_LOGE(fmt, ...) 51 #define INIT_LOGF(fmt, ...) 52 #else 53 #ifdef __LITEOS_M__ 54 #define INIT_LOGV(fmt, ...) \ 55 HILOG_DEBUG(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__) 56 #define INIT_LOGI(fmt, ...) \ 57 HILOG_INFO(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__) 58 #define INIT_LOGW(fmt, ...) \ 59 HILOG_WARN(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__) 60 #define INIT_LOGE(fmt, ...) \ 61 HILOG_ERROR(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__) 62 #define INIT_LOGF(fmt, ...) \ 63 HILOG_FATAL(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__) 64 #else 65 INIT_LOCAL_API void EnableInitLog(InitLogLevel level); 66 #define INIT_LOGV(fmt, ...) \ 67 StartupLog(INIT_DEBUG, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 68 #define INIT_LOGI(fmt, ...) \ 69 StartupLog(INIT_INFO, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 70 #define INIT_LOGW(fmt, ...) \ 71 StartupLog(INIT_WARN, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 72 #define INIT_LOGE(fmt, ...) \ 73 StartupLog(INIT_ERROR, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 74 #define INIT_LOGF(fmt, ...) \ 75 StartupLog(INIT_FATAL, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 76 #endif 77 #endif 78 79 #ifndef UNLIKELY 80 #define UNLIKELY(x) __builtin_expect(!!(x), 0) 81 #endif 82 83 #define INIT_ERROR_CHECK(ret, statement, format, ...) \ 84 do { \ 85 if (!(ret)) { \ 86 INIT_LOGE(format, ##__VA_ARGS__); \ 87 statement; \ 88 } \ 89 } while (0) 90 91 #define INIT_INFO_CHECK(ret, statement, format, ...) \ 92 do { \ 93 if (!(ret)) { \ 94 INIT_LOGI(format, ##__VA_ARGS__); \ 95 statement; \ 96 } \ 97 } while (0) 98 99 #define INIT_WARNING_CHECK(ret, statement, format, ...) \ 100 do { \ 101 if (!(ret)) { \ 102 INIT_LOGW(format, ##__VA_ARGS__); \ 103 statement; \ 104 } \ 105 } while (0) 106 107 #define INIT_CHECK(ret, statement) \ 108 do { \ 109 if (!(ret)) { \ 110 statement; \ 111 } \ 112 } while (0) 113 114 #define INIT_CHECK_RETURN_VALUE(ret, result) \ 115 do { \ 116 if (!(ret)) { \ 117 return result; \ 118 } \ 119 } while (0) 120 121 #define INIT_CHECK_ONLY_RETURN(ret) \ 122 do { \ 123 if (!(ret)) { \ 124 return; \ 125 } \ 126 } while (0) 127 128 #define INIT_CHECK_ONLY_ELOG(ret, format, ...) \ 129 do { \ 130 if (!(ret)) { \ 131 INIT_LOGE(format, ##__VA_ARGS__); \ 132 } \ 133 } while (0) 134 135 #ifdef __cplusplus 136 #if __cplusplus 137 } 138 #endif 139 #endif 140 141 #endif // INIT_LOG_H 142