log: make settings to be read from env #1
This commit is contained in:
parent
dd15119a08
commit
9c138ddaca
@ -1,9 +1,13 @@
|
||||
#define CDO_LOG_IMPLEMENTATION
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "cmd.h"
|
||||
#include "tools/log.h"
|
||||
|
||||
static void __attribute__((constructor)) init_cdo(void) {
|
||||
atexit(exit_cmd);
|
||||
init_log();
|
||||
init_cmd();
|
||||
}
|
||||
|
||||
|
||||
@ -75,6 +75,7 @@ void init_cmd(void) {
|
||||
LOG_ERROR("Undefined argument iterator %d", i);
|
||||
}
|
||||
}
|
||||
LOG_DEBUG("Cmd module initialised.")
|
||||
}
|
||||
|
||||
void exit_cmd(void) {
|
||||
|
||||
16
src/tools/log.c
Normal file
16
src/tools/log.c
Normal file
@ -0,0 +1,16 @@
|
||||
#include "log.h"
|
||||
|
||||
char* cdo_log_enable_color = NULL;
|
||||
char* cdo_log_enable_debug = NULL;
|
||||
|
||||
void init_log(void) {
|
||||
cdo_log_enable_color = getenv(LOG_COLORED_OUTPUT_ENV);
|
||||
cdo_log_enable_debug = getenv(LOG_DEBUG_MESSAGES_ENV);
|
||||
LOG_DEBUG("Log module initialised.");
|
||||
if (!cdo_log_enable_color) {
|
||||
LOG_DEBUG("%s env not defined.", LOG_COLORED_OUTPUT_ENV);
|
||||
}
|
||||
if (!cdo_log_enable_debug) {
|
||||
LOG_DEBUG("%s env not defined", LOG_DEBUG_MESSAGES_ENV);
|
||||
}
|
||||
}
|
||||
@ -5,34 +5,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LOG_COLORED_OUTPUT_ENV "CDO_LOG_ENABLE_COLOR"
|
||||
#define LOG_DEBUG_MESSAGES_ENV "CDO_LOG_ENABLE_DEBUG"
|
||||
|
||||
/* ================= Settings ================== */
|
||||
#define LOG_COLORED_OUTPUT
|
||||
#define HIGH_PRIORITY_DEBUG
|
||||
#define LOG_DEBUG_MESSAGES 0
|
||||
/* ================= Settings ================== */
|
||||
|
||||
// internal debug defines
|
||||
#ifdef HIGH_PRIORITY_DEBUG
|
||||
#include <assert.h>
|
||||
#define INT_ASSERT(statement) assert(statement)
|
||||
#else
|
||||
#define INT_ASSERT(statement)
|
||||
#endif
|
||||
|
||||
#ifdef LOG_COLORED_OUTPUT
|
||||
#define C_INFO "\x1b[34mINFO\x1b[0m"
|
||||
#define C_DEBUG "\x1b[35mDEBUG\x1b[0m"
|
||||
#define C_DEBUG_ISR "\x1b[36mDEBUG_ISR\x1b[0m"
|
||||
#define C_WARN "\x1b[33mWARNING\x1b[0m"
|
||||
#define C_ERROR "\x1b[31mERROR\x1b[0m"
|
||||
#else
|
||||
#define C_INFO "INFO"
|
||||
#define C_DEBUG "DEBUG"
|
||||
#define C_DEBUG_ISR "DEBUG_ISR"
|
||||
#define C_WARN "WARNING"
|
||||
#define C_ERROR "ERROR"
|
||||
#endif
|
||||
|
||||
typedef enum LogLevel {
|
||||
LEVEL_INFO,
|
||||
LEVEL_DEBUG,
|
||||
LEVEL_WARNING,
|
||||
LEVEL_ERROR
|
||||
} LogLevel;
|
||||
|
||||
static const char* coloredLogLevels[] = {
|
||||
"\x1b[34mINFO\x1b[0m",
|
||||
"\x1b[35mDEBUG\x1b[0m",
|
||||
"\x1b[33mWARNING\x1b[0m",
|
||||
"\x1b[31mERROR\x1b[0m"
|
||||
};
|
||||
|
||||
static const char* plainLogLevels[] = {
|
||||
"INFO",
|
||||
"DEBUG",
|
||||
"WARNING",
|
||||
"ERROR"
|
||||
};
|
||||
|
||||
extern char* cdo_log_enable_color;
|
||||
extern char* cdo_log_enable_debug;
|
||||
extern void init_log(void);
|
||||
|
||||
#define LOG_BUFFER_MAX_CAP 256
|
||||
#define LOG_MESSAGE_MAX_CAP 150
|
||||
|
||||
@ -82,23 +86,27 @@ static void create_log_line_simple(const char * _status,
|
||||
|
||||
}
|
||||
|
||||
#define GET_LOG_SEVERITY_STR(SEVERITY)( \
|
||||
(cdo_log_enable_color) ? coloredLogLevels[SEVERITY] : plainLogLevels[SEVERITY] \
|
||||
)
|
||||
|
||||
#define LOG_INFO( fmt, ...) \
|
||||
create_log_line(C_INFO, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
|
||||
create_log_line(GET_LOG_SEVERITY_STR(LEVEL_INFO), __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
|
||||
|
||||
#define LOG_WARNING(fmt, ...) \
|
||||
create_log_line(C_WARN, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
|
||||
create_log_line(GET_LOG_SEVERITY_STR(LEVEL_WARNING), __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
|
||||
|
||||
#define LOG_ERROR(fmt, ...) \
|
||||
create_log_line(C_ERROR, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__); \
|
||||
create_log_line(GET_LOG_SEVERITY_STR(LEVEL_ERROR), __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__); \
|
||||
exit(1);
|
||||
|
||||
#define LOG_ERROR_USER(fmt, ...) \
|
||||
create_log_line_simple(C_ERROR, fmt, ##__VA_ARGS__);
|
||||
create_log_line_simple(GET_LOG_SEVERITY_STR(LEVEL_ERROR), fmt, ##__VA_ARGS__);
|
||||
|
||||
#if LOG_DEBUG_MESSAGES
|
||||
#define LOG_DEBUG(fmt, ...) \
|
||||
create_log_line(C_DEBUG, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
|
||||
#endif
|
||||
create_log_line(GET_LOG_SEVERITY_STR(LEVEL_DEBUG), __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* __THREAD_COMMON_LOG_H_ */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user