From 4699cdd2d86efc1c81bcdbb3e3a042ee5de30f76 Mon Sep 17 00:00:00 2001 From: n67094 Date: Sun, 15 Oct 2023 17:57:08 +0200 Subject: [PATCH] fix: make sure string buffer is empty --- logger.h | 430 ++++++++++++++++++++++++++----------------------------- 1 file changed, 206 insertions(+), 224 deletions(-) diff --git a/logger.h b/logger.h index 857a9ce..ccfab45 100644 --- a/logger.h +++ b/logger.h @@ -2,8 +2,7 @@ #define LOGGER_H #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif #include "logger_config.h" @@ -21,290 +20,273 @@ extern "C" #include #include - static void LoggerInit(); - static int LoggerClose(); +static void LoggerInit(); +static int LoggerClose(); - static void LoggerSetLevel(int flags); - static void LoggerInfo(const char *msg, ...); - static void LoggerDebug(const char *msg, ...); - static void LoggerSuccess(const char *msg, ...); - static void LoggerWarning(const char *masg, ...); - static void LoggerError(const char *msg, ...); +static void LoggerSetLevel(int flags); +static void LoggerInfo(const char *msg, ...); +static void LoggerDebug(const char *msg, ...); +static void LoggerSuccess(const char *msg, ...); +static void LoggerWarning(const char *masg, ...); +static void LoggerError(const char *msg, ...); - static FILE *logger_file = NULL; +static FILE *logger_file = NULL; - static int logger_level = LOGGER_LEVEL_ALL; +static int logger_level = LOGGER_LEVEL_ALL; - /* - * used to know if we can write in file, set to 1 if the file can't be - * opened - */ - static int logger_file_error = 0; +/* + * used to know if we can write in file, set to 1 if the file can't be + * opened + */ +static int logger_file_error = 0; - static void LoggerReplaceSpace(char *pointer, const char character) - { - while (*pointer) { - if (*pointer == ' ') *pointer = character; +static void LoggerReplaceSpace(char *pointer, const char character) { + while (*pointer) { + if (*pointer == ' ') + *pointer = character; - pointer++; - } + pointer++; } +} - /* returned pointer should be freed */ - static char *LoggerGetTime() - { - char *date = NULL; - time_t current_date = time(NULL); +/* returned pointer should be freed */ +static char *LoggerGetTime() { + char *date = NULL; + time_t current_date = time(NULL); - if (current_date == (time_t)(-1)) { - date = - (char *)malloc(sizeof(char) * strlen(LOGGER_NO_TIME_PREFIX) + 1); - strcpy(date, LOGGER_NO_TIME_PREFIX); - } else { - int date_length = sizeof(char) * 20; - date = (char *)malloc(date_length); + if (current_date == (time_t)(-1)) { + date = (char *)malloc(sizeof(char) * strlen(LOGGER_NO_TIME_PREFIX) + 1); + strcpy(date, LOGGER_NO_TIME_PREFIX); + } else { + int date_length = sizeof(char) * 20; + date = (char *)malloc(date_length); - strftime( - date, date_length, "%Y-%m-%d %H:%M:%S", gmtime(¤t_date)); - - char *newline = strchr(date, '\n'); - if (newline != NULL) *newline = '\0'; - } + strftime(date, date_length, "%Y-%m-%d %H:%M:%S", gmtime(¤t_date)); - return date; + char *newline = strchr(date, '\n'); + if (newline != NULL) + *newline = '\0'; } - static int LoggerOpenFile() - { - char *date = LoggerGetTime(); + return date; +} - int filename_length = strlen(LOGGER_FILENAME); - int date_length = strlen(date); +static int LoggerOpenFile() { + char *date = LoggerGetTime(); - /* + 1 for the - between filename and date */ - char *full_filename = - (char *)malloc(sizeof(char) * (filename_length + date_length + 1)); + int filename_length = strlen(LOGGER_FILENAME); + int date_length = strlen(date); - strcat(full_filename, LOGGER_FILENAME); - strcat(full_filename, "-"); - strcat(full_filename, date); + /* + 1 for the - between filename and date and + 1 for /0 */ + char *full_filename = + (char *)malloc(sizeof(char) * (filename_length + date_length + 2)); + *full_filename = '\0'; - LoggerReplaceSpace(full_filename, '_'); + strcat(full_filename, LOGGER_FILENAME); + strcat(full_filename, "-"); + strcat(full_filename, date); + strcat(full_filename, "\0"); - logger_file = fopen(full_filename, "a+"); + LoggerReplaceSpace(full_filename, '_'); - if (logger_file == NULL) { - printf("log.c error opening file %s", full_filename); - logger_file_error = 1; - return 1; - } + logger_file = fopen(full_filename, "a+"); - free(full_filename); - full_filename = NULL; + if (logger_file == NULL) { + printf("log.c error opening file %s", full_filename); + logger_file_error = 1; + return 1; + } - free(date); - date = NULL; + free(full_filename); + full_filename = NULL; - return 0; - }; + free(date); + date = NULL; - static int LoggerCloseFile() - { - if (logger_file != NULL) { return fclose(logger_file); } + return 0; +}; - return 0; +static int LoggerCloseFile() { + if (logger_file != NULL) { + return fclose(logger_file); } - static void LoggerInFile( - int msg_length, - const char *date, - const char *prefix, - const char *msg, - va_list args) - { - /* + 1 for \n added below */ - char *full_msg = (char *)malloc(sizeof(char) * (msg_length + 1) + 1); - *full_msg = '\0'; - - strcat(full_msg, date); - strcat(full_msg, prefix); - strcat(full_msg, msg); - strcat(full_msg, "\n"); - strcat(full_msg, "\0"); - - vfprintf(logger_file, full_msg, args); - - free(full_msg); - full_msg = NULL; - } + return 0; +} - static void LoggerInConsole( - int msg_length, - const char *date, - const char *color, - const char *prefix, - const char *msg, - va_list args) - { - char *full_msg = (char *)malloc(sizeof(char) * (msg_length + 1) + 1); - *full_msg = '\0'; +static void LoggerInFile(int msg_length, const char *date, const char *prefix, + const char *msg, va_list args) { + /* + 2 for \n and \0 added below */ + char *full_msg = (char *)malloc(sizeof(char) * (msg_length + 2)); + *full_msg = '\0'; - if (LOGGER_WITH_COLOR) strcat(full_msg, LOGGER_FAINT); + strcat(full_msg, date); + strcat(full_msg, prefix); + strcat(full_msg, msg); + strcat(full_msg, "\n"); + strcat(full_msg, "\0"); - strcat(full_msg, date); + vfprintf(logger_file, full_msg, args); - if (LOGGER_WITH_COLOR) strcat(full_msg, LOGGER_RESET_STYLE); + free(full_msg); + full_msg = NULL; +} - if (LOGGER_WITH_COLOR) strcat(full_msg, color); +static void LoggerInConsole(int msg_length, const char *date, const char *color, + const char *prefix, const char *msg, va_list args) { + /* + 2 for \n and \0 added below */ + char *full_msg = (char *)malloc(sizeof(char) * (msg_length + 2)); + *full_msg = '\0'; - strcat(full_msg, prefix); + if (LOGGER_WITH_COLOR) + strcat(full_msg, LOGGER_FAINT); - if (LOGGER_WITH_COLOR) strcat(full_msg, LOGGER_RESET_STYLE); + strcat(full_msg, date); - strcat(full_msg, msg); - strcat(full_msg, "\n"); - strcat(full_msg, "\0"); + if (LOGGER_WITH_COLOR) + strcat(full_msg, LOGGER_RESET_STYLE); - vprintf(full_msg, args); + if (LOGGER_WITH_COLOR) + strcat(full_msg, color); - free(full_msg); - full_msg = NULL; - } + strcat(full_msg, prefix); - /* - * Since va_copy is only available in c99 I use two va_list, va_list are - * invalidated by vprintf and vfprintf so it cannot be used twice - */ - static void Logger( - const char *color, - const char *msg, - const char *prefix, - va_list v_list_1, - va_list v_list_2) - { - int msg_length = strlen(msg); - int prefix_length = strlen(prefix); - - char *date = LoggerGetTime(); - int date_length = strlen(date); - - if (LOGGER_IN_FILE && logger_file_error != 1) { - int full_msg_length = date_length + prefix_length + msg_length; - - LoggerInFile(full_msg_length, date, prefix, msg, v_list_1); - } + if (LOGGER_WITH_COLOR) + strcat(full_msg, LOGGER_RESET_STYLE); - if (LOGGER_IN_CONSOLE) { - int full_msg_length = 0; + strcat(full_msg, msg); + strcat(full_msg, "\n"); + strcat(full_msg, "\0"); - if (LOGGER_WITH_COLOR == 1) { - int faint_style_length = strlen(LOGGER_FAINT); - int color_length = strlen(color); - int reset_style_length = strlen(LOGGER_RESET_STYLE); + vprintf(full_msg, args); - full_msg_length = faint_style_length + date_length + - reset_style_length + color_length + - prefix_length + reset_style_length + msg_length; - } else { - full_msg_length = date_length + prefix_length + msg_length; - } + free(full_msg); + full_msg = NULL; +} - LoggerInConsole(full_msg_length, date, color, prefix, msg, v_list_2); - } +/* + * Since va_copy is only available in c99 I use two va_list, va_list are + * invalidated by vprintf and vfprintf so it cannot be used twice + */ +static void Logger(const char *color, const char *msg, const char *prefix, + va_list v_list_1, va_list v_list_2) { + int msg_length = strlen(msg); + int prefix_length = strlen(prefix); - free(date); - date = NULL; - }; + char *date = LoggerGetTime(); + int date_length = strlen(date); - void LoggerInit() - { - if (LOGGER_IN_FILE) { LoggerOpenFile(); } - LoggerInfo(LOGGER_INIT_MSG); - } + if (LOGGER_IN_FILE && logger_file_error != 1) { + int full_msg_length = date_length + prefix_length + msg_length; - void LoggerSetLevel(int flags) { logger_level = flags; } + LoggerInFile(full_msg_length, date, prefix, msg, v_list_1); + } - void LoggerInfo(const char *msg, ...) - { - if (logger_level & LOGGER_LEVEL_INFO) { - va_list v_list_1; - va_list v_list_2; - va_start(v_list_1, msg); - va_start(v_list_2, msg); + if (LOGGER_IN_CONSOLE) { + int full_msg_length = 0; - Logger(LOGGER_BLUE, msg, LOGGER_INFO_PREFIX, v_list_1, v_list_2); + if (LOGGER_WITH_COLOR == 1) { + int faint_style_length = strlen(LOGGER_FAINT); + int color_length = strlen(color); + int reset_style_length = strlen(LOGGER_RESET_STYLE); - va_end(v_list_1); - va_end(v_list_2); + full_msg_length = faint_style_length + date_length + reset_style_length + + color_length + prefix_length + reset_style_length + + msg_length; + } else { + full_msg_length = date_length + prefix_length + msg_length; } - }; - void LoggerDebug(const char *msg, ...) - { - if (logger_level & LOGGER_LEVEL_DEBUG) { - va_list v_list_1; - va_list v_list_2; - va_start(v_list_1, msg); - va_start(v_list_2, msg); + LoggerInConsole(full_msg_length, date, color, prefix, msg, v_list_2); + } - Logger(LOGGER_MAGENTA, msg, LOGGER_DEBUG_PREFIX, v_list_1, v_list_2); + free(date); + date = NULL; +}; - va_end(v_list_1); - va_end(v_list_2); - } - }; +void LoggerInit() { + if (LOGGER_IN_FILE) { + LoggerOpenFile(); + } + LoggerInfo(LOGGER_INIT_MSG); +} - void LoggerSuccess(const char *msg, ...) - { - if (logger_level & LOGGER_LEVEL_SUCCESS) { - va_list v_list_1; - va_list v_list_2; - va_start(v_list_1, msg); - va_start(v_list_2, msg); +void LoggerSetLevel(int flags) { logger_level = flags; } - Logger(LOGGER_GREEN, msg, LOGGER_SUCCESS_PREFIX, v_list_1, v_list_2); +void LoggerInfo(const char *msg, ...) { + if (logger_level & LOGGER_LEVEL_INFO) { + va_list v_list_1; + va_list v_list_2; + va_start(v_list_1, msg); + va_start(v_list_2, msg); - va_end(v_list_1); - va_end(v_list_2); - } - }; + Logger(LOGGER_BLUE, msg, LOGGER_INFO_PREFIX, v_list_1, v_list_2); - void LoggerWarning(const char *msg, ...) - { - if (logger_level & LOGGER_LEVEL_WARNING) { - va_list v_list_1; - va_list v_list_2; - va_start(v_list_1, msg); - va_start(v_list_2, msg); + va_end(v_list_1); + va_end(v_list_2); + } +}; - Logger( - LOGGER_YELLOW, msg, LOGGER_WARNING_PREFIX, v_list_1, v_list_2); +void LoggerDebug(const char *msg, ...) { + if (logger_level & LOGGER_LEVEL_DEBUG) { + va_list v_list_1; + va_list v_list_2; + va_start(v_list_1, msg); + va_start(v_list_2, msg); - va_end(v_list_1); - va_end(v_list_2); - } - }; + Logger(LOGGER_MAGENTA, msg, LOGGER_DEBUG_PREFIX, v_list_1, v_list_2); + + va_end(v_list_1); + va_end(v_list_2); + } +}; - void LoggerError(const char *msg, ...) - { - if (logger_level & LOGGER_LEVEL_ERROR) { - va_list v_list_1; - va_list v_list_2; - va_start(v_list_1, msg); - va_start(v_list_2, msg); +void LoggerSuccess(const char *msg, ...) { + if (logger_level & LOGGER_LEVEL_SUCCESS) { + va_list v_list_1; + va_list v_list_2; + va_start(v_list_1, msg); + va_start(v_list_2, msg); - Logger(LOGGER_RED, msg, LOGGER_ERROR_PREFIX, v_list_1, v_list_2); + Logger(LOGGER_GREEN, msg, LOGGER_SUCCESS_PREFIX, v_list_1, v_list_2); - va_end(v_list_1); - va_end(v_list_2); - } - }; + va_end(v_list_1); + va_end(v_list_2); + } +}; + +void LoggerWarning(const char *msg, ...) { + if (logger_level & LOGGER_LEVEL_WARNING) { + va_list v_list_1; + va_list v_list_2; + va_start(v_list_1, msg); + va_start(v_list_2, msg); + + Logger(LOGGER_YELLOW, msg, LOGGER_WARNING_PREFIX, v_list_1, v_list_2); - int LoggerClose() - { - LoggerInfo(LOGGER_CLOSE_MSG); - return LoggerCloseFile(); + va_end(v_list_1); + va_end(v_list_2); } +}; + +void LoggerError(const char *msg, ...) { + if (logger_level & LOGGER_LEVEL_ERROR) { + va_list v_list_1; + va_list v_list_2; + va_start(v_list_1, msg); + va_start(v_list_2, msg); + + Logger(LOGGER_RED, msg, LOGGER_ERROR_PREFIX, v_list_1, v_list_2); + + va_end(v_list_1); + va_end(v_list_2); + } +}; + +int LoggerClose() { + LoggerInfo(LOGGER_CLOSE_MSG); + return LoggerCloseFile(); +} #ifdef __cplusplus }