Logging
Sometimes, we just want to log a simple message without the full traceback. In this case, we can use the logging API.
MathError: File "/home/alvinng/Desktop/c_traceback/examples/example_logging.c", line 32 in log_error:
(Test 1) This should be formatted error level 1 with math error
BufferError: File "/home/alvinng/Desktop/c_traceback/examples/example_logging.c", line 49 in log_error_level2:
(Test 2) This should be error level 2 with buffer error
DeprecationWarning: File "/home/alvinng/Desktop/c_traceback/examples/example_logging.c", line 54 in log_warning:
(Test 3) This should be formatted warning level 1 with deprecation warning
UserWarning: File "/home/alvinng/Desktop/c_traceback/examples/example_logging.c", line 72 in log_warning_level2:
(Test 4) This should be warning level 2 with user warning
Message: File "/home/alvinng/Desktop/c_traceback/examples/example_logging.c", line 77 in log_message:
(Test 5) This should be formatted message level 1
Message: File "/home/alvinng/Desktop/c_traceback/examples/example_logging.c", line 92 in log_message_level2:
(Test 6) This should be message level 2API
LOG_ERROR Macro
Wrapper for logging an error to stderr without stacktrace.
void LOG_ERROR(CTB_Error ctb_error, const char *restrict msg)Logging an error has no effect on error context.
It simply logs a message. Use
THROW instead.Parameters
| Name | Type | Description |
|---|---|---|
ctb_error | CTB_Error | The error type. |
msg | const char * | Error message. |
Expands to
do \
{ \
ctb_log_error(__FILE__, __LINE__, __func__, ctb_error, msg); \
} while (0)Usage
LOG_ERROR(
CTB_BUFFER_ERROR,
"This should be formatted error with buffer error",
);LOG_ERROR_FMT Macro
Wrapper for logging an error with formatted message to stderr without stacktrace.
void LOG_ERROR_FMT(CTB_Error ctb_error, const char *restrict msg, ...)Logging an error has no effect on error context.
It simply logs a message. Use
THROW_FMT instead.Parameters
| Name | Type | Description |
|---|---|---|
ctb_error | CTB_Error | The error type. |
msg | const char * | Error message. |
... | N/A | Additional arguments for formatting the message. |
Expands to
do \
{ \
ctb_log_error_fmt(__FILE__, __LINE__, __func__, ctb_error, msg, __VA_ARGS__); \
} while (0)Usage
LOG_ERROR_FMT(
CTB_SYSTEM_ERROR,
"(Test %d) This should be formatted error with system error",
1
);LOG_WARNING Macro
Wrapper for logging a warning to stderr without stacktrace.
void LOG_WARNING(CTB_Error ctb_warning, const char *restrict msg)Parameters
| Name | Type | Description |
|---|---|---|
ctb_warning | CTB_Warning | The warning type. |
msg | const char * | Warning message. |
Expands to
do \
{ \
ctb_log_warning(__FILE__, __LINE__, __func__, ctb_warning, msg); \
} while (0)Usage
LOG_WARNING(
CTB_USER_WARNING,
"This should be warning with user warning"
);LOG_WARNING_FMT Macro
Wrapper for logging a warning with formatted message to stderr without stacktrace.
void LOG_WARNING_FMT(CTB_Error ctb_warning, const char* restrict msg, ...)Parameters
| Name | Type | Description |
|---|---|---|
ctb_warning | CTB_Warning | The warning type. |
msg | const char * | Warning message. |
... | N/A | Additional arguments for formatting the message. |
Expands to
do \
{ \
ctb_log_warning_fmt( \
__FILE__, __LINE__, __func__, ctb_warning, msg, __VA_ARGS__ \
); \
} while (0)Usage
LOG_WARNING_FMT(
CTB_DEPRECATION_WARNING,
"(Test %d) This should be formatted warning with deprecation warning",
3
);LOG_MESSAGE Macro
Wrapper for logging a message to stdout without stacktrace.
void LOG_MESSAGE(const char *restrict msg)Parameters
| Name | Type | Description |
|---|---|---|
msg | const char * | Message. |
Expands to
do \
{ \
ctb_log_message(__FILE__, __LINE__, __func__, msg); \
} while (0)Usage
void message(int i)
{
LOG_MESSAGE("Hello, world! :)");
}LOG_MESSAGE_FMT Macro
Wrapper for logging a formatted message to stdout without stacktrace.
void LOG_MESSAGE_FMT(const char *restrict msg, ...)Parameters
| Name | Type | Description |
|---|---|---|
msg | const char * | Message. |
... | N/A | Additional arguments for formatting the message. |
Expands to
do \
{ \
ctb_log_message(__FILE__, __LINE__, __func__, msg, __VA_ARGS__); \
} while (0)Usage
LOG_MESSAGE_FMT("(Test %d) Hello, world! :)", 0);Last updated on