Added logging system, but still not used

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

View File

@ -67,9 +67,6 @@ int main(int argc, char** argv) {
}
// TODO: Parse commands from the command line
char* container;
get_oldest_container(&container);
printf("Oldest container: %s\n", container);
}
int command_help(int argc, char* argv[]) {

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;
}

View File

@ -6,10 +6,40 @@
#define ERROR_BUFFER_SIZE 4096
/// @brief The global error buffer. It can be accessed using the error() function.
extern char _ERROR_BUFFER[];
/// @brief The level of importance of a log message.
typedef enum {
/// @brief A debug message. These messages are used for debugging purposes, and are not meant to be seen by the user.
LOG_LEVEL_DEBUG,
/// @brief An info message. These messages are used to inform the user about the progress of the program.
LOG_LEVEL_INFO,
/// @brief A warning message. These messages are used to warn the user about potential issues.
LOG_LEVEL_WARNING,
/// @brief An error message. These messages are used to inform the user that a critical error has occurred.
LOG_LEVEL_ERROR,
} log_level_t;
/// @brief The minimum log level of the program.
extern log_level_t _LOG_LEVEL;
/// @brief The result of an operation.
typedef int result_t;
/// @brief Sets the log level of the program.
/// @param level The log level to set.
void set_log_level(log_level_t level);
/// @brief Logs a message with the given level, and the given format string and arguments.
/// @param level The level of the message.
/// @param format The format string to use for the message.
/// @param ... The arguments to use for the format string.
void log_message(log_level_t level, const char* format, ...);
/// @brief Returns a generic success result.
/// @return The success result.
result_t success(void);