From d501a16ba2f43c90aaf550aaac5446974a53d3de Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 10 Sep 2022 18:22:50 +0800 Subject: [PATCH] feat: add logger function --- include/{ => utils}/cJSON.h | 0 include/utils/logger.h | 22 ++++++++++++++ src/{ => utils}/cJSON.c | 0 src/utils/logger.c | 57 +++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) rename include/{ => utils}/cJSON.h (100%) create mode 100644 include/utils/logger.h rename src/{ => utils}/cJSON.c (100%) create mode 100644 src/utils/logger.c diff --git a/include/cJSON.h b/include/utils/cJSON.h similarity index 100% rename from include/cJSON.h rename to include/utils/cJSON.h diff --git a/include/utils/logger.h b/include/utils/logger.h new file mode 100644 index 0000000..48ecc04 --- /dev/null +++ b/include/utils/logger.h @@ -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 diff --git a/src/cJSON.c b/src/utils/cJSON.c similarity index 100% rename from src/cJSON.c rename to src/utils/cJSON.c diff --git a/src/utils/logger.c b/src/utils/logger.c new file mode 100644 index 0000000..73b68b5 --- /dev/null +++ b/src/utils/logger.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#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); +}