frontlib/logging.c
changeset 7158 a0573014ff4f
parent 7155 273ad375d64e
equal deleted inserted replaced
7155:273ad375d64e 7158:a0573014ff4f
     1 #include "logging.h"
     1 #include "logging.h"
       
     2 
       
     3 #include <time.h>
       
     4 #include <stdio.h>
       
     5 #include <stdarg.h>
     2 
     6 
     3 char* flib_format_ip(uint32_t numip) {
     7 char* flib_format_ip(uint32_t numip) {
     4 	static char ip[16];
     8 	static char ip[16];
     5 	snprintf(ip, 16, "%u.%u.%u.%u", numip>>24, (numip>>16)&0xff, (numip>>8)&0xff, numip&0xff);
     9 	snprintf(ip, 16, "%u.%u.%u.%u", numip>>24, (numip>>16)&0xff, (numip>>8)&0xff, numip&0xff);
     6 	return ip;
    10 	return ip;
     7 }
    11 }
       
    12 
       
    13 static void log_time(FILE *file) {
       
    14     time_t timer;
       
    15     char buffer[25];
       
    16     struct tm* tm_info;
       
    17 
       
    18     time(&timer);
       
    19     tm_info = localtime(&timer);
       
    20 
       
    21     strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info);
       
    22     fprintf(file, "%s", buffer);
       
    23 }
       
    24 
       
    25 static void flib_vflog(FILE *file, const char *prefix, const char *fmt, va_list args) {
       
    26 	log_time(file);
       
    27 	fprintf(file, " [%s]", prefix);
       
    28 	vfprintf(file, fmt, args);
       
    29 	fprintf(file, "\n");
       
    30 }
       
    31 
       
    32 void flib_log_e(const char *fmt, ...) {
       
    33 	va_list argp;
       
    34 	va_start(argp, fmt);
       
    35 	flib_vflog(stderr, "E", fmt, argp);
       
    36 	va_end(argp);
       
    37 }
       
    38 
       
    39 void flib_log_w(const char *fmt, ...) {
       
    40 	va_list argp;
       
    41 	va_start(argp, fmt);
       
    42 	flib_vflog(stdout, "W", fmt, argp);
       
    43 	va_end(argp);
       
    44 }
       
    45 
       
    46 void flib_log_i(const char *fmt, ...) {
       
    47 	va_list argp;
       
    48 	va_start(argp, fmt);
       
    49 	flib_vflog(stdout, "I", fmt, argp);
       
    50 	va_end(argp);
       
    51 }