Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 0 additions & 93 deletions examples/example_inline.c

This file was deleted.

88 changes: 88 additions & 0 deletions examples/example_logging.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* \file example_logging.c
*
* \brief Example usage of logging functions from c_traceback library
*/

#include <stdio.h>
#include <string.h>

#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);
}
4 changes: 3 additions & 1 deletion examples/example_tut04.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
2 changes: 1 addition & 1 deletion include/c_traceback.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 23 additions & 25 deletions include/c_traceback/log_inline.h → include/c_traceback/logging.h
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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)

/**
Expand All @@ -28,21 +28,21 @@
* \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)

/**
* \brief Wrapper for logging a message to stdout without stacktrace.
*
* \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)

/**
Expand All @@ -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)

/**
Expand All @@ -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)
Expand All @@ -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)

/**
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -185,12 +183,12 @@ 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,
const char *restrict msg,
...
);

#endif // C_TRACEBACK_LOG_INLINE_H
#endif // C_TRACEBACK_LOGGING_H
Loading