• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include <stdint.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #ifdef __LITEOS_M__
22 #include "hilog_lite/hiview_log.h"
23 #endif
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 #define INIT_DEBUG_LEVEL "persist.init.debug.loglevel"
32 
33 #ifndef INIT_LOG_PATH
34 #define INIT_LOG_PATH STARTUP_INIT_UT_PATH"/data/init_agent/"
35 #endif
36 
37 #if defined(__GNUC__) && (__GNUC__ >= 4)
38     #define INIT_PUBLIC_API __attribute__((visibility ("default")))
39     #define INIT_INNER_API __attribute__((visibility ("default")))
40     #define INIT_LOCAL_API __attribute__((visibility("hidden")))
41 #else
42     #define INIT_PUBLIC_API
43     #define INIT_INNER_API
44     #define INIT_LOCAL_API
45 #endif
46 
47 typedef enum InitLogLevel {
48     INIT_DEBUG = 0,
49     INIT_INFO,
50     INIT_WARN,
51     INIT_ERROR,
52     INIT_FATAL
53 } InitLogLevel;
54 
55 #if (defined(STARTUP_INIT_TEST) || defined(APPSPAWN_TEST))
56 #define FILE_NAME   (strrchr((__FILE__), '/')  + 1)
57 #define STATIC
58 #else
59 #define FILE_NAME   (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
60 #define STATIC static
61 #endif
62 
63 #ifndef STARTUP_INIT_UT_PATH
64 #ifdef STARTUP_INIT_TEST // ut
65 #define STARTUP_INIT_UT_PATH "/data/init_ut"
66 #else
67 #define STARTUP_INIT_UT_PATH ""
68 #endif
69 #endif
70 
71 INIT_PUBLIC_API void StartupLog(InitLogLevel logLevel, uint32_t domain, const char *tag, const char *fmt, ...);
72 INIT_PUBLIC_API void SetInitLogLevel(InitLogLevel level);
73 INIT_PUBLIC_API InitLogLevel GetInitLogLevel(void);
74 
75 #if defined(INIT_NO_LOG)
76 #define STARTUP_LOGV(domain, tag, fmt, ...)
77 #define STARTUP_LOGI(domain, tag, fmt, ...)
78 #define STARTUP_LOGW(domain, tag, fmt, ...)
79 #define STARTUP_LOGE(domain, tag, fmt, ...)
80 #define STARTUP_LOGF(domain, tag, fmt, ...)
81 #else
82 #ifndef __LITEOS_M__
83 #define STARTUP_LOGV(domain, tag, fmt, ...) \
84     StartupLog(INIT_DEBUG, domain, tag, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
85 #define STARTUP_LOGI(domain, tag, fmt, ...) \
86     StartupLog(INIT_INFO, domain, tag, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
87 #define STARTUP_LOGW(domain, tag, fmt, ...) \
88     StartupLog(INIT_WARN, domain, tag, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
89 #define STARTUP_LOGE(domain, tag, fmt, ...) \
90     StartupLog(INIT_ERROR, domain, tag, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
91 #define STARTUP_LOGF(domain, tag, fmt, ...) \
92     StartupLog(INIT_FATAL, domain, tag, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
93 #else
94 #define STARTUP_LOGV(domain, tag, fmt, ...) \
95     HILOG_DEBUG(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
96 #define STARTUP_LOGI(domain, tag, fmt, ...) \
97     HILOG_INFO(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
98 #define STARTUP_LOGW(domain, tag, fmt, ...) \
99     HILOG_WARN(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
100 #define STARTUP_LOGE(domain, tag, fmt, ...) \
101     HILOG_ERROR(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
102 #define STARTUP_LOGF(domain, tag, fmt, ...) \
103     HILOG_FATAL(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
104 #endif
105 #endif
106 
107 #define BASE_DOMAIN 0xD002C00
108 #ifndef BEGET_DOMAIN
109 #define BEGET_DOMAIN (BASE_DOMAIN + 0xb)
110 #endif
111 #define BEGET_LABEL "BEGET"
112 #define BEGET_LOGI(fmt, ...) STARTUP_LOGI(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
113 #define BEGET_LOGE(fmt, ...) STARTUP_LOGE(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
114 #define BEGET_LOGV(fmt, ...) STARTUP_LOGV(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
115 #define BEGET_LOGW(fmt, ...) STARTUP_LOGW(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
116 
117 #define InitLogPrint(outFileName, logLevel, kLevel, fmt, ...) \
118     StartupLog(logLevel, BEGET_DOMAIN, kLevel, fmt, ##__VA_ARGS__)
119 
120 #define BEGET_ERROR_CHECK(ret, statement, format, ...) \
121     if (!(ret)) {                                     \
122         BEGET_LOGE(format, ##__VA_ARGS__);             \
123         statement;                                    \
124     }                                                 \
125 
126 #define BEGET_INFO_CHECK(ret, statement, format, ...) \
127     if (!(ret)) {                                    \
128         BEGET_LOGI(format, ##__VA_ARGS__);            \
129         statement;                                   \
130     }                                          \
131 
132 #define BEGET_WARNING_CHECK(ret, statement, format, ...) \
133     if (!(ret)) {                                     \
134         BEGET_LOGW(format, ##__VA_ARGS__);             \
135         statement;                                    \
136     }                                                 \
137 
138 #define BEGET_CHECK(ret, statement) \
139     if (!(ret)) {                  \
140         statement;                 \
141     }                         \
142 
143 #define BEGET_CHECK_RETURN_VALUE(ret, result) \
144     do {                                \
145         if (!(ret)) {                            \
146             return result;                       \
147         }                                  \
148     } while (0)
149 
150 #define BEGET_CHECK_ONLY_RETURN(ret) \
151     do {                                \
152         if (!(ret)) {                   \
153             return;                     \
154         } \
155     } while (0)
156 
157 #define BEGET_CHECK_ONLY_ELOG(ret, format, ...) \
158     do {                                       \
159         if (!(ret)) {                          \
160             BEGET_LOGE(format, ##__VA_ARGS__);  \
161         } \
162     } while (0)
163 
164 #ifdef __cplusplus
165 #if __cplusplus
166 }
167 #endif
168 #endif
169 #endif
170