Mescal
Loading...
Searching...
No Matches
error.h File Reference

Macros for displaying error messages. More...

Go to the source code of this file.

Macros

#define ATTENTION   "\xe2\x9d\x97"
 Symbole ❗.
 
#define HINT   "\xe2\x9c\xa8"
 Symbole ✨.
 
#define OK   "\xE2\x9C\x85"
 Symbole ✅.
 
#define KO   "\xE2\x9D\x8C"
 Symbole ❌.
 
#define DEBUG_LEVEL   INFO
 Debug level, INFO by default.
 
#define PRINT_ERROR(symbol, name, function, file, line, msg, ...)
 Displays an error message on the error output.
 
#define FATAL(msg, ...)
 Prints a FATAL message and end the program.
 
#define PRINT_DEBUG(symbol, name, level, msg, ...)
 
#define TRACE(msg, ...)
 Prints a TRACE message.
 
#define DEBUG(msg, ...)
 Print a DEBUG message.
 
#define INFO(msg, ...)
 Print an INFO message.
 
#define WARNING(msg, ...)
 Print a WARNING message.
 
#define ERROR(msg, ...)
 Print an ERROR message.
 
#define CRITICAL(msg, ...)
 Print a CRITICAL message.
 

Enumerations

enum  debug_level {
  OFF , CRITICAL , ERROR , WARNING ,
  INFO , DEBUG , TRACE
}
 
enum  msg_level {
  CRITICAL_L , ERROR_L , WARNING_L , INFO_L ,
  DEBUG_L , TRACE_L
}
 

Detailed Description

Macros for displaying error messages.

This file provides the macros TRACE(), DEBUG(), INFO(), WARNING(), ERROR(), CRITICAL(), and FATAL(), which allow displaying error messages on the error output, and, for the FATAL() macro, terminating the program. Error messages are displayed with the function name, file name, and line number where the macro call is made.

The other macros in the file are primarily intended to implement the TRACE(), DEBUG(), INFO(), WARNING(), ERROR(), CRITICAL(), and FATAL() macros. They are not intended to be used directly by the user (but nothing prevents it).

The TRACE(), DEBUG(), INFO(), WARNING(), ERROR(), CRITICAL(), and FATAL() macros take as an argument a message to display, which can contain conversion specifiers as in the printf function. They can take additional arguments, one per conversion specifier.

Example of use:

#include "error.h"
ERROR("The variable colors is %d, it should be greater than %d.",
colors, MIN_COLORS);
Macros for displaying error messages.
#define ERROR(msg,...)
Print an ERROR message.
Definition error.h:158
Note
Messages of a certain type are displayed depending on the debug level, set by the DEBUG_LEVEL variable. For example, if the debug level is set to ERROR, only the ERROR(), CRITICAL(), and FATAL() macros will display their message. The other macros will display nothing. By default, the debug level is INFO.

Debug levels:
  • OFF (only FATAL messages are displayed),
  • CRITICAL (only CRITICAL and FATAL messages),
  • ERROR (only ERROR, CRITICAL, and FATAL messages),
  • WARNING (only WARNING, ERROR, CRITICAL, and FATAL messages),
  • INFO (all messages except DEBUG and TRACE),
  • DEBUG (all messages except TRACE),
  • TRACE (all messages).
To change the debug level, you must define the DEBUG_LEVEL macro before including error.h, typically at compilation. For example, to display all debug messages, you must set DEBUG_LEVEL to TRACE. To do this, you can compile the program as follows:
make -B CFLAGS="-DDEBUG_LEVEL=TRACE"
See also
DEBUG_LEVEL and the macros TRACE(), DEBUG(), INFO(), WARNING(), ERROR(), CRITICAL(), and FATAL().

Macro Definition Documentation

◆ CRITICAL

#define CRITICAL ( msg,
... )
Value:
PRINT_DEBUG(KO, "CRITICAL", CRITICAL_L, msg, __VA_ARGS__)
#define KO
Symbole ❌.
Definition error.h:63

Print a CRITICAL message.

◆ DEBUG

#define DEBUG ( msg,
... )
Value:
PRINT_DEBUG(HINT, "DEBUG", DEBUG_L, msg, __VA_ARGS__)
#define HINT
Symbole ✨.
Definition error.h:59

Print a DEBUG message.

◆ DEBUG_LEVEL

#define DEBUG_LEVEL   INFO

Debug level, INFO by default.

By default, the debug level is INFO. To change the debug level, you must define the DEBUG_LEVEL macro before including error.h.

This can be done at compilation:

make -B CFLAGS="-DDEBUG_LEVEL=WARNING"

For example, to display all debug messages, you must set DEBUG_LEVEL to TRACE. To display only FATAL-level messages, you must set DEBUG_LEVEL to OFF.

Possible levels:

  • OFF (only FATAL messages are displayed),
  • CRITICAL (only CRITICAL and FATAL messages),
  • ERROR (only ERROR, CRITICAL, and FATAL messages),
  • WARNING (only WARNING, ERROR, CRITICAL, and FATAL messages),
  • INFO (all messages except DEBUG and TRACE),
  • DEBUG (all messages except TRACE),
  • TRACE (all messages).
See also
enum debug_level

◆ ERROR

#define ERROR ( msg,
... )
Value:
PRINT_DEBUG(ATTENTION, "ERROR", ERROR_L, msg, __VA_ARGS__)
#define ATTENTION
Symbole ❗.
Definition error.h:57

Print an ERROR message.

◆ FATAL

#define FATAL ( msg,
... )
Value:
do { \
PRINT_ERROR(KO, "FATAL", __func__, __FILE__, __LINE__, \
msg __VA_OPT__(, ) __VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)

Prints a FATAL message and end the program.

◆ INFO

#define INFO ( msg,
... )
Value:
PRINT_DEBUG(HINT, "INFO", INFO_L, msg, __VA_ARGS__)

Print an INFO message.

◆ PRINT_DEBUG

#define PRINT_DEBUG ( symbol,
name,
level,
msg,
... )
Value:
do { \
if ((int)DEBUG_LEVEL > (int)level) \
PRINT_ERROR(symbol, name, __func__, __FILE__, __LINE__, \
msg __VA_OPT__(, ) __VA_ARGS__); \
} while (0)
#define DEBUG_LEVEL
Debug level, INFO by default.
Definition error.h:99

◆ PRINT_ERROR

#define PRINT_ERROR ( symbol,
name,
function,
file,
line,
msg,
... )
Value:
do { \
fprintf(stderr, \
"\n" symbol " [" name "]" \
" - Function %s (%s:%d) -\n " msg "\n", \
function, file, line __VA_OPT__(, ) __VA_ARGS__); \
} while (0)

Displays an error message on the error output.

This macro is used by the macros TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL, and FATAL.

Parameters
symbolSymbol to display (ATTENTION, HINT, OK, or KO).
nameName of the message (such as "TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", or "FATAL").
functionName of the function where the message is displayed.
fileName of the file where the message is displayed.
lineLine number where the message is displayed.
msgMessage to display. This message can contain the same conversion specifiers as the printf function (such as d, s, etc.).
...Additional arguments to display with the message, one per conversion specifier.

◆ TRACE

#define TRACE ( msg,
... )
Value:
PRINT_DEBUG(OK, "TRACE", TRACE_L, msg, __VA_ARGS__)
#define OK
Symbole ✅.
Definition error.h:61

Prints a TRACE message.

◆ WARNING

#define WARNING ( msg,
... )
Value:
PRINT_DEBUG(ATTENTION, "WARNING", WARNING_L, msg, __VA_ARGS__)

Print a WARNING message.