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] = '