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