3 changed files with 81 additions and 11 deletions
			
			
		@ -0,0 +1,25 @@ | 
				
			|||||
 | 
					#ifndef _LOG_H | 
				
			||||
 | 
					#define _LOG_H | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#include <time.h> | 
				
			||||
 | 
					#include <stdio.h> | 
				
			||||
 | 
					#include <stdarg.h> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					enum { | 
				
			||||
 | 
					    LOG_DEBUG, | 
				
			||||
 | 
					    LOG_INFO, | 
				
			||||
 | 
					    LOG_WARN, | 
				
			||||
 | 
					    LOG_ERROR, | 
				
			||||
 | 
					    LOG_FATAL | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#define log_debug(...) log_printf(LOG_DEBUG, __VA_ARGS__) | 
				
			||||
 | 
					#define log_info(...) log_printf(LOG_INFO,  __VA_ARGS__) | 
				
			||||
 | 
					#define log_warn(...) log_printf(LOG_WARN,  __VA_ARGS__) | 
				
			||||
 | 
					#define log_error(...) log_printf(LOG_ERROR, __VA_ARGS__) | 
				
			||||
 | 
					#define log_fatal(...) log_printf(LOG_FATAL, __VA_ARGS__) | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					extern int log_level; | 
				
			||||
 | 
					void log_printf(int level, const char *fmt, ...); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#endif | 
				
			||||
@ -0,0 +1,37 @@ | 
				
			|||||
 | 
					#include "log.h" | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					int log_level = LOG_DEBUG; // default log level
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					static const char *log_string[] = { | 
				
			||||
 | 
					    "[DEBUG]", | 
				
			||||
 | 
					    "[INFO]", | 
				
			||||
 | 
					    "[WARN]", | 
				
			||||
 | 
					    "[ERROR]", | 
				
			||||
 | 
					    "[FATAL]", | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					static const char *log_color[] = { | 
				
			||||
 | 
					    "\x1b[39m", // debug
 | 
				
			||||
 | 
					    "\x1b[32m", // info
 | 
				
			||||
 | 
					    "\x1b[33m", // warn
 | 
				
			||||
 | 
					    "\x1b[31m", // error
 | 
				
			||||
 | 
					    "\x1b[95m", // fatal
 | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					void log_printf(int level, const char *fmt, ...) { | 
				
			||||
 | 
					    if (level < log_level) { // skip low log level
 | 
				
			||||
 | 
					        return; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					    time_t t = time(NULL); | 
				
			||||
 | 
					    char time_str[20]; // YYYY-mm-dd HH:MM:SS (20 bytes)
 | 
				
			||||
 | 
					    time_str[strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", localtime(&t))] = '\0'; // generate time str
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    va_list ap; | 
				
			||||
 | 
					    va_start(ap, fmt); | 
				
			||||
 | 
					    fprintf(stderr, "\x1b[36m[Bootstrap]\x1b[0m \x1b[90m%s\x1b[0m", time_str); // show log prefix
 | 
				
			||||
 | 
					    fprintf(stderr, " %s%s\x1b[0m ", log_color[level], log_string[level]); // show log level
 | 
				
			||||
 | 
					    vfprintf(stderr, fmt, ap); // output log content
 | 
				
			||||
 | 
					    fprintf(stderr, "\n"); // add LF after line
 | 
				
			||||
 | 
					    fflush(stderr); | 
				
			||||
 | 
					    va_end(ap); | 
				
			||||
 | 
					} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue