I'm currently trying to make a C library that would allow me to make any kind of log i want in my future projets.
In order to do it i've set few thing such as colors, bold ...
#define RESET "\033[0m"
#define BOLD(msg) "\033[1m" msg RESET
#define BLINK(msg) "\033[5m" msg RESET
#define YELLOW(msg) "\033[38;5;208m" msg RESET
#define ORANGE(msg) "\033[38;5;208m" msg RESET
#define RED(msg) "\033[38;5;196m" msg RESET
#define BLUE(msg) "\033[38;5;27m" msg RESET
#define GREEN(msg) "\033[38;5;46m" msg RESET
#define PURPLE(msg) "\033[38;5;164m" msg RESET
#define NULL_STR ""
#define INFO_STR "[" BOLD(YELLOW("INFO")) "] "
#define WARN_STR "[" BOLD(ORANGE("WARNING")) "] "
#define ERROR_STR "[" BLINK(BOLD(RED("ERROR"))) "] "
#define DEBUG_STR "[" BOLD(BLUE("DEBUG")) "] "
#define UNKNOWN_TYPE_STR "[" BOLD(PURPLE("UNKNOWN TYPE")) "] "
In order to build the string berfore printing it, i've set an array of my string that is use with an enum :
typedef enum log_type_e {
NONE = 0,
INFO = 1,
WARN = 2,
ERRO = 4,
DEBUG = 8
} log_type_t;
// I put both of my tries they lead to the same issue
static const char (*LOG_TYPE_STR)[] = {NULL_STR, INFO_STR, WARN_STR, UNKNOWN_TYPE_STR, ERROR_STR, UNKNOWN_TYPE_STR, UNKNOWN_TYPE_STR, UNKNOWN_TYPE_STR, DEBUG_STR};
//the 9 has to be set or it lead to an error
static const char LOG_TYPE_STR[][9] = {NULL_STR, INFO_STR, WARN_STR, UNKNOWN_TYPE_STR, ERROR_STR, UNKNOWN_TYPE_STR, UNKNOWN_TYPE_STR, UNKNOWN_TYPE_STR, DEBUG_STR};
When i compile a program that use the LOG_TYPE_STR array, i got this warning a lot of times :
initializer-string for array of chars is too long
16 | #define INFO_STR "[" BOLD(YELLOW("INFO")) "] "
If anyone could help me to understand an fix it or if anyone know how i could build my strings dynamical this would be really appreciate. Thanks
static const char LOG_TYPE_STR[][9]allows only8characters in a nul-terminated string, but your macros are building strings that are longer.INFO_STRin the error message is used as an initialiser, but even theYELLOWpart is too long on its own.YELLOWadds 15 characters to a string.BOLDadds 8 characters. SoBOLD(YELLOW("INFO"))is 27 characters long (plus 1 for the null terminator).