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
27extern "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
47typedef enum InitLogLevel {
48    INIT_DEBUG = 0,
49    INIT_INFO,
50    INIT_WARN,
51    INIT_ERROR,
52    INIT_FATAL
53} InitLogLevel;
54
55typedef enum {
56    FSTAB_MOUNT_FAILED = 0,
57    SYS_PARAM_INIT_FAILED,
58    INIT_GOLBALY_KEY_FAILED,
59    CORE_PROCESS_FAILED
60} StartupInitErrno;
61
62#if (defined(STARTUP_INIT_TEST) || defined(APPSPAWN_TEST))
63#define STARTUP_FILE_NAME   (__FILE_NAME__)
64#define STATIC
65#else
66#define STARTUP_FILE_NAME   (strrchr((__FILE_NAME__), '/') ? strrchr((__FILE_NAME__), '/') + 1 : (__FILE_NAME__))
67#define STATIC static
68#endif
69
70#ifndef STARTUP_INIT_UT_PATH
71#ifdef STARTUP_INIT_TEST // ut
72#define STARTUP_INIT_UT_PATH "/data/init_ut"
73#else
74#define STARTUP_INIT_UT_PATH ""
75#endif
76#endif
77
78INIT_PUBLIC_API void StartupLog(InitLogLevel logLevel, uint32_t domain, const char *tag, const char *fmt, ...);
79INIT_PUBLIC_API void SetInitLogLevel(InitLogLevel level);
80INIT_PUBLIC_API InitLogLevel GetInitLogLevel(void);
81
82#if defined(INIT_NO_LOG)
83#define STARTUP_LOGV(domain, tag, fmt, ...)
84#define STARTUP_LOGI(domain, tag, fmt, ...)
85#define STARTUP_LOGW(domain, tag, fmt, ...)
86#define STARTUP_LOGE(domain, tag, fmt, ...)
87#define STARTUP_LOGF(domain, tag, fmt, ...)
88#else
89#ifndef __LITEOS_M__
90#define STARTUP_LOGV(domain, tag, fmt, ...) \
91    StartupLog(INIT_DEBUG, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
92#define STARTUP_LOGI(domain, tag, fmt, ...) \
93    StartupLog(INIT_INFO, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
94#define STARTUP_LOGW(domain, tag, fmt, ...) \
95    StartupLog(INIT_WARN, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
96#define STARTUP_LOGE(domain, tag, fmt, ...) \
97    StartupLog(INIT_ERROR, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
98#define STARTUP_LOGF(domain, tag, fmt, ...) \
99    StartupLog(INIT_FATAL, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
100#else
101#define STARTUP_LOGV(domain, tag, fmt, ...) \
102    HILOG_DEBUG(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
103#define STARTUP_LOGI(domain, tag, fmt, ...) \
104    HILOG_INFO(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
105#define STARTUP_LOGW(domain, tag, fmt, ...) \
106    HILOG_WARN(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
107#define STARTUP_LOGE(domain, tag, fmt, ...) \
108    HILOG_ERROR(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
109#define STARTUP_LOGF(domain, tag, fmt, ...) \
110    HILOG_FATAL(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
111#endif
112#endif
113
114#define BASE_DOMAIN 0xD002C00
115#ifndef BEGET_DOMAIN
116#define BEGET_DOMAIN (BASE_DOMAIN + 0xb)
117#endif
118#define BEGET_LABEL "BEGET"
119#define BEGET_LOGI(fmt, ...) STARTUP_LOGI(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
120#define BEGET_LOGE(fmt, ...) STARTUP_LOGE(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
121#define BEGET_LOGV(fmt, ...) STARTUP_LOGV(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
122#define BEGET_LOGW(fmt, ...) STARTUP_LOGW(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
123
124#define InitLogPrint(outFileName, logLevel, kLevel, fmt, ...) \
125    StartupLog(logLevel, BEGET_DOMAIN, kLevel, fmt, ##__VA_ARGS__)
126
127#define BEGET_ERROR_CHECK(ret, statement, format, ...) \
128    if (!(ret)) {                                     \
129        BEGET_LOGE(format, ##__VA_ARGS__);             \
130        statement;                                    \
131    }                                                 \
132
133#define BEGET_INFO_CHECK(ret, statement, format, ...) \
134    if (!(ret)) {                                    \
135        BEGET_LOGI(format, ##__VA_ARGS__);            \
136        statement;                                   \
137    }                                          \
138
139#define BEGET_WARNING_CHECK(ret, statement, format, ...) \
140    if (!(ret)) {                                     \
141        BEGET_LOGW(format, ##__VA_ARGS__);             \
142        statement;                                    \
143    }                                                 \
144
145#define BEGET_CHECK(ret, statement) \
146    if (!(ret)) {                  \
147        statement;                 \
148    }                         \
149
150#define BEGET_CHECK_RETURN_VALUE(ret, result) \
151    do {                                \
152        if (!(ret)) {                            \
153            return result;                       \
154        }                                  \
155    } while (0)
156
157#define BEGET_CHECK_ONLY_RETURN(ret) \
158    do {                                \
159        if (!(ret)) {                   \
160            return;                     \
161        } \
162    } while (0)
163
164#define BEGET_CHECK_ONLY_ELOG(ret, format, ...) \
165    do {                                       \
166        if (!(ret)) {                          \
167            BEGET_LOGE(format, ##__VA_ARGS__);  \
168        } \
169    } while (0)
170
171#ifdef __cplusplus
172#if __cplusplus
173}
174#endif
175#endif
176#endif
177