mirror of https://github.com/dnomd343/ClearDNS
dnomd343
2 years ago
4 changed files with 79 additions and 0 deletions
@ -0,0 +1,22 @@ |
|||||
|
#ifndef _LOG_H |
||||
|
#define _LOG_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_perror(char *prefix); |
||||
|
void log_printf(int level, const char *fmt, ...); |
||||
|
|
||||
|
#endif |
@ -0,0 +1,57 @@ |
|||||
|
#include <time.h> |
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <stdarg.h> |
||||
|
#include "logger.h" |
||||
|
|
||||
|
int LOG_LEVEL = LOG_INFO; // 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); |
||||
|
|
||||
|
if (level == LOG_FATAL) { |
||||
|
exit(1); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void log_perror(char *prefix) { |
||||
|
time_t t; |
||||
|
time(&t); |
||||
|
struct tm *lt = localtime(&t); |
||||
|
fprintf(stderr, "\x1b[36m[Bootstrap]\x1b[0m "); |
||||
|
fprintf(stderr, "\x1b[90m%04d-%02d-%02d %02d:%02d:%02d\x1b[0m", |
||||
|
lt->tm_year + 1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec); |
||||
|
fprintf(stderr, " %s%s\x1b[0m ", log_color[3], log_string[3]); // error level
|
||||
|
fflush(stderr); |
||||
|
perror(prefix); |
||||
|
} |
Loading…
Reference in new issue