init_log.h 4.4 KB
Newer Older
Z
zhong_ning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INIT_LOG_H
#define INIT_LOG_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Z
zhong_ning 已提交
21
#include <unistd.h>
Z
zhong_ning 已提交
22

X
xionglei6 已提交
23
#include "beget_ext.h"
S
sun_fan 已提交
24

Z
zhong_ning 已提交
25 26 27 28 29 30
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

Z
zhong_ning 已提交
31
#ifndef INIT_LOG_TAG
Z
zhong_ning 已提交
32
#define INIT_LOG_TAG "Init"
Z
zhong_ning 已提交
33 34
#endif

X
xionglei6 已提交
35
#ifndef INIT_LOG_DOMAIN
X
xionglei6 已提交
36
#define INIT_LOG_DOMAIN (BASE_DOMAIN + 1)
X
fix log  
xionglei6 已提交
37
#endif
S
sun_fan 已提交
38

C
cheng_jinsong 已提交
39 40
typedef void (*InitCommLog)(int logLevel, uint32_t domain, const char *tag, const char *fmt, va_list vargs);

M
Mupceet 已提交
41
INIT_LOCAL_API void OpenLogDevice(void);
M
Mupceet 已提交
42
INIT_LOCAL_API void InitLog(int logLevel, unsigned int domain, const char *tag, const char *fmt, va_list vargs);
C
cheng_jinsong 已提交
43 44 45
INIT_LOCAL_API void SetInitCommLog(InitCommLog logFunc);
INIT_LOCAL_API void EnableInitLog(InitLogLevel level);
INIT_LOCAL_API void EnableInitLogFromCmdline(void);
X
add ut  
xionglei6 已提交
46

A
an_xinwei 已提交
47 48 49 50 51 52 53
#ifdef PARAM_BASE
#define INIT_LOGV(fmt, ...)
#define INIT_LOGI(fmt, ...)
#define INIT_LOGW(fmt, ...)
#define INIT_LOGE(fmt, ...)
#define INIT_LOGF(fmt, ...)
#else
X
xionglei6 已提交
54
#define INIT_LOGV(fmt, ...) \
M
Mupceet 已提交
55
    StartupLog(INIT_DEBUG, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
X
xionglei6 已提交
56
#define INIT_LOGI(fmt, ...) \
M
Mupceet 已提交
57
    StartupLog(INIT_INFO, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
X
xionglei6 已提交
58
#define INIT_LOGW(fmt, ...) \
M
Mupceet 已提交
59
    StartupLog(INIT_WARN, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
X
xionglei6 已提交
60
#define INIT_LOGE(fmt, ...) \
M
Mupceet 已提交
61
    StartupLog(INIT_ERROR, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
X
xionglei6 已提交
62
#define INIT_LOGF(fmt, ...) \
M
Mupceet 已提交
63
    StartupLog(INIT_FATAL, INIT_LOG_DOMAIN, INIT_LOG_TAG, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__)
A
an_xinwei 已提交
64
#endif
X
xionglei6 已提交
65 66 67

#ifndef UNLIKELY
#define UNLIKELY(x)    __builtin_expect(!!(x), 0)
Z
zhong_ning 已提交
68
#endif
Z
zhong_ning 已提交
69

Z
zhong_ning 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83
#define INIT_ERROR_CHECK(ret, statement, format, ...) \
    do {                                                  \
        if (!(ret)) {                                     \
            INIT_LOGE(format, ##__VA_ARGS__);             \
            statement;                                    \
        }                                                 \
    } while (0)

#define INIT_INFO_CHECK(ret, statement, format, ...) \
    do {                                                  \
        if (!(ret)) {                                    \
            INIT_LOGI(format, ##__VA_ARGS__);            \
            statement;                                   \
        }                                          \
X
add ut  
xionglei6 已提交
84 85 86 87 88 89 90 91
    } while (0)

#define INIT_WARNING_CHECK(ret, statement, format, ...) \
    do {                                                  \
        if (!(ret)) {                                     \
            INIT_LOGW(format, ##__VA_ARGS__);             \
            statement;                                    \
        }                                                 \
Z
zhong_ning 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    } while (0)

#define INIT_CHECK(ret, statement) \
    do {                                \
        if (!(ret)) {                  \
            statement;                 \
        }                         \
    } while (0)

#define INIT_CHECK_RETURN_VALUE(ret, result) \
    do {                                \
        if (!(ret)) {                            \
            return result;                       \
        }                                  \
    } while (0)

#define INIT_CHECK_ONLY_RETURN(ret) \
    do {                                \
        if (!(ret)) {                   \
            return;                     \
        } \
    } while (0)
Z
zhong_ning 已提交
114

S
sun_fan 已提交
115 116 117 118 119 120 121
#define INIT_CHECK_ONLY_ELOG(ret, format, ...) \
    do {                                       \
        if (!(ret)) {                          \
            INIT_LOGE(format, ##__VA_ARGS__);  \
        } \
    } while (0)

Z
zhong_ning 已提交
122 123 124 125 126 127 128
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif

#endif // INIT_LOG_H