0

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

2
  • The definition static const char LOG_TYPE_STR[][9] allows only 8 characters in a nul-terminated string, but your macros are building strings that are longer. INFO_STR in the error message is used as an initialiser, but even the YELLOW part is too long on its own. Commented Jun 23, 2020 at 21:09
  • YELLOW adds 15 characters to a string. BOLD adds 8 characters. So BOLD(YELLOW("INFO")) is 27 characters long (plus 1 for the null terminator). Commented Jun 23, 2020 at 21:11

1 Answer 1

2

Your string size 9 doesn't allow room for all the escape sequences and other wrapper characters. INFO_STR needs 30 bytes, UNKNOWN_TYPE_STR needs 39 bytes.

You should be able to do it with the array of pointers to string literals. You just got the syntax a little wrong. Get rid of the parentheses.

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};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.