From 6bec7fb19a85af4ecf8b137af66732688dedd331 Mon Sep 17 00:00:00 2001 From: Alvin <154572722+alvinng4@users.noreply.github.com> Date: Sun, 4 Jan 2026 17:13:04 +0800 Subject: [PATCH] Change all inline logging naming to logging --- CMakeLists.txt | 2 +- examples/example_inline.c | 93 ------------------- examples/example_logging.c | 88 ++++++++++++++++++ examples/example_tut04.c | 4 +- include/c_traceback.h | 2 +- .../c_traceback/{log_inline.h => logging.h} | 48 +++++----- src/{log_inline.c => logging.c} | 62 +++++-------- src/signal_handler.c | 28 +++--- src/traceback.c | 12 +-- 9 files changed, 160 insertions(+), 179 deletions(-) delete mode 100644 examples/example_inline.c create mode 100644 examples/example_logging.c rename include/c_traceback/{log_inline.h => logging.h} (79%) rename src/{log_inline.c => logging.c} (87%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b704d5..6f7481c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ option(BUILD_EXAMPLES "Build example executables" OFF) add_library(c_traceback STATIC src/error.c src/error_codes.c - src/log_inline.c + src/logging.c src/trace.c src/traceback.c src/utils.c diff --git a/examples/example_inline.c b/examples/example_inline.c deleted file mode 100644 index e4327c8..0000000 --- a/examples/example_inline.c +++ /dev/null @@ -1,93 +0,0 @@ -/** - * \file example_inline.c - * - * \brief Example usage of inline logging functions from c_traceback library - */ - -#include -#include - -#include "c_traceback.h" - -#define MESSAGE_BUFFER_SIZE 256 - -void inline_error(int i); -void inline_error_level2(int i); -void inline_warning(int i); -void inline_warning_level2(int i); -void inline_message(int i); -void inline_message_level2(int i); - -int main(void) -{ - inline_error(1); - inline_warning(3); - inline_message(5); - - return 0; -} - -void inline_error(int i) -{ - LOG_ERROR_INLINE_FMT( - CTB_MATH_ERROR, - "(Test %d) This should be inline formatted error level 1 with math error", - i - ); - inline_error_level2(i + 1); -} - -void inline_error_level2(int i) -{ - char message[MESSAGE_BUFFER_SIZE]; - snprintf( - message, - MESSAGE_BUFFER_SIZE, - "(Test %d) This should be inline error level 2 with buffer error", - i - ); - LOG_ERROR_INLINE(CTB_BUFFER_ERROR, message); -} - -void inline_warning(int i) -{ - LOG_WARNING_INLINE_FMT( - CTB_DEPRECATION_WARNING, - "(Test %d) This should be inline formatted warning level 1 with deprecation " - "warning", - i - ); - inline_warning_level2(i + 1); -} - -void inline_warning_level2(int i) -{ - char message[MESSAGE_BUFFER_SIZE]; - snprintf( - message, - MESSAGE_BUFFER_SIZE, - "(Test %d) This should be inline warning level 2 with user warning", - i - ); - LOG_WARNING_INLINE(CTB_USER_WARNING, message); -} - -void inline_message(int i) -{ - LOG_MESSAGE_INLINE_FMT( - "(Test %d) This should be inline formatted message level 1", i - ); - inline_message_level2(i + 1); -} - -void inline_message_level2(int i) -{ - char message[MESSAGE_BUFFER_SIZE]; - snprintf( - message, - MESSAGE_BUFFER_SIZE, - "(Test %d) This should be inline message level 2", - i - ); - LOG_MESSAGE_INLINE(message); -} diff --git a/examples/example_logging.c b/examples/example_logging.c new file mode 100644 index 0000000..47d00a1 --- /dev/null +++ b/examples/example_logging.c @@ -0,0 +1,88 @@ +/** + * \file example_logging.c + * + * \brief Example usage of logging functions from c_traceback library + */ + +#include +#include + +#include "c_traceback.h" + +#define MESSAGE_BUFFER_SIZE 256 + +void log_error(int i); +void log_error_level2(int i); +void log_warning(int i); +void log_warning_level2(int i); +void log_message(int i); +void log_message_level2(int i); + +int main(void) +{ + log_error(1); + log_warning(3); + log_message(5); + + return 0; +} + +void log_error(int i) +{ + LOG_ERROR_FMT( + CTB_MATH_ERROR, + "(Test %d) This should be formatted error level 1 with math error", + i + ); + log_error_level2(i + 1); +} + +void log_error_level2(int i) +{ + char message[MESSAGE_BUFFER_SIZE]; + snprintf( + message, + MESSAGE_BUFFER_SIZE, + "(Test %d) This should be error level 2 with buffer error", + i + ); + LOG_ERROR(CTB_BUFFER_ERROR, message); +} + +void log_warning(int i) +{ + LOG_WARNING_FMT( + CTB_DEPRECATION_WARNING, + "(Test %d) This should be formatted warning level 1 with deprecation " + "warning", + i + ); + log_warning_level2(i + 1); +} + +void log_warning_level2(int i) +{ + char message[MESSAGE_BUFFER_SIZE]; + snprintf( + message, + MESSAGE_BUFFER_SIZE, + "(Test %d) This should be warning level 2 with user warning", + i + ); + LOG_WARNING(CTB_USER_WARNING, message); +} + +void log_message(int i) +{ + LOG_MESSAGE_FMT("(Test %d) This should be formatted message level 1", i); + log_message_level2(i + 1); +} + +void log_message_level2(int i) +{ + char message[MESSAGE_BUFFER_SIZE]; + snprintf( + message, MESSAGE_BUFFER_SIZE, "(Test %d) This should be message level 2", i + ); + LOG_MESSAGE(message); +} diff --git a/examples/example_tut04.c b/examples/example_tut04.c index f6c9d94..cad4578 100644 --- a/examples/example_tut04.c +++ b/examples/example_tut04.c @@ -70,7 +70,9 @@ void divide_vec(double *arr, const size_t n, const double denominator) if (denominator == 0.0) { // Division by zero - LOG_WARNING_INLINE_FMT(CTB_WARNING, "Denominator should not be zero! Received: %lf.", denominator); + LOG_WARNING_FMT( + CTB_WARNING, "Denominator should not be zero! Received: %lf.", denominator + ); } for (int i = 0; i < n; i++) diff --git a/include/c_traceback.h b/include/c_traceback.h index 008d649..8bc1ea1 100644 --- a/include/c_traceback.h +++ b/include/c_traceback.h @@ -11,7 +11,7 @@ #include "c_traceback/color_codes.h" #include "c_traceback/error.h" #include "c_traceback/error_codes.h" -#include "c_traceback/log_inline.h" +#include "c_traceback/logging.h" #include "c_traceback/signal_handler.h" #include "c_traceback/trace.h" #include "c_traceback/traceback.h" diff --git a/include/c_traceback/log_inline.h b/include/c_traceback/logging.h similarity index 79% rename from include/c_traceback/log_inline.h rename to include/c_traceback/logging.h index 7daa036..03fd3cc 100644 --- a/include/c_traceback/log_inline.h +++ b/include/c_traceback/logging.h @@ -1,12 +1,12 @@ /** - * \file log_inline.h - * \brief Header file for inline logging functions. + * \file logging.h + * \brief Header file for logging functions. * * \author Ching-Yin Ng */ -#ifndef C_TRACEBACK_LOG_INLINE_H -#define C_TRACEBACK_LOG_INLINE_H +#ifndef C_TRACEBACK_LOGGING_H +#define C_TRACEBACK_LOGGING_H #include "c_traceback/error_codes.h" @@ -16,10 +16,10 @@ * \param[in] ctb_error The error type. * \param[in] msg Error message. */ -#define LOG_ERROR_INLINE(ctb_error, msg) \ +#define LOG_ERROR(ctb_error, msg) \ do \ { \ - ctb_log_error_inline(__FILE__, __LINE__, __func__, ctb_error, msg); \ + ctb_log_error(__FILE__, __LINE__, __func__, ctb_error, msg); \ } while (0) /** @@ -28,10 +28,10 @@ * \param[in] ctb_warning The warning type. * \param[in] msg Warning message. */ -#define LOG_WARNING_INLINE(ctb_warning, msg) \ +#define LOG_WARNING(ctb_warning, msg) \ do \ { \ - ctb_log_warning_inline(__FILE__, __LINE__, __func__, ctb_warning, msg); \ + ctb_log_warning(__FILE__, __LINE__, __func__, ctb_warning, msg); \ } while (0) /** @@ -39,10 +39,10 @@ * * \param[in] msg Message. */ -#define LOG_MESSAGE_INLINE(msg) \ +#define LOG_MESSAGE(msg) \ do \ { \ - ctb_log_message_inline(__FILE__, __LINE__, __func__, msg); \ + ctb_log_message(__FILE__, __LINE__, __func__, msg); \ } while (0) /** @@ -53,12 +53,10 @@ * \param[in] msg Error message. * \param[in] ... Additional arguments for formatting the message. */ -#define LOG_ERROR_INLINE_FMT(ctb_error, msg, ...) \ +#define LOG_ERROR_FMT(ctb_error, msg, ...) \ do \ { \ - ctb_log_error_inline_fmt( \ - __FILE__, __LINE__, __func__, ctb_error, msg, __VA_ARGS__ \ - ); \ + ctb_log_error_fmt(__FILE__, __LINE__, __func__, ctb_error, msg, __VA_ARGS__); \ } while (0) /** @@ -69,10 +67,10 @@ * \param[in] msg Warning message. * \param[in] ... Additional arguments for formatting the message. */ -#define LOG_WARNING_INLINE_FMT(ctb_warning, msg, ...) \ +#define LOG_WARNING_FMT(ctb_warning, msg, ...) \ do \ { \ - ctb_log_warning_inline_fmt( \ + ctb_log_warning_fmt( \ __FILE__, __LINE__, __func__, ctb_warning, msg, __VA_ARGS__ \ ); \ } while (0) @@ -83,10 +81,10 @@ * \param[in] msg Warning message. * \param[in] ... Additional arguments for formatting the message. */ -#define LOG_MESSAGE_INLINE_FMT(msg, ...) \ +#define LOG_MESSAGE_FMT(msg, ...) \ do \ { \ - ctb_log_message_inline_fmt(__FILE__, __LINE__, __func__, msg, __VA_ARGS__); \ + ctb_log_message_fmt(__FILE__, __LINE__, __func__, msg, __VA_ARGS__); \ } while (0) /** @@ -98,7 +96,7 @@ * \param[in] error The error type. * \param[in] msg Error message. */ -void ctb_log_error_inline( +void ctb_log_error( const char *restrict file, const int line, const char *restrict func, @@ -115,7 +113,7 @@ void ctb_log_error_inline( * \param[in] warning The warning type. * \param[in] msg Warning message. */ -void ctb_log_warning_inline( +void ctb_log_warning( const char *restrict file, const int line, const char *restrict func, @@ -131,7 +129,7 @@ void ctb_log_warning_inline( * \param[in] func Function where the message is sent. * \param[in] msg Message. */ -void ctb_log_message_inline( +void ctb_log_message( const char *restrict file, const int line, const char *restrict func, @@ -148,7 +146,7 @@ void ctb_log_message_inline( * \param[in] msg Error message. * \param[in] ... Additional arguments for formatting the message. */ -void ctb_log_error_inline_fmt( +void ctb_log_error_fmt( const char *restrict file, const int line, const char *restrict func, @@ -167,7 +165,7 @@ void ctb_log_error_inline_fmt( * \param[in] msg Warning message. * \param[in] ... Additional arguments for formatting the message. */ -void ctb_log_warning_inline_fmt( +void ctb_log_warning_fmt( const char *restrict file, const int line, const char *restrict func, @@ -185,7 +183,7 @@ void ctb_log_warning_inline_fmt( * \param[in] msg Message. * \param[in] ... Additional arguments for formatting the message. */ -void ctb_log_message_inline_fmt( +void ctb_log_message_fmt( const char *restrict file, const int line, const char *restrict func, @@ -193,4 +191,4 @@ void ctb_log_message_inline_fmt( ... ); -#endif // C_TRACEBACK_LOG_INLINE_H \ No newline at end of file +#endif // C_TRACEBACK_LOGGING_H \ No newline at end of file diff --git a/src/log_inline.c b/src/logging.c similarity index 87% rename from src/log_inline.c rename to src/logging.c index d7f1aea..83e0a0d 100644 --- a/src/log_inline.c +++ b/src/logging.c @@ -1,6 +1,6 @@ /** - * \file log_inline.c - * \brief Function definitions for inline logging. + * \file logging.c + * \brief Function definitions for simple logging. * * \author Ching-Yin Ng */ @@ -15,7 +15,7 @@ #include "internal/utils.h" /** - * \brief Helper for logging inline messages without the message body. + * \brief Helper for logging simple messages without the message body. * * \param[in] use_color Whether to use color in the output. * \param[in, out] stream The output stream. @@ -26,7 +26,7 @@ * \param[in] func The function name. * \param[in] header The header string. */ -static void ctb_log_inline_core( +static void ctb_logging_core( const bool use_color, FILE *stream, const char *header_color, @@ -106,7 +106,7 @@ static void ctb_log_inline_core( } /** - * \brief Log inline message. + * \brief Log message. * * \param[in, out] stream The output stream. * \param[in] header_color The color code for the header. @@ -117,7 +117,7 @@ static void ctb_log_inline_core( * \param[in] header The header string. * \param[in] msg The message string. */ -static void ctb_log_inline( +static void ctb_log( FILE *stream, const char *header_color, const char *message_color, @@ -129,15 +129,8 @@ static void ctb_log_inline( ) { const bool use_color = should_use_color(stream); - ctb_log_inline_core( - use_color, - stream, - header_color, - message_color, - file_address, - line, - func, - header + ctb_logging_core( + use_color, stream, header_color, message_color, file_address, line, func, header ); if (use_color) @@ -153,7 +146,7 @@ static void ctb_log_inline( } /** - * \brief Log inline message with variadic arguments. + * \brief Log message with variadic arguments. * * \param[in, out] stream The output stream. * \param[in] header_color The color code for the header. @@ -165,7 +158,7 @@ static void ctb_log_inline( * \param[in] msg The message format string. * \param[in] args The variadic arguments. */ -static void ctb_log_inline_fmt( +static void ctb_log_fmt( FILE *stream, const char *header_color, const char *message_color, @@ -178,15 +171,8 @@ static void ctb_log_inline_fmt( ) { const bool use_color = should_use_color(stream); - ctb_log_inline_core( - use_color, - stream, - header_color, - message_color, - file_address, - line, - func, - header + ctb_logging_core( + use_color, stream, header_color, message_color, file_address, line, func, header ); if (use_color) @@ -202,7 +188,7 @@ static void ctb_log_inline_fmt( fflush(stream); } -void ctb_log_error_inline( +void ctb_log_error( const char *restrict file, const int line, const char *restrict func, @@ -211,7 +197,7 @@ void ctb_log_error_inline( ) { FILE *stream = stderr; - ctb_log_inline( + ctb_log( stream, CTB_ERROR_BOLD_COLOR, CTB_ERROR_COLOR, @@ -223,7 +209,7 @@ void ctb_log_error_inline( ); } -void ctb_log_warning_inline( +void ctb_log_warning( const char *restrict file, const int line, const char *restrict func, @@ -232,7 +218,7 @@ void ctb_log_warning_inline( ) { FILE *stream = stderr; - ctb_log_inline( + ctb_log( stream, CTB_WARNING_BOLD_COLOR, CTB_WARNING_COLOR, @@ -244,7 +230,7 @@ void ctb_log_warning_inline( ); } -void ctb_log_message_inline( +void ctb_log_message( const char *restrict file, const int line, const char *restrict func, @@ -252,7 +238,7 @@ void ctb_log_message_inline( ) { FILE *stream = stdout; - ctb_log_inline( + ctb_log( stream, CTB_NORMAL_BOLD_COLOR, CTB_NORMAL_COLOR, @@ -264,7 +250,7 @@ void ctb_log_message_inline( ); } -void ctb_log_error_inline_fmt( +void ctb_log_error_fmt( const char *restrict file, const int line, const char *restrict func, @@ -276,7 +262,7 @@ void ctb_log_error_inline_fmt( FILE *stream = stderr; va_list args; va_start(args, msg); - ctb_log_inline_fmt( + ctb_log_fmt( stream, CTB_ERROR_BOLD_COLOR, CTB_ERROR_COLOR, @@ -290,7 +276,7 @@ void ctb_log_error_inline_fmt( va_end(args); } -void ctb_log_warning_inline_fmt( +void ctb_log_warning_fmt( const char *restrict file, const int line, const char *restrict func, @@ -302,7 +288,7 @@ void ctb_log_warning_inline_fmt( FILE *stream = stderr; va_list args; va_start(args, msg); - ctb_log_inline_fmt( + ctb_log_fmt( stream, CTB_WARNING_BOLD_COLOR, CTB_WARNING_COLOR, @@ -316,7 +302,7 @@ void ctb_log_warning_inline_fmt( va_end(args); } -void ctb_log_message_inline_fmt( +void ctb_log_message_fmt( const char *restrict file, const int line, const char *restrict func, @@ -327,7 +313,7 @@ void ctb_log_message_inline_fmt( FILE *stream = stdout; va_list args; va_start(args, msg); - ctb_log_inline_fmt( + ctb_log_fmt( stream, CTB_NORMAL_BOLD_COLOR, CTB_NORMAL_COLOR, diff --git a/src/signal_handler.c b/src/signal_handler.c index 48ab023..eeca124 100644 --- a/src/signal_handler.c +++ b/src/signal_handler.c @@ -40,27 +40,27 @@ void ctb_install_signal_handler(void) { if (signal(SIGABRT, ctb_internal_signal_handler) == SIG_ERR) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGABRT"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGABRT"); } if (signal(SIGSEGV, ctb_internal_signal_handler) == SIG_ERR) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGSEGV"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGSEGV"); } if (signal(SIGILL, ctb_internal_signal_handler) == SIG_ERR) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGILL"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGILL"); } if (signal(SIGTERM, ctb_internal_signal_handler) == SIG_ERR) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGTERM"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGTERM"); } if (signal(SIGFPE, ctb_internal_signal_handler) == SIG_ERR) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGFPE"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGFPE"); } if (signal(SIGINT, ctb_internal_signal_handler) == SIG_ERR) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGINT"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGINT"); } } @@ -113,7 +113,7 @@ void ctb_install_signal_handler(void) if (sigaltstack(&ss, NULL) == -1) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set alternate signal stack"); + LOG_WARNING(CTB_WARNING, "Failed to set alternate signal stack"); } struct sigaction sa; @@ -125,32 +125,32 @@ void ctb_install_signal_handler(void) // Register Signals if (sigaction(SIGABRT, &sa, NULL) == -1) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGABRT"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGABRT"); } if (sigaction(SIGSEGV, &sa, NULL) == -1) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGSEGV"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGSEGV"); } if (sigaction(SIGILL, &sa, NULL) == -1) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGILL"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGILL"); } if (sigaction(SIGTERM, &sa, NULL) == -1) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGTERM"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGTERM"); } if (sigaction(SIGFPE, &sa, NULL) == -1) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGFPE"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGFPE"); } if (sigaction(SIGINT, &sa, NULL) == -1) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGINT"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGINT"); } #ifdef SIGBUS if (sigaction(SIGBUS, &sa, NULL) == -1) { - LOG_WARNING_INLINE(CTB_WARNING, "Failed to set signal handler for SIGBUS"); + LOG_WARNING(CTB_WARNING, "Failed to set signal handler for SIGBUS"); } #endif /* SIGBUS */ } diff --git a/src/traceback.c b/src/traceback.c index 5bc0587..3b0bc4e 100644 --- a/src/traceback.c +++ b/src/traceback.c @@ -590,18 +590,18 @@ void ctb_print_compilation_info(void) row++; } - /* Sample inline logging */ + /* Sample logging */ fputs("\n", stream); - fprintf(stream, "%sInline logging (example)%s\n", theme.theme_bold, theme.reset); - for (int d = 0; d < 24; d++) + fprintf(stream, "%sLogging (example)%s\n", theme.theme_bold, theme.reset); + for (int d = 0; d < 17; d++) { fputs(dash, stream); } fputs("\n", stream); fflush(stream); - LOG_ERROR_INLINE(CTB_ERROR, "Sample error for compilation info"); - LOG_WARNING_INLINE(CTB_USER_WARNING, "Sample warning for compilation info"); - LOG_MESSAGE_INLINE("Sample info for compilation info"); + LOG_ERROR(CTB_ERROR, "Sample error for compilation info"); + LOG_WARNING(CTB_USER_WARNING, "Sample warning for compilation info"); + LOG_MESSAGE("Sample info for compilation info"); /* Sample Traceback */ const int num_examples = 3;