1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021 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 INIT_LOG_H
17d9f0492fSopenharmony_ci#define INIT_LOG_H
18d9f0492fSopenharmony_ci#include <stdio.h>
19d9f0492fSopenharmony_ci#include <stdlib.h>
20d9f0492fSopenharmony_ci#include <string.h>
21d9f0492fSopenharmony_ci#include <unistd.h>
22d9f0492fSopenharmony_ci
23d9f0492fSopenharmony_ci#include "beget_ext.h"
24d9f0492fSopenharmony_ci
25d9f0492fSopenharmony_ci#ifdef __cplusplus
26d9f0492fSopenharmony_ci#if __cplusplus
27d9f0492fSopenharmony_ciextern "C" {
28d9f0492fSopenharmony_ci#endif
29d9f0492fSopenharmony_ci#endif
30d9f0492fSopenharmony_ci
31d9f0492fSopenharmony_ci#ifndef INIT_LOG_TAG
32d9f0492fSopenharmony_ci#define INIT_LOG_TAG "Init"
33d9f0492fSopenharmony_ci#endif
34d9f0492fSopenharmony_ci
35d9f0492fSopenharmony_ci#ifndef INIT_LOG_DOMAIN
36d9f0492fSopenharmony_ci#define INIT_LOG_DOMAIN (BASE_DOMAIN + 1)
37d9f0492fSopenharmony_ci#endif
38d9f0492fSopenharmony_ci
39d9f0492fSopenharmony_citypedef void (*InitCommLog)(int logLevel, uint32_t domain, const char *tag, const char *fmt, va_list vargs);
40d9f0492fSopenharmony_ci
41d9f0492fSopenharmony_ciINIT_LOCAL_API void OpenLogDevice(void);
42d9f0492fSopenharmony_ciINIT_LOCAL_API void InitLog(int logLevel, unsigned int domain, const char *tag, const char *fmt, va_list vargs);
43d9f0492fSopenharmony_ciINIT_LOCAL_API void SetInitCommLog(InitCommLog logFunc);
44d9f0492fSopenharmony_ciINIT_PUBLIC_API int GetKmsgFd();
45d9f0492fSopenharmony_ci
46d9f0492fSopenharmony_ci#if defined(INIT_NO_LOG) || defined(PARAM_BASE)
47d9f0492fSopenharmony_ci#define EnableInitLog(level) ((void)level)
48d9f0492fSopenharmony_ci#define INIT_LOGV(fmt, ...)
49d9f0492fSopenharmony_ci#define INIT_LOGI(fmt, ...)
50d9f0492fSopenharmony_ci#define INIT_LOGW(fmt, ...)
51d9f0492fSopenharmony_ci#define INIT_LOGE(fmt, ...)
52d9f0492fSopenharmony_ci#define INIT_LOGF(fmt, ...)
53d9f0492fSopenharmony_ci#else
54d9f0492fSopenharmony_ci#ifdef __LITEOS_M__
55d9f0492fSopenharmony_ci#define INIT_LOGV(fmt, ...) \
56d9f0492fSopenharmony_ci    HILOG_DEBUG(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
57d9f0492fSopenharmony_ci#define INIT_LOGI(fmt, ...) \
58d9f0492fSopenharmony_ci    HILOG_INFO(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
59d9f0492fSopenharmony_ci#define INIT_LOGW(fmt, ...) \
60d9f0492fSopenharmony_ci    HILOG_WARN(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
61d9f0492fSopenharmony_ci#define INIT_LOGE(fmt, ...) \
62d9f0492fSopenharmony_ci    HILOG_ERROR(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
63d9f0492fSopenharmony_ci#define INIT_LOGF(fmt, ...) \
64d9f0492fSopenharmony_ci    HILOG_FATAL(HILOG_MODULE_INIT, fmt, ##__VA_ARGS__)
65d9f0492fSopenharmony_ci#else
66d9f0492fSopenharmony_ciINIT_LOCAL_API void EnableInitLog(InitLogLevel level);
67d9f0492fSopenharmony_ci#define INIT_LOGV(fmt, ...) \
68d9f0492fSopenharmony_ci    StartupLog(INIT_DEBUG, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
69d9f0492fSopenharmony_ci#define INIT_LOGI(fmt, ...) \
70d9f0492fSopenharmony_ci    StartupLog(INIT_INFO, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
71d9f0492fSopenharmony_ci#define INIT_LOGW(fmt, ...) \
72d9f0492fSopenharmony_ci    StartupLog(INIT_WARN, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
73d9f0492fSopenharmony_ci#define INIT_LOGE(fmt, ...) \
74d9f0492fSopenharmony_ci    StartupLog(INIT_ERROR, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
75d9f0492fSopenharmony_ci#define INIT_LOGF(fmt, ...) \
76d9f0492fSopenharmony_ci    StartupLog(INIT_FATAL, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (STARTUP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
77d9f0492fSopenharmony_ci#endif
78d9f0492fSopenharmony_ci#endif
79d9f0492fSopenharmony_ci
80d9f0492fSopenharmony_ci#ifndef UNLIKELY
81d9f0492fSopenharmony_ci#define UNLIKELY(x)    __builtin_expect(!!(x), 0)
82d9f0492fSopenharmony_ci#endif
83d9f0492fSopenharmony_ci
84d9f0492fSopenharmony_ci#define INIT_ERROR_CHECK(ret, statement, format, ...) \
85d9f0492fSopenharmony_ci    do {                                                  \
86d9f0492fSopenharmony_ci        if (!(ret)) {                                     \
87d9f0492fSopenharmony_ci            INIT_LOGE(format, ##__VA_ARGS__);             \
88d9f0492fSopenharmony_ci            statement;                                    \
89d9f0492fSopenharmony_ci        }                                                 \
90d9f0492fSopenharmony_ci    } while (0)
91d9f0492fSopenharmony_ci
92d9f0492fSopenharmony_ci#define INIT_INFO_CHECK(ret, statement, format, ...) \
93d9f0492fSopenharmony_ci    do {                                                  \
94d9f0492fSopenharmony_ci        if (!(ret)) {                                    \
95d9f0492fSopenharmony_ci            INIT_LOGI(format, ##__VA_ARGS__);            \
96d9f0492fSopenharmony_ci            statement;                                   \
97d9f0492fSopenharmony_ci        }                                          \
98d9f0492fSopenharmony_ci    } while (0)
99d9f0492fSopenharmony_ci
100d9f0492fSopenharmony_ci#define INIT_WARNING_CHECK(ret, statement, format, ...) \
101d9f0492fSopenharmony_ci    do {                                                  \
102d9f0492fSopenharmony_ci        if (!(ret)) {                                     \
103d9f0492fSopenharmony_ci            INIT_LOGW(format, ##__VA_ARGS__);             \
104d9f0492fSopenharmony_ci            statement;                                    \
105d9f0492fSopenharmony_ci        }                                                 \
106d9f0492fSopenharmony_ci    } while (0)
107d9f0492fSopenharmony_ci
108d9f0492fSopenharmony_ci#define INIT_CHECK(ret, statement) \
109d9f0492fSopenharmony_ci    do {                                \
110d9f0492fSopenharmony_ci        if (!(ret)) {                  \
111d9f0492fSopenharmony_ci            statement;                 \
112d9f0492fSopenharmony_ci        }                         \
113d9f0492fSopenharmony_ci    } while (0)
114d9f0492fSopenharmony_ci
115d9f0492fSopenharmony_ci#define INIT_CHECK_RETURN_VALUE(ret, result) \
116d9f0492fSopenharmony_ci    do {                                \
117d9f0492fSopenharmony_ci        if (!(ret)) {                            \
118d9f0492fSopenharmony_ci            return result;                       \
119d9f0492fSopenharmony_ci        }                                  \
120d9f0492fSopenharmony_ci    } while (0)
121d9f0492fSopenharmony_ci
122d9f0492fSopenharmony_ci#define INIT_CHECK_ONLY_RETURN(ret) \
123d9f0492fSopenharmony_ci    do {                                \
124d9f0492fSopenharmony_ci        if (!(ret)) {                   \
125d9f0492fSopenharmony_ci            return;                     \
126d9f0492fSopenharmony_ci        } \
127d9f0492fSopenharmony_ci    } while (0)
128d9f0492fSopenharmony_ci
129d9f0492fSopenharmony_ci#define INIT_CHECK_ONLY_ELOG(ret, format, ...) \
130d9f0492fSopenharmony_ci    do {                                       \
131d9f0492fSopenharmony_ci        if (!(ret)) {                          \
132d9f0492fSopenharmony_ci            INIT_LOGE(format, ##__VA_ARGS__);  \
133d9f0492fSopenharmony_ci        } \
134d9f0492fSopenharmony_ci    } while (0)
135d9f0492fSopenharmony_ci
136d9f0492fSopenharmony_ci#ifdef __cplusplus
137d9f0492fSopenharmony_ci#if __cplusplus
138d9f0492fSopenharmony_ci}
139d9f0492fSopenharmony_ci#endif
140d9f0492fSopenharmony_ci#endif
141d9f0492fSopenharmony_ci
142d9f0492fSopenharmony_ci#endif // INIT_LOG_H
143