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 INIT_LOCAL_API void EnableInitLog(InitLogLevel level); 45 INIT_LOCAL_API void EnableInitLogFromCmdline(void); 46 47 #if defined(INIT_NO_LOG) || defined(PARAM_BASE) 48 #define INIT_LOGV(fmt, ...) 49 #define INIT_LOGI(fmt, ...) 50 #define INIT_LOGW(fmt, ...) 51 #define INIT_LOGE(fmt, ...) 52 #define INIT_LOGF(fmt, ...) 53 #else 54 #define INIT_LOGV(fmt, ...) \ 55 StartupLog(INIT_DEBUG, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 56 #define INIT_LOGI(fmt, ...) \ 57 StartupLog(INIT_INFO, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 58 #define INIT_LOGW(fmt, ...) \ 59 StartupLog(INIT_WARN, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 60 #define INIT_LOGE(fmt, ...) \ 61 StartupLog(INIT_ERROR, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 62 #define INIT_LOGF(fmt, ...) \ 63 StartupLog(INIT_FATAL, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 64 #endif 65 66 #ifndef UNLIKELY 67 #define UNLIKELY(x) __builtin_expect(!!(x), 0) 68 #endif 69 70 #define INIT_ERROR_CHECK(ret, statement, format, ...) \ 71 do { \ 72 if (!(ret)) { \ 73 INIT_LOGE(format, ##__VA_ARGS__); \ 74 statement; \ 75 } \ 76 } while (0) 77 78 #define INIT_INFO_CHECK(ret, statement, format, ...) \ 79 do { \ 80 if (!(ret)) { \ 81 INIT_LOGI(format, ##__VA_ARGS__); \ 82 statement; \ 83 } \ 84 } while (0) 85 86 #define INIT_WARNING_CHECK(ret, statement, format, ...) \ 87 do { \ 88 if (!(ret)) { \ 89 INIT_LOGW(format, ##__VA_ARGS__); \ 90 statement; \ 91 } \ 92 } while (0) 93 94 #define INIT_CHECK(ret, statement) \ 95 do { \ 96 if (!(ret)) { \ 97 statement; \ 98 } \ 99 } while (0) 100 101 #define INIT_CHECK_RETURN_VALUE(ret, result) \ 102 do { \ 103 if (!(ret)) { \ 104 return result; \ 105 } \ 106 } while (0) 107 108 #define INIT_CHECK_ONLY_RETURN(ret) \ 109 do { \ 110 if (!(ret)) { \ 111 return; \ 112 } \ 113 } while (0) 114 115 #define INIT_CHECK_ONLY_ELOG(ret, format, ...) \ 116 do { \ 117 if (!(ret)) { \ 118 INIT_LOGE(format, ##__VA_ARGS__); \ 119 } \ 120 } while (0) 121 122 #ifdef __cplusplus 123 #if __cplusplus 124 } 125 #endif 126 #endif 127 128 #endif // INIT_LOG_H 129