1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdarg.h>
5 #include <time.h>
6
7 #ifdef SJXC_DISABLE_DEBUG
8 #define SJXC_REAL_DEBUG_LEVEL 0
9 #else
10 #define SJXC_REAL_DEBUG_LEVEL SJXC_DEBUG_LEVEL
11 #endif
12
13 #define SJXC_ERROR (1 << 0)
14 #define SJXC_WARN (1 << 1)
15 #define SJXC_INFO (1 << 2)
16 #define SJXC_DEBUG (1 << 3)
17
18 int SJXC_DEBUG_LEVEL = 1;
19
20 struct SJXC_gdb {
21 int level;
22 const char *msg;
23 };
24
25 struct SJXC_gdb SJXC_gdb_table[] = {
26 {SJXC_ERROR, "Config the log level as error."},
27 {SJXC_WARN, "Config the log level as warn."},
28 {SJXC_INFO, "Config the log level as info."},
29 {SJXC_DEBUG, "Config the log level as DEBUG."}
30 };
31
32 void SJXC_print_debuf_usage(void)
33 {
34 struct SJXC_gdb *p_gdb;
35 fprintf(stderr,
36 " To calculate the debug level, logically 'or'\n"
37 " some of the following values together to get a bebug level:\n");
38 for (p_gdb = SJXC_gdb_table; p_gdb < SJXC_gdb_table + (sizeof(SJXC_gdb_table) / sizeof(struct SJXC_gdb)); p_gdb++) {
39 fprintf(stderr, "\t%d:\t%s\n", p_gdb->level, p_gdb->msg);
40 }
41 fprintf(stderr, "\n");
42 }
43
44 void SJXC_log_msg_print(int level, const char *file, const char *func, const int line, char *fmt, ...)
45 {
46 char *status;
47
48 switch (level) {
49 case SJXC_ERROR:
50 status = "ERROR";
51 break;
52 case SJXC_WARN:
53 status = "WRAN";
54 break;
55 case SJXC_INFO:
56 status = "INFO";
57 break;
58 case SJXC_DEBUG:
59 status = "DEBUG";
60 break;
61 default:
62 printf("Debug message level ERROR!\n");
63 }
64
65 if (SJXC_REAL_DEBUG_LEVEL & level) {
66 char msg_buf[20*1024];
67 char *ascii_time_buf;
68 time_t tnow = time(NULL);
69 va_list ap;
70
71 ascii_time_buf = asctime(localtime(&tnow));
72 ascii_time_buf[strlen(ascii_time_buf) - 1] = '\0';
73
74 va_start(ap, fmt);
75 sprintf(msg_buf, " ------> [ %s %s:%s:%d ] %s ", ascii_time_buf, file, func, line, status);
76 vsprintf(msg_buf + strlen(msg_buf), fmt, ap);
77 fprintf(stderr, "%s\n", msg_buf);
78 va_end(ap);
79 }
80 }
81
82 #if 1
83 int main(void)
84 {
85 int i = 0;
86 printf("%d\n", SJXC_REAL_DEBUG_LEVEL);
87 SJXC_print_debuf_usage();
88 while( i != 32) {
89 SJXC_log_msg_print(SJXC_ERROR, __FILE__, __func__, __LINE__, "1234567890");
90 i++;
91 }
92 return 0;
93 }
94 #endif
知识兔