log.h 3.9 KB
Newer Older
O
overweight 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
 * clibcni licensed under the Mulan PSL v1.
 * You can use this software according to the terms and conditions of the Mulan PSL v1.
 * You may obtain a copy of Mulan PSL v1 at:
 *     http://license.coscl.org.cn/MulanPSL
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v1 for more details.
 * Author: wujing
 * Create: 2019-4-08
 * Description: provide container log functions
 ******************************************************************************/
#ifndef __CLIBCNI_LOG_H
#define __CLIBCNI_LOG_H

#include <stdbool.h>
#include <errno.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef O_CLOEXEC
#define O_CLOEXEC 02000000
#endif

#define CLIBCNI_LOG_BUFFER_SIZE 4096

L
LiuHao 已提交
31
#define CLIBCNI_LOG_TIME_STR_MAX_LEN 21
O
overweight 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

enum clibcni_log_drivers {
    LOG_DRIVER_STDOUT,
    LOG_DRIVER_FIFO,
    LOG_DRIVER_NOSET,
};

enum clibcni_log_level {
    CLIBCNI_LOG_FATAL = 0,
    CLIBCNI_LOG_ALERT,
    CLIBCNI_LOG_CRIT,
    CLIBCNI_LOG_ERROR,
    CLIBCNI_LOG_WARN,
    CLIBCNI_LOG_NOTICE,
    CLIBCNI_LOG_INFO,
    CLIBCNI_LOG_DEBUG,
    CLIBCNI_LOG_TRACE,
    CLIBCNI_LOG_MAX
};

struct clibcni_log_config {
    const char *name;
    const char *file;
    const char *priority;
    const char *prefix;
    const char *driver;
};

/* brief logging event object */
struct clibcni_log_object_metadata {
    /* location information of the logging item */
    const char *file;
    const char *func;
    int line;

    int level;
};

int clibcni_log_enable(const struct clibcni_log_config *log);

void clibcni_set_log_prefix(const char *prefix);

void clibcni_free_log_prefix(void);

L
LiuHao 已提交
76
int clibcni_log(const struct clibcni_log_object_metadata *metadata, const char *format, ...);
O
overweight 已提交
77

L
LiuHao 已提交
78 79 80 81 82 83
#define COMMON_LOG(loglevel, format, ...)                                               \
    do {                                                                                \
        struct clibcni_log_object_metadata meta = {                                     \
            .file = __FILENAME__, .func = __func__, .line = __LINE__, .level = loglevel,    \
        };                                                                              \
        (void)clibcni_log(&meta, format, ##__VA_ARGS__);                         \
O
overweight 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    } while (0)

#define DEBUG(format, ...)                                            \
    COMMON_LOG(CLIBCNI_LOG_DEBUG, format, ##__VA_ARGS__)

#define INFO(format, ...)                                             \
    COMMON_LOG(CLIBCNI_LOG_INFO, format, ##__VA_ARGS__)

#define NOTICE(format, ...)                                           \
    COMMON_LOG(CLIBCNI_LOG_NOTICE, format, ##__VA_ARGS__)

#define WARN(format, ...)                                             \
    COMMON_LOG(CLIBCNI_LOG_WARN, format, ##__VA_ARGS__)

#define ERROR(format, ...)                                            \
    COMMON_LOG(CLIBCNI_LOG_ERROR, format, ##__VA_ARGS__)

#define CRIT(format, ...)                                             \
    COMMON_LOG(CLIBCNI_LOG_CRIT, format, ##__VA_ARGS__)

#define ALERT(format, ...)                                            \
    COMMON_LOG(CLIBCNI_LOG_ALERT, format, ##__VA_ARGS__)

#define FATAL(format, ...)                                            \
    COMMON_LOG(CLIBCNI_LOG_FATAL, format, ##__VA_ARGS__)

#define SYSERROR(format, ...)                                  \
    do {                                                       \
        ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \
    } while (0)

#define COMMAND_ERROR(fmt, args...)              \
    do {                                         \
        (void)fprintf(stderr, fmt "\n", ##args); \
    } while (0)

#ifdef __cplusplus
}
#endif

L
LiuHao 已提交
124
#endif /* __CLIBCNI_LOG_H */