Added logging system, but still not used

This commit is contained in:
2024-02-20 15:00:17 +01:00
parent af34a9c260
commit 6eaa10e3d3
3 changed files with 88 additions and 3 deletions

View File

@@ -14,6 +14,64 @@
char _ERROR_BUFFER[ERROR_BUFFER_SIZE];
log_level_t _LOG_LEVEL = LOG_LEVEL_INFO;
void set_log_level(log_level_t level) {
_LOG_LEVEL = level;
}
void log_message(log_level_t level, const char* format, ...) {
// Check that the log level is high enough
if (level < _LOG_LEVEL)
return;
// Convert the log level to a color and a string
const char* color_str;
const char* level_str;
switch (level) {
case LOG_LEVEL_DEBUG:
color_str = "\033[0m";
level_str = "DEBUG";
break;
case LOG_LEVEL_INFO:
color_str = "\033[36m";
level_str = "INFO";
break;
case LOG_LEVEL_WARNING:
color_str = "\033[33m";
level_str = "WARNING";
break;
case LOG_LEVEL_ERROR:
color_str = "\033[31m";
level_str = "ERROR";
break;
default:
color_str = "\033[0m";
level_str = "UNKNOWN";
break;
}
// Get the current time
time_t t = time(NULL);
struct tm* tm = localtime(&t);
// Print the log message
fprintf(stderr, "%s[%02d/%02d/%04d %02d:%02d:%02d - %s]\033[0m ", color_str, tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec, level_str);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fprintf(stderr, "\n");
// Flush the standard error stream
fflush(stderr);
}
result_t success(void) {
return EXIT_SUCCESS;
}