project_files/frontlib/util/logging.c
changeset 7275 15f722e0b96f
parent 7224 5143861c83bd
child 7314 6171f0bad318
equal deleted inserted replaced
7273:8eed495fd8da 7275:15f722e0b96f
    32 
    32 
    33     strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info);
    33     strftime(buffer, 25, "%Y-%m-%d %H:%M:%S", tm_info);
    34     fprintf(flib_log_getfile(), "%s", buffer);
    34     fprintf(flib_log_getfile(), "%s", buffer);
    35 }
    35 }
    36 
    36 
    37 static void flib_vflog(const char *prefix, int level, const char *fmt, va_list args) {
    37 static const char *getPrefix(int level) {
       
    38 	switch(level) {
       
    39 	case FLIB_LOGLEVEL_ERROR: return "E";
       
    40 	case FLIB_LOGLEVEL_WARNING: return "W";
       
    41 	case FLIB_LOGLEVEL_INFO: return "I";
       
    42 	case FLIB_LOGLEVEL_DEBUG: return "D";
       
    43 	default: return "?";
       
    44 	}
       
    45 }
       
    46 
       
    47 static void _flib_vflog(const char *func, int level, const char *fmt, va_list args) {
    38 	FILE *logfile = flib_log_getfile();
    48 	FILE *logfile = flib_log_getfile();
    39 	if(level >= flib_loglevel) {
    49 	if(level >= flib_loglevel) {
    40 		fprintf(logfile, "%s ", prefix);
    50 		fprintf(logfile, "%s ", getPrefix(level));
    41 		log_time(logfile);
    51 		log_time(logfile);
    42 		fprintf(logfile, "  ", prefix);
    52 		fprintf(logfile, " [%-30s] ", func);
    43 		vfprintf(logfile, fmt, args);
    53 		vfprintf(logfile, fmt, args);
    44 		fprintf(logfile, "\n");
    54 		fprintf(logfile, "\n");
    45 		fflush(logfile);
    55 		fflush(logfile);
    46 	}
    56 	}
    47 }
    57 }
    48 
    58 
    49 void flib_log_e(const char *fmt, ...) {
    59 void _flib_flog(const char *func, int level, const char *fmt, ...) {
    50 	va_list argp;
    60 	va_list argp;
    51 	va_start(argp, fmt);
    61 	va_start(argp, fmt);
    52 	flib_vflog("E", FLIB_LOGLEVEL_ERROR, fmt, argp);
    62 	_flib_vflog(func, level, fmt, argp);
    53 	va_end(argp);
    63 	va_end(argp);
    54 }
    64 }
    55 
    65 
    56 void flib_log_w(const char *fmt, ...) {
    66 bool _flib_fassert(const char *func, int level, bool cond, const char *fmt, ...) {
    57 	va_list argp;
    67 	if(!cond) {
    58 	va_start(argp, fmt);
    68 		va_list argp;
    59 	flib_vflog("W", FLIB_LOGLEVEL_WARNING, fmt, argp);
    69 		va_start(argp, fmt);
    60 	va_end(argp);
    70 		_flib_vflog(func, level, fmt, argp);
       
    71 		va_end(argp);
       
    72 	}
       
    73 	return !cond;
    61 }
    74 }
    62 
    75 
    63 void flib_log_i(const char *fmt, ...) {
    76 bool _flib_assert_params(const char *func, bool cond) {
    64 	va_list argp;
    77 	return _flib_fassert(func, FLIB_LOGLEVEL_ERROR, cond, "Invalid parameter to function");
    65 	va_start(argp, fmt);
       
    66 	flib_vflog("I", FLIB_LOGLEVEL_INFO, fmt, argp);
       
    67 	va_end(argp);
       
    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 }
    78 }
    76 
    79 
    77 int flib_log_getLevel() {
    80 int flib_log_getLevel() {
    78 	return flib_loglevel;
    81 	return flib_loglevel;
    79 }
    82 }