feat: c: log colorized
This commit is contained in:
@@ -21,6 +21,20 @@ const char* log_level_to_string(LogLevel level) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char* log_level_to_color(LogLevel level) {
|
||||||
|
switch (level) {
|
||||||
|
case LOG_LEVEL_TRACE: return COLOR_GREEN;
|
||||||
|
case LOG_LEVEL_DEBUG: return COLOR_BLUE;
|
||||||
|
case LOG_LEVEL_LOG: return COLOR_CYAN;
|
||||||
|
case LOG_LEVEL_INFO: return COLOR_GREEN;
|
||||||
|
case LOG_LEVEL_NOTICE: return COLOR_CYAN;
|
||||||
|
case LOG_LEVEL_WARN: return COLOR_YELLOW;
|
||||||
|
case LOG_LEVEL_EXCEPTION: return COLOR_RED;
|
||||||
|
default: return COLOR_RESET;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
LogLevel log_level_from_string(const char *level_str) {
|
LogLevel log_level_from_string(const char *level_str) {
|
||||||
if (!level_str) return LOG_LEVEL_INFO;
|
if (!level_str) return LOG_LEVEL_INFO;
|
||||||
if (strcmp(level_str, "TRACE") == 0)
|
if (strcmp(level_str, "TRACE") == 0)
|
||||||
@@ -55,7 +69,13 @@ void init_logger(void) {
|
|||||||
current_log_level = log_level_from_string(getenv("LOG_LEVEL"));
|
current_log_level = log_level_from_string(getenv("LOG_LEVEL"));
|
||||||
}
|
}
|
||||||
|
|
||||||
char* raise_message(LogLevel level, const char *file, const char *func, int line, const char *format, ...) {
|
char* raise_message(
|
||||||
|
LogLevel level,
|
||||||
|
const char *file,
|
||||||
|
const char *func,
|
||||||
|
int line,
|
||||||
|
const char *format,
|
||||||
|
...) {
|
||||||
(void)func;
|
(void)func;
|
||||||
if (level < current_log_level) {
|
if (level < current_log_level) {
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -67,7 +87,7 @@ char* raise_message(LogLevel level, const char *file, const char *func, int line
|
|||||||
static char timeStr[20];
|
static char timeStr[20];
|
||||||
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &tm_info);
|
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &tm_info);
|
||||||
|
|
||||||
fprintf(stderr, "%s %s %s:%d ", timeStr, log_level_to_string(level), file, line);
|
fprintf(stderr, "%s %s%s%s %s:%d ", timeStr, log_level_to_color(level), log_level_to_string(level), COLOR_RESET, file, line);
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
|
|||||||
@@ -42,8 +42,16 @@ void set_output_color_mode(ColorMode mode);
|
|||||||
#define IS_TERMINAL() (isatty(fileno(stderr)))
|
#define IS_TERMINAL() (isatty(fileno(stderr)))
|
||||||
#define USE_COLOR() ((color_mode == COLOR_MODE_FORCE) || (color_mode == COLOR_MODE_AUTO && IS_TERMINAL()))
|
#define USE_COLOR() ((color_mode == COLOR_MODE_FORCE) || (color_mode == COLOR_MODE_AUTO && IS_TERMINAL()))
|
||||||
|
|
||||||
#define COLOR_RED (USE_COLOR() ? "\033[1;31m" : "")
|
#define COLOR_RED "\033[1;31m"
|
||||||
#define COLOR_RESET (USE_COLOR() ? "\033[0m" : "")
|
#define COLOR_GREEN "\033[1;32m"
|
||||||
|
#define COLOR_YELLOW "\033[1;33m"
|
||||||
|
#define COLOR_BLUE "\033[1;34m"
|
||||||
|
#define COLOR_MAGENTA "\033[1;35m"
|
||||||
|
#define COLOR_CYAN "\033[1;36m"
|
||||||
|
#define COLOR_WHITE "\033[1;37m"
|
||||||
|
#define COLOR_RESET "\033[0m"
|
||||||
|
|
||||||
|
#define OPTIONAL_COLOR(color) (USE_COLOR() ? color : "")
|
||||||
|
|
||||||
// ------------
|
// ------------
|
||||||
// -- Errors --
|
// -- Errors --
|
||||||
@@ -203,7 +211,7 @@ void* arena_realloc_copy__(const char *file, const char *func, int line, Arena *
|
|||||||
#define MEM_QiB (MEM_RiB * 1024)
|
#define MEM_QiB (MEM_RiB * 1024)
|
||||||
|
|
||||||
void substr_clone__(const char *file, const char *func, int line, const char * const src, char *dest, size_t from, size_t len);
|
void substr_clone__(const char *file, const char *func, int line, const char * const src, char *dest, size_t from, size_t len);
|
||||||
#define substr_clone(src, dest, from, len) substr_clone__(__FILE__, __LINE__, src, dest, from, len)
|
#define substr_clone(src, dest, from, len) substr_clone__(__FILE__, __func__, __LINE__, src, dest, from, len)
|
||||||
|
|
||||||
// ----------
|
// ----------
|
||||||
// -- Json --
|
// -- Json --
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ fi
|
|||||||
|
|
||||||
case "$MODE" in
|
case "$MODE" in
|
||||||
watch)
|
watch)
|
||||||
find . -type d | nix run .#watch -- 'sh ./make.sh build' -p '*.c' -p '*.h' 2>&1
|
find . -type d | nix run .#watch -- 'sh ./make.sh build && sh ./make.sh check' -p '*.c' -p '*.h' 2>&1
|
||||||
;;
|
;;
|
||||||
build)
|
build)
|
||||||
mkdir -p target
|
mkdir -p target
|
||||||
|
|||||||
Reference in New Issue
Block a user