From 6eaa10e3d3fe97026cedd6f8f9c26487c2050761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Tue, 20 Feb 2024 15:00:17 +0100 Subject: [PATCH] Added logging system, but still not used --- src/sandbox.c | 3 --- src/utils.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 30 ++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/sandbox.c b/src/sandbox.c index a4d323b..582fe7f 100755 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -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[]) { diff --git a/src/utils.c b/src/utils.c index 930c4f1..d73b190 100755 --- a/src/utils.c +++ b/src/utils.c @@ -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; } diff --git a/src/utils.h b/src/utils.h index dd65736..0aeb173 100755 --- a/src/utils.h +++ b/src/utils.h @@ -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);