Inline Logging
Sometimes, we just want to log a simple message without the full traceback. In this case, we can use the inline logging API.
API
CTB_LOG_ERROR_INLINE Macro
Wrapper for logging an error to stderr without stacktrace.
CTB_LOG_ERROR_INLINE(ctb_error, msg, ...)Logging an inline error has no effect on the context / call stack. It only logs an error message.
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_inline( \
__FILE__, __LINE__, __func__, ctb_error, msg, __VA_ARGS__ \
); \
} while (0)Usage
double division(double x, double y)
{
if (y == 0)
{
CTB_LOG_ERROR_INLINE(
CTB_ZERO_DIVISION_ERROR,
"y cannot be zero! Received: %lf",
y
);
return 0.0;
}
return x / y;
}Output:
ZeroDivisionError: File "/home/alvinng/Desktop/c_trackback/examples/example.c", line 19 in division:
y cannot be zero! Received: 0.000000CTB_LOG_WARNING_INLINE Macro
Wrapper for logging a warning to stderr without stacktrace.
CTB_LOG_WARNING_INLINE(ctb_warning, 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_inline( \
__FILE__, __LINE__, __func__, ctb_warning, msg, __VA_ARGS__ \
); \
} while (0)Usage
double some_function(void)
{
CTB_LOG_WARNING_INLINE(
CTB_DEPRECATION_WARNING,
"Function \"%s\" is deprecated. It will be removed in the next version.",
__func__
);
/* Do something */
return;
}Output:
DeprecationWarning: File "/home/alvinng/Desktop/c_trackback/examples/example.c", line 17 in some_function:
Function "some_function" is deprecated. It will be removed in the next version.CTB_LOG_MESSAGE_INLINE Macro
Wrapper for logging a message to stdout without stacktrace.
CTB_LOG_MESSAGE_INLINE(msg, ...)Parameters
| Name | Type | Description |
|---|---|---|
msg | const char * | Message. |
... | N/A | Additional arguments for formatting the message. |
Expands to
do \
{ \
ctb_log_message_inline(__FILE__, __LINE__, __func__, msg, __VA_ARGS__); \
} while (0)Usage
void inline_message(int i)
{
CTB_LOG_MESSAGE_INLINE("(Test %d) Hello, world! :)", i);
}Output:
Message: File "/home/alvinng/Desktop/c_trackback/examples/example.c", line 75 in inline_message:
(Test 0) Hello, world! :)Last updated on