• C实现日志等级控制


     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <stdarg.h>
     5 #include <time.h>
     6 
     7 #define SJXC_LOG_DEBUG(level, format, arg...)     SJXC_log_msg_print(level, __FILE__, __func__, __LINE__, format, ##arg)
     8 
     9 #ifdef SJXC_DISABLE_DEBUG
    10 #define SJXC_REAL_DEBUG_LEVEL    0
    11 #else
    12 #define SJXC_REAL_DEBUG_LEVEL    SJXC_DEBUG_LEVEL
    13 #endif
    14 
    15 #define SJXC_ERROR   (1 << 0)
    16 #define SJXC_WARN    (1 << 1)
    17 #define SJXC_INFO    (1 << 2)
    18 #define SJXC_DEBUG   (1 << 3)
    19 
    20 int SJXC_DEBUG_LEVEL = 1;
    21 
    22 struct SJXC_gdb {
    23     int level;
    24     const char *msg;
    25 };
    26 
    27 struct SJXC_gdb SJXC_gdb_table[] = { 
    28     {SJXC_ERROR, "Config the log level as error."},
    29     {SJXC_WARN, "Config the log level as warn."},
    30     {SJXC_INFO, "Config the log level as info."},
    31     {SJXC_DEBUG, "Config the log level as DEBUG."}
    32 };
    33 
    34 void SJXC_print_debuf_usage(void)
    35 {
    36     struct SJXC_gdb *p_gdb;
    37     fprintf(stderr, 
    38             "    To calculate the debug level, logically 'or'
    "
    39             "    some of the following values together to get a bebug level:
    ");
    40     for (p_gdb = SJXC_gdb_table; p_gdb < SJXC_gdb_table + (sizeof(SJXC_gdb_table) / sizeof(struct SJXC_gdb)); p_gdb++) {
    41         fprintf(stderr, "	%d:	%s
    ", p_gdb->level, p_gdb->msg);
    42     }   
    43     fprintf(stderr, "
    ");
    44 }
    45 
    46 void SJXC_log_msg_print(int level, const char *file, const char *func, const int line, char *fmt, ...)
    47 {   
    48     char *status;
    49     
    50     switch (level) {
    51         case SJXC_ERROR:
    52             status = "ERROR";
    53             break;
    54         case SJXC_WARN:
    55             status = "WRAN";
    56             break;
    57         case SJXC_INFO:
    58             status = "INFO";
    59             break;
    60         case SJXC_DEBUG:
    61             status = "DEBUG";
    62             break;
    63         default:
    64             printf("Debug message level ERROR!
    ");
    65     }
    66     
    67     if (SJXC_REAL_DEBUG_LEVEL & level) {
    68         char msg_buf[20*1024];
    69         char *ascii_time_buf;
    70         time_t tnow = time(NULL);
    71         va_list ap;
    72         
    73         ascii_time_buf = asctime(localtime(&tnow));
    74         ascii_time_buf[strlen(ascii_time_buf) - 1] = '';
    75         
    76         va_start(ap, fmt);
    77         sprintf(msg_buf, "    ------> [ %s %s:%s:%d ] %s ", ascii_time_buf, file, func, line, status);
    78         vsprintf(msg_buf + strlen(msg_buf), fmt, ap);
    79         fprintf(stderr, "%s
    ", msg_buf);
    80         va_end(ap);
    81     }
    82 }
    83 
    84 #if 1
    85 int main(void)
    86 {
    87     int i = 0;
    88     printf("%d
    ", SJXC_REAL_DEBUG_LEVEL);
    89     SJXC_print_debuf_usage();
    90     while( i != 32) {
    91         SJXC_log_msg_print(SJXC_ERROR, __FILE__, __func__, __LINE__, "1234567890");
    92         i++;
    93     }
    94   return 0;
    95 }
    96 #endif
  • 相关阅读:
    深度学习(dropout)
    centos 常见软件安装
    最近读的书 与论文
    如何理解反向传播 Backpropagation 梯度下降算法要点
    ubuntu 15 安装cuda,开启GPU加速
    快速排序算法
    Linux网络中接收 "二进制" 流的那些事 --- 就recv的返回值和strlen库函数进行对话
    linux源码升级编译内核
    C/C++中慎用宏(#define)
    Qt之hello world
  • 原文地址:https://www.cnblogs.com/coolYuan/p/11597150.html
Copyright © 2020-2023  润新知