24 #include <stdarg.h> |
24 #include <stdarg.h> |
25 #include <stdlib.h> |
25 #include <stdlib.h> |
26 |
26 |
27 static int flib_loglevel = FLIB_LOGLEVEL_INFO; |
27 static int flib_loglevel = FLIB_LOGLEVEL_INFO; |
28 static FILE *flib_logfile = NULL; |
28 static FILE *flib_logfile = NULL; |
|
29 void (*flib_logCallback)(int level, const char *msg) = NULL; |
29 |
30 |
30 char* flib_format_ip(uint32_t numip) { |
31 char* flib_format_ip(uint32_t numip) { |
31 static char ip[16]; |
32 static char ip[16]; |
32 snprintf(ip, 16, "%u.%u.%u.%u", (unsigned)(numip>>24), (unsigned)((numip>>16)&0xff), (unsigned)((numip>>8)&0xff), (unsigned)(numip&0xff)); |
33 snprintf(ip, 16, "%u.%u.%u.%u", (unsigned)(numip>>24), (unsigned)((numip>>16)&0xff), (unsigned)((numip>>8)&0xff), (unsigned)(numip&0xff)); |
33 return ip; |
34 return ip; |
39 } else { |
40 } else { |
40 return flib_logfile; |
41 return flib_logfile; |
41 } |
42 } |
42 } |
43 } |
43 |
44 |
44 static void log_time() { |
45 static int log_time(char *buffer) { |
45 time_t timer; |
46 time_t timer; |
46 char buffer[25]; |
|
47 struct tm* tm_info; |
47 struct tm* tm_info; |
48 |
48 |
49 time(&timer); |
49 time(&timer); |
50 tm_info = localtime(&timer); |
50 tm_info = localtime(&timer); |
51 |
51 |
52 strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info); |
52 return strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info); |
53 fprintf(flib_log_getfile(), "%s", buffer); |
|
54 } |
53 } |
55 |
54 |
56 static const char *getPrefix(int level) { |
55 static char getPrefix(int level) { |
57 switch(level) { |
56 switch(level) { |
58 case FLIB_LOGLEVEL_ERROR: return "E"; |
57 case FLIB_LOGLEVEL_ERROR: return 'E'; |
59 case FLIB_LOGLEVEL_WARNING: return "W"; |
58 case FLIB_LOGLEVEL_WARNING: return 'W'; |
60 case FLIB_LOGLEVEL_INFO: return "I"; |
59 case FLIB_LOGLEVEL_INFO: return 'I'; |
61 case FLIB_LOGLEVEL_DEBUG: return "D"; |
60 case FLIB_LOGLEVEL_DEBUG: return 'D'; |
62 default: return "?"; |
61 default: return '?'; |
63 } |
62 } |
64 } |
63 } |
65 |
64 |
66 static void _flib_vflog(const char *func, int level, const char *fmt, va_list args) { |
65 static void _flib_vflog(const char *func, int level, const char *fmt, va_list args) { |
67 FILE *logfile = flib_log_getfile(); |
|
68 if(level >= flib_loglevel) { |
66 if(level >= flib_loglevel) { |
69 fprintf(logfile, "%s ", getPrefix(level)); |
67 char logbuffer[1024]; |
70 log_time(logfile); |
68 logbuffer[0] = getPrefix(level); |
71 fprintf(logfile, " [%-30s] ", func); |
69 logbuffer[1] = ' '; |
72 vfprintf(logfile, fmt, args); |
70 |
73 fprintf(logfile, "\n"); |
71 int pos = 2; |
74 fflush(logfile); |
72 |
|
73 int len = log_time(logbuffer+pos); |
|
74 if(len>=0) { |
|
75 pos += len; |
|
76 if(pos>sizeof(logbuffer)-1) pos = sizeof(logbuffer)-1; |
|
77 } else { |
|
78 return; |
|
79 } |
|
80 |
|
81 len = snprintf(logbuffer+pos, sizeof(logbuffer)-pos, " [%-30s] ", func); |
|
82 if(len>=0) { |
|
83 pos += len; |
|
84 if(pos>sizeof(logbuffer)-1) pos = sizeof(logbuffer)-1; |
|
85 } else { |
|
86 return; |
|
87 } |
|
88 |
|
89 len = vsnprintf(logbuffer+pos, sizeof(logbuffer)-pos, fmt, args); |
|
90 if(len>=0) { |
|
91 pos += len; |
|
92 if(pos>sizeof(logbuffer)-1) pos = sizeof(logbuffer)-1; |
|
93 } else { |
|
94 return; |
|
95 } |
|
96 |
|
97 if(flib_logCallback != NULL) { |
|
98 flib_logCallback(level, logbuffer); |
|
99 } else { |
|
100 FILE *logfile = flib_log_getfile(); |
|
101 fputs(logbuffer, logfile); |
|
102 fputc('\n', logfile); |
|
103 fflush(logfile); |
|
104 } |
75 } |
105 } |
76 } |
106 } |
77 |
107 |
78 void _flib_flog(const char *func, int level, const char *fmt, ...) { |
108 void _flib_flog(const char *func, int level, const char *fmt, ...) { |
79 va_list argp; |
109 va_list argp; |