• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void SetInitLogLevel(InitLogLevel logLevel);
32 
33 #ifndef INIT_LOG_TAG
34 #define INIT_LOG_TAG "Init"
35 #endif
36 
37 #ifdef OHOS_LITE
38 #define INIT_LOGV(fmt, ...) InitToHiLog(INIT_DEBUG, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__)
39 #define INIT_LOGI(fmt, ...) InitToHiLog(INIT_INFO, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__)
40 #define INIT_LOGW(fmt, ...) InitToHiLog(INIT_WARN, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__)
41 #define INIT_LOGE(fmt, ...) InitToHiLog(INIT_ERROR, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__)
42 #define INIT_LOGF(fmt, ...) InitToHiLog(INIT_FATAL, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__)
43 
44 #define STARTUP_LOGV(logFIle, LABEL, fmt, ...) InitToHiLog(LABEL, INIT_DEBUG, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__)
45 #define STARTUP_LOGI(logFIle, LABEL, fmt, ...) InitToHiLog(INIT_INFO, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__)
46 #define STARTUP_LOGE(logFIle, LABEL, fmt, ...) InitToHiLog(INIT_ERROR, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__)
47 
48 void InitToHiLog(InitLogLevel logLevel, const char *fmt, ...);
49 
50 #else
51 
52 #define INIT_LOGV(fmt, ...) \
53     do {    \
54         InitLogPrint(INIT_LOG_PATH "init_agent.log", INIT_DEBUG, "<7>", "[%s:%d)] " fmt "\n", \
55             (FILE_NAME), (__LINE__), ##__VA_ARGS__); \
56     } while (0)
57 
58 #define INIT_LOGI(fmt, ...) \
59     do {    \
60         InitLogPrint(INIT_LOG_PATH "init_agent.log", INIT_INFO, "<6>", "[%s:%d)] " fmt "\n", \
61             (FILE_NAME), (__LINE__), ##__VA_ARGS__); \
62     } while (0)
63 
64 #define INIT_LOGW(fmt, ...) \
65     do {    \
66         InitLogPrint(INIT_LOG_PATH "init_agent.log", INIT_WARN, "<4>", "[%s:%d)] " fmt "\n", \
67             (FILE_NAME), (__LINE__), ##__VA_ARGS__); \
68     } while (0)
69 
70 #define INIT_LOGE(fmt, ...) \
71     do {    \
72         InitLogPrint(INIT_LOG_PATH "init_agent.log", INIT_ERROR, "<3>", "[%s:%d)] " fmt "\n", \
73             (FILE_NAME), (__LINE__), ##__VA_ARGS__); \
74     } while (0)
75 
76 #define INIT_LOGF(fmt, ...) \
77     do {    \
78         InitLogPrint(INIT_LOG_PATH "init_agent.log", INIT_FATAL, "<3>", "[%s:%d)] " fmt "\n", \
79             (FILE_NAME), (__LINE__), ##__VA_ARGS__); \
80     } while (0)
81 
82 #endif
83 
84 #ifndef UNLIKELY
85 #define UNLIKELY(x)    __builtin_expect(!!(x), 0)
86 #endif
87 
88 #define INIT_ERROR_CHECK(ret, statement, format, ...) \
89     do {                                                  \
90         if (!(ret)) {                                     \
91             INIT_LOGE(format, ##__VA_ARGS__);             \
92             statement;                                    \
93         }                                                 \
94     } while (0)
95 
96 #define INIT_INFO_CHECK(ret, statement, format, ...) \
97     do {                                                  \
98         if (!(ret)) {                                    \
99             INIT_LOGI(format, ##__VA_ARGS__);            \
100             statement;                                   \
101         }                                          \
102     } while (0)
103 
104 #define INIT_WARNING_CHECK(ret, statement, format, ...) \
105     do {                                                  \
106         if (!(ret)) {                                     \
107             INIT_LOGW(format, ##__VA_ARGS__);             \
108             statement;                                    \
109         }                                                 \
110     } while (0)
111 
112 #define INIT_CHECK(ret, statement) \
113     do {                                \
114         if (!(ret)) {                  \
115             statement;                 \
116         }                         \
117     } while (0)
118 
119 #define INIT_CHECK_RETURN_VALUE(ret, result) \
120     do {                                \
121         if (!(ret)) {                            \
122             return result;                       \
123         }                                  \
124     } while (0)
125 
126 #define INIT_CHECK_ONLY_RETURN(ret) \
127     do {                                \
128         if (!(ret)) {                   \
129             return;                     \
130         } \
131     } while (0)
132 
133 #define INIT_CHECK_ONLY_ELOG(ret, format, ...) \
134     do {                                       \
135         if (!(ret)) {                          \
136             INIT_LOGE(format, ##__VA_ARGS__);  \
137         } \
138     } while (0)
139 
140 #ifdef __cplusplus
141 #if __cplusplus
142 }
143 #endif
144 #endif
145 
146 #endif // INIT_LOG_H
147