1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci
16d9f0492fSopenharmony_ci#ifndef BEGET_EXT_API_H
17d9f0492fSopenharmony_ci#define BEGET_EXT_API_H
18d9f0492fSopenharmony_ci#include <stdint.h>
19d9f0492fSopenharmony_ci#include <stdarg.h>
20d9f0492fSopenharmony_ci#include <string.h>
21d9f0492fSopenharmony_ci#ifdef __LITEOS_M__
22d9f0492fSopenharmony_ci#include "hilog_lite/hiview_log.h"
23d9f0492fSopenharmony_ci#endif
24d9f0492fSopenharmony_ci
25d9f0492fSopenharmony_ci#ifdef __cplusplus
26d9f0492fSopenharmony_ci#if __cplusplus
27d9f0492fSopenharmony_ciextern "C" {
28d9f0492fSopenharmony_ci#endif
29d9f0492fSopenharmony_ci#endif
30d9f0492fSopenharmony_ci
31d9f0492fSopenharmony_ci#define INIT_DEBUG_LEVEL "persist.init.debug.loglevel"
32d9f0492fSopenharmony_ci
33d9f0492fSopenharmony_ci#ifndef INIT_LOG_PATH
34d9f0492fSopenharmony_ci#define INIT_LOG_PATH STARTUP_INIT_UT_PATH"/data/init_agent/"
35d9f0492fSopenharmony_ci#endif
36d9f0492fSopenharmony_ci
37d9f0492fSopenharmony_ci#if defined(__GNUC__) && (__GNUC__ >= 4)
38d9f0492fSopenharmony_ci    #define INIT_PUBLIC_API __attribute__((visibility ("default")))
39d9f0492fSopenharmony_ci    #define INIT_INNER_API __attribute__((visibility ("default")))
40d9f0492fSopenharmony_ci    #define INIT_LOCAL_API __attribute__((visibility("hidden")))
41d9f0492fSopenharmony_ci#else
42d9f0492fSopenharmony_ci    #define INIT_PUBLIC_API
43d9f0492fSopenharmony_ci    #define INIT_INNER_API
44d9f0492fSopenharmony_ci    #define INIT_LOCAL_API
45d9f0492fSopenharmony_ci#endif
46d9f0492fSopenharmony_ci
47d9f0492fSopenharmony_citypedef enum InitLogLevel {
48d9f0492fSopenharmony_ci    INIT_DEBUG = 0,
49d9f0492fSopenharmony_ci    INIT_INFO,
50d9f0492fSopenharmony_ci    INIT_WARN,
51d9f0492fSopenharmony_ci    INIT_ERROR,
52d9f0492fSopenharmony_ci    INIT_FATAL
53d9f0492fSopenharmony_ci} InitLogLevel;
54d9f0492fSopenharmony_ci
55d9f0492fSopenharmony_citypedef enum {
56d9f0492fSopenharmony_ci    FSTAB_MOUNT_FAILED = 0,
57d9f0492fSopenharmony_ci    SYS_PARAM_INIT_FAILED,
58d9f0492fSopenharmony_ci    INIT_GOLBALY_KEY_FAILED,
59d9f0492fSopenharmony_ci    CORE_PROCESS_FAILED
60d9f0492fSopenharmony_ci} StartupInitErrno;
61d9f0492fSopenharmony_ci
62d9f0492fSopenharmony_ci#if (defined(STARTUP_INIT_TEST) || defined(APPSPAWN_TEST))
63d9f0492fSopenharmony_ci#define STARTUP_FILE_NAME   (__FILE_NAME__)
64d9f0492fSopenharmony_ci#define STATIC
65d9f0492fSopenharmony_ci#else
66d9f0492fSopenharmony_ci#define STARTUP_FILE_NAME   (strrchr((__FILE_NAME__), '/') ? strrchr((__FILE_NAME__), '/') + 1 : (__FILE_NAME__))
67d9f0492fSopenharmony_ci#define STATIC static
68d9f0492fSopenharmony_ci#endif
69d9f0492fSopenharmony_ci
70d9f0492fSopenharmony_ci#ifndef STARTUP_INIT_UT_PATH
71d9f0492fSopenharmony_ci#ifdef STARTUP_INIT_TEST // ut
72d9f0492fSopenharmony_ci#define STARTUP_INIT_UT_PATH "/data/init_ut"
73d9f0492fSopenharmony_ci#else
74d9f0492fSopenharmony_ci#define STARTUP_INIT_UT_PATH ""
75d9f0492fSopenharmony_ci#endif
76d9f0492fSopenharmony_ci#endif
77d9f0492fSopenharmony_ci
78d9f0492fSopenharmony_ciINIT_PUBLIC_API void StartupLog(InitLogLevel logLevel, uint32_t domain, const char *tag, const char *fmt, ...);
79d9f0492fSopenharmony_ciINIT_PUBLIC_API void SetInitLogLevel(InitLogLevel level);
80d9f0492fSopenharmony_ciINIT_PUBLIC_API InitLogLevel GetInitLogLevel(void);
81d9f0492fSopenharmony_ci
82d9f0492fSopenharmony_ci#if defined(INIT_NO_LOG)
83d9f0492fSopenharmony_ci#define STARTUP_LOGV(domain, tag, fmt, ...)
84d9f0492fSopenharmony_ci#define STARTUP_LOGI(domain, tag, fmt, ...)
85d9f0492fSopenharmony_ci#define STARTUP_LOGW(domain, tag, fmt, ...)
86d9f0492fSopenharmony_ci#define STARTUP_LOGE(domain, tag, fmt, ...)
87d9f0492fSopenharmony_ci#define STARTUP_LOGF(domain, tag, fmt, ...)
88d9f0492fSopenharmony_ci#else
89d9f0492fSopenharmony_ci#ifndef __LITEOS_M__
90d9f0492fSopenharmony_ci#define STARTUP_LOGV(domain, tag, fmt, ...) \
91d9f0492fSopenharmony_ci    StartupLog(INIT_DEBUG, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
92d9f0492fSopenharmony_ci#define STARTUP_LOGI(domain, tag, fmt, ...) \
93d9f0492fSopenharmony_ci    StartupLog(INIT_INFO, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
94d9f0492fSopenharmony_ci#define STARTUP_LOGW(domain, tag, fmt, ...) \
95d9f0492fSopenharmony_ci    StartupLog(INIT_WARN, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
96d9f0492fSopenharmony_ci#define STARTUP_LOGE(domain, tag, fmt, ...) \
97d9f0492fSopenharmony_ci    StartupLog(INIT_ERROR, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
98d9f0492fSopenharmony_ci#define STARTUP_LOGF(domain, tag, fmt, ...) \
99d9f0492fSopenharmony_ci    StartupLog(INIT_FATAL, domain, tag, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
100d9f0492fSopenharmony_ci#else
101d9f0492fSopenharmony_ci#define STARTUP_LOGV(domain, tag, fmt, ...) \
102d9f0492fSopenharmony_ci    HILOG_DEBUG(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
103d9f0492fSopenharmony_ci#define STARTUP_LOGI(domain, tag, fmt, ...) \
104d9f0492fSopenharmony_ci    HILOG_INFO(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
105d9f0492fSopenharmony_ci#define STARTUP_LOGW(domain, tag, fmt, ...) \
106d9f0492fSopenharmony_ci    HILOG_WARN(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
107d9f0492fSopenharmony_ci#define STARTUP_LOGE(domain, tag, fmt, ...) \
108d9f0492fSopenharmony_ci    HILOG_ERROR(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
109d9f0492fSopenharmony_ci#define STARTUP_LOGF(domain, tag, fmt, ...) \
110d9f0492fSopenharmony_ci    HILOG_FATAL(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
111d9f0492fSopenharmony_ci#endif
112d9f0492fSopenharmony_ci#endif
113d9f0492fSopenharmony_ci
114d9f0492fSopenharmony_ci#define BASE_DOMAIN 0xD002C00
115d9f0492fSopenharmony_ci#ifndef BEGET_DOMAIN
116d9f0492fSopenharmony_ci#define BEGET_DOMAIN (BASE_DOMAIN + 0xb)
117d9f0492fSopenharmony_ci#endif
118d9f0492fSopenharmony_ci#define BEGET_LABEL "BEGET"
119d9f0492fSopenharmony_ci#define BEGET_LOGI(fmt, ...) STARTUP_LOGI(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
120d9f0492fSopenharmony_ci#define BEGET_LOGE(fmt, ...) STARTUP_LOGE(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
121d9f0492fSopenharmony_ci#define BEGET_LOGV(fmt, ...) STARTUP_LOGV(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
122d9f0492fSopenharmony_ci#define BEGET_LOGW(fmt, ...) STARTUP_LOGW(BEGET_DOMAIN, BEGET_LABEL, fmt, ##__VA_ARGS__)
123d9f0492fSopenharmony_ci
124d9f0492fSopenharmony_ci#define InitLogPrint(outFileName, logLevel, kLevel, fmt, ...) \
125d9f0492fSopenharmony_ci    StartupLog(logLevel, BEGET_DOMAIN, kLevel, fmt, ##__VA_ARGS__)
126d9f0492fSopenharmony_ci
127d9f0492fSopenharmony_ci#define BEGET_ERROR_CHECK(ret, statement, format, ...) \
128d9f0492fSopenharmony_ci    if (!(ret)) {                                     \
129d9f0492fSopenharmony_ci        BEGET_LOGE(format, ##__VA_ARGS__);             \
130d9f0492fSopenharmony_ci        statement;                                    \
131d9f0492fSopenharmony_ci    }                                                 \
132d9f0492fSopenharmony_ci
133d9f0492fSopenharmony_ci#define BEGET_INFO_CHECK(ret, statement, format, ...) \
134d9f0492fSopenharmony_ci    if (!(ret)) {                                    \
135d9f0492fSopenharmony_ci        BEGET_LOGI(format, ##__VA_ARGS__);            \
136d9f0492fSopenharmony_ci        statement;                                   \
137d9f0492fSopenharmony_ci    }                                          \
138d9f0492fSopenharmony_ci
139d9f0492fSopenharmony_ci#define BEGET_WARNING_CHECK(ret, statement, format, ...) \
140d9f0492fSopenharmony_ci    if (!(ret)) {                                     \
141d9f0492fSopenharmony_ci        BEGET_LOGW(format, ##__VA_ARGS__);             \
142d9f0492fSopenharmony_ci        statement;                                    \
143d9f0492fSopenharmony_ci    }                                                 \
144d9f0492fSopenharmony_ci
145d9f0492fSopenharmony_ci#define BEGET_CHECK(ret, statement) \
146d9f0492fSopenharmony_ci    if (!(ret)) {                  \
147d9f0492fSopenharmony_ci        statement;                 \
148d9f0492fSopenharmony_ci    }                         \
149d9f0492fSopenharmony_ci
150d9f0492fSopenharmony_ci#define BEGET_CHECK_RETURN_VALUE(ret, result) \
151d9f0492fSopenharmony_ci    do {                                \
152d9f0492fSopenharmony_ci        if (!(ret)) {                            \
153d9f0492fSopenharmony_ci            return result;                       \
154d9f0492fSopenharmony_ci        }                                  \
155d9f0492fSopenharmony_ci    } while (0)
156d9f0492fSopenharmony_ci
157d9f0492fSopenharmony_ci#define BEGET_CHECK_ONLY_RETURN(ret) \
158d9f0492fSopenharmony_ci    do {                                \
159d9f0492fSopenharmony_ci        if (!(ret)) {                   \
160d9f0492fSopenharmony_ci            return;                     \
161d9f0492fSopenharmony_ci        } \
162d9f0492fSopenharmony_ci    } while (0)
163d9f0492fSopenharmony_ci
164d9f0492fSopenharmony_ci#define BEGET_CHECK_ONLY_ELOG(ret, format, ...) \
165d9f0492fSopenharmony_ci    do {                                       \
166d9f0492fSopenharmony_ci        if (!(ret)) {                          \
167d9f0492fSopenharmony_ci            BEGET_LOGE(format, ##__VA_ARGS__);  \
168d9f0492fSopenharmony_ci        } \
169d9f0492fSopenharmony_ci    } while (0)
170d9f0492fSopenharmony_ci
171d9f0492fSopenharmony_ci#ifdef __cplusplus
172d9f0492fSopenharmony_ci#if __cplusplus
173d9f0492fSopenharmony_ci}
174d9f0492fSopenharmony_ci#endif
175d9f0492fSopenharmony_ci#endif
176d9f0492fSopenharmony_ci#endif
177