project_files/frontlib/util/logging.c
changeset 7182 076aba32abd3
parent 7179 f84805e6df03
child 7224 5143861c83bd
equal deleted inserted replaced
7179:f84805e6df03 7182:076aba32abd3
     3 #include <time.h>
     3 #include <time.h>
     4 #include <stdio.h>
     4 #include <stdio.h>
     5 #include <stdarg.h>
     5 #include <stdarg.h>
     6 #include <stdlib.h>
     6 #include <stdlib.h>
     7 
     7 
       
     8 static int flib_loglevel = FLIB_LOGLEVEL_INFO;
       
     9 static FILE *flib_logfile = NULL;
       
    10 
     8 char* flib_format_ip(uint32_t numip) {
    11 char* flib_format_ip(uint32_t numip) {
     9 	static char ip[16];
    12 	static char ip[16];
    10 	snprintf(ip, 16, "%u.%u.%u.%u", (unsigned)(numip>>24), (unsigned)((numip>>16)&0xff), (unsigned)((numip>>8)&0xff), (unsigned)(numip&0xff));
    13 	snprintf(ip, 16, "%u.%u.%u.%u", (unsigned)(numip>>24), (unsigned)((numip>>16)&0xff), (unsigned)((numip>>8)&0xff), (unsigned)(numip&0xff));
    11 	return ip;
    14 	return ip;
    12 }
    15 }
    13 
    16 
    14 static void log_time(FILE *file) {
    17 static inline FILE *flib_log_getfile() {
       
    18 	if(flib_logfile==NULL) {
       
    19 		return stdout;
       
    20 	} else {
       
    21 		return flib_logfile;
       
    22 	}
       
    23 }
       
    24 
       
    25 static void log_time() {
    15     time_t timer;
    26     time_t timer;
    16     char buffer[25];
    27     char buffer[25];
    17     struct tm* tm_info;
    28     struct tm* tm_info;
    18 
    29 
    19     time(&timer);
    30     time(&timer);
    20     tm_info = localtime(&timer);
    31     tm_info = localtime(&timer);
    21 
    32 
    22     strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info);
    33     strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info);
    23     fprintf(file, "%s", buffer);
    34     fprintf(flib_log_getfile(), "%s", buffer);
    24 }
    35 }
    25 
    36 
    26 static void flib_vflog(FILE *file, const char *prefix, const char *fmt, va_list args) {
    37 static void flib_vflog(const char *prefix, int level, const char *fmt, va_list args) {
    27 	log_time(file);
    38 	FILE *logfile = flib_log_getfile();
    28 	fprintf(file, " [%s]", prefix);
    39 	if(level >= flib_loglevel) {
    29 	vfprintf(file, fmt, args);
    40 		fprintf(logfile, "%s ", prefix);
    30 	fprintf(file, "\n");
    41 		log_time(logfile);
    31 	fflush(file);
    42 		fprintf(logfile, "  ", prefix);
       
    43 		vfprintf(logfile, fmt, args);
       
    44 		fprintf(logfile, "\n");
       
    45 		fflush(logfile);
       
    46 	}
    32 }
    47 }
    33 
    48 
    34 void flib_log_e(const char *fmt, ...) {
    49 void flib_log_e(const char *fmt, ...) {
    35 	va_list argp;
    50 	va_list argp;
    36 	va_start(argp, fmt);
    51 	va_start(argp, fmt);
    37 	flib_vflog(stderr, "E", fmt, argp);
    52 	flib_vflog("E", FLIB_LOGLEVEL_ERROR, fmt, argp);
    38 	va_end(argp);
    53 	va_end(argp);
    39 }
    54 }
    40 
    55 
    41 void flib_log_w(const char *fmt, ...) {
    56 void flib_log_w(const char *fmt, ...) {
    42 	va_list argp;
    57 	va_list argp;
    43 	va_start(argp, fmt);
    58 	va_start(argp, fmt);
    44 	flib_vflog(stdout, "W", fmt, argp);
    59 	flib_vflog("W", FLIB_LOGLEVEL_WARNING, fmt, argp);
    45 	va_end(argp);
    60 	va_end(argp);
    46 }
    61 }
    47 
    62 
    48 void flib_log_i(const char *fmt, ...) {
    63 void flib_log_i(const char *fmt, ...) {
    49 	va_list argp;
    64 	va_list argp;
    50 	va_start(argp, fmt);
    65 	va_start(argp, fmt);
    51 	flib_vflog(stdout, "I", fmt, argp);
    66 	flib_vflog("I", FLIB_LOGLEVEL_INFO, fmt, argp);
    52 	va_end(argp);
    67 	va_end(argp);
    53 }
    68 }
       
    69 
       
    70 void flib_log_d(const char *fmt, ...) {
       
    71 	va_list argp;
       
    72 	va_start(argp, fmt);
       
    73 	flib_vflog("D", FLIB_LOGLEVEL_DEBUG, fmt, argp);
       
    74 	va_end(argp);
       
    75 }
       
    76 
       
    77 int flib_log_getLevel() {
       
    78 	return flib_loglevel;
       
    79 }
       
    80 
       
    81 void flib_log_setLevel(int level) {
       
    82 	flib_loglevel = level;
       
    83 }
       
    84 
       
    85 void flib_log_setFile(FILE *file) {
       
    86 	flib_logfile = file;
       
    87 }