|
Banjo API 1.0.0-rc.2
Low-level C99 game development API
|
Macros | |
| #define | BJ_MAXIMUM_LOG_LEN 120u |
| #define | bj_log_msg(LEVEL, ...) |
| #define | bj_trace(...) |
| #define | bj_debug(...) |
| #define | bj_info(...) |
| #define | bj_warn(...) |
| #define | bj_err(...) |
| #define | bj_fatal(...) |
Typedefs | |
| typedef enum bj_log_level | bj_log_level |
Enumerations | |
| enum | bj_log_level { BJ_LOG_TRACE , BJ_LOG_DEBUG , BJ_LOG_INFO , BJ_LOG_WARN , BJ_LOG_ERROR , BJ_LOG_FATAL } |
Functions | |
| const char * | bj_get_log_level_string (int level) |
| void | bj_set_log_level (int level) |
| int | bj_get_log_level (void) |
| size_t | bj_log_message (int level, const char *file, int line, const char *format,...) |
Severity-filtered structured logging.
Banjo applications and the library itself log through one shared channel. Six severity levels are defined in bj_log_level (TRACE, DEBUG, INFO, WARN, ERROR, FATAL). At runtime only messages at or above the threshold set by bj_set_log_level are emitted; everything below is dropped at the call site, so leaving trace logs in shipped code costs nothing when the threshold is higher.
Use the per-level macros (bj_trace, bj_debug, bj_info, bj_warn, bj_err, bj_fatal) over the generic bj_log_msg / bj_log_message. They take printf-style arguments:
In debug builds, the macros also emit the source file and line (rendered as "FILE:LINE" between the level and the message). In release builds those are stripped to keep the binary lean.
A complete log line looks like TIME LEVEL: (SOURCE) MESSAGE, e.g. 12:23:34 WARN: logging.c:27 Warning level message. The maximum line length is fixed at BJ_MAXIMUM_LOG_LEN; messages longer than that are truncated, with header components (level, time, source) dropped in reverse priority order to keep the message intact.
Set BANJO_CONFIG_LOG_COLOR at build time to colourise level names (and source location in debug builds) using ANSI codes when the terminal supports it.
logging.c for a runnable demo. | #define bj_debug | ( | ... | ) |
Log a message using the BJ_LOG_DEBUG level.
The function effectively calls bj_log_message, forwarding arguments to format the input string.
This function is preferred over calling bj_log_msg or bj_log_message directly for clarity.
| ... | The string formatting, forwarded to the last arguments of bj_log_message. |
| #define bj_err | ( | ... | ) |
Log a message using the BJ_LOG_ERROR level.
The function effectively calls bj_log_message, forwarding arguments to format the input string.
This function is preferred over calling bj_log_msg or bj_log_message directly for clarity.
| ... | The string formatting, forwarded to the last arguments of bj_log_message. |
| #define bj_fatal | ( | ... | ) |
Log a message using the BJ_LOG_FATAL level.
The function effectively calls bj_log_message, forwarding arguments to format the input string.
This function is preferred over calling bj_log_msg or bj_log_message directly for clarity.
| ... | The string formatting, forwarded to the last arguments of bj_log_message. |
| #define bj_info | ( | ... | ) |
Log a message using the BJ_LOG_INFO level.
The function effectively calls bj_log_message, forwarding arguments to format the input string.
This function is preferred over calling bj_log_msg or bj_log_message directly for clarity.
| ... | The string formatting, forwarded to the last arguments of bj_log_message. |
Definition at line 141 of file log.h.
Referenced by button_callback(), cursor_callback(), demonstrate_error_copy(), demonstrate_error_matching(), demonstrate_zero_cost(), display_value(), enter_callback(), initialize_server(), key_callback(), main(), resize_callback(), resize_callback(), step(), step(), teardown(), and teardown().
| #define bj_log_msg | ( | LEVEL, | |
| ... ) |
Log a message using the given level LEVEL.
Any integer value can be used for LEVEL, but usually any of BJ_LOG_TRACE, BJ_LOG_DEBUG, BJ_LOG_INFO, BJ_LOG_WARN, BJ_LOG_ERROR or BJ_LOG_FATAL is used.
When a message is sent with a given level, it will be reported only if the value set with bj_set_log_level is at least the same value.
The function effectively calls bj_log_message, forwarding arguments to format the input string.
| LEVEL | The log level to report the message in. |
| ... | Arguments forwarded to bj_log_message. |
Definition at line 96 of file log.h.
Referenced by main().
| #define BJ_MAXIMUM_LOG_LEN 120u |
Maximum length of log messages.
Log messages, including header information such as timestamp and log level, cannot have a size in bytes larger than BJ_MAXIMUM_LOG_LEN, excluding the null character. In case a message is longer than this limit, the content is truncated in order of priority as described in bj_log_message.
| #define bj_trace | ( | ... | ) |
Log a message using the BJ_LOG_TRACE level.
The function effectively calls bj_log_message, forwarding arguments to format the input string.
This function is preferred over calling bj_log_msg or bj_log_message directly for clarity.
| ... | The string formatting, forwarded to the last arguments of bj_log_message. |
Definition at line 113 of file log.h.
Referenced by main().
| #define bj_warn | ( | ... | ) |
Log a message using the BJ_LOG_WARN level.
The function effectively calls bj_log_message, forwarding arguments to format the input string.
This function is preferred over calling bj_log_msg or bj_log_message directly for clarity.
| ... | The string formatting, forwarded to the last arguments of bj_log_message. |
Definition at line 155 of file log.h.
Referenced by main().
| typedef enum bj_log_level bj_log_level |
| enum bj_log_level |
Log Levels.
Level values are ordered from 0.
| int bj_get_log_level | ( | void | ) |
Gets the current log level set by bj_set_log_level.
Referenced by main().
| const char * bj_get_log_level_string | ( | int | level | ) |
Returns a string describing the given level.
The function is only valid if called with BJ_LOG_TRACE, BJ_LOG_DEBUG, BJ_LOG_INFO, BJ_LOG_WARN, BJ_LOG_ERROR or BJ_LOG_FATAL.
The returned value is owned by the callee.
| level | The log level to get the description from. |
| size_t bj_log_message | ( | int | level, |
| const char * | file, | ||
| int | line, | ||
| const char * | format, | ||
| ... ) |
Generic message reporting function.
Sends a message using the given level. Message will only be reported if level is at least the value set as a parameter to bj_set_log_level (or BJ_LOG_TRACE if never called).
The more convenient functions bj_log_msg, bj_trace, ... should be used for simplicity.
The string formatting follows standard printf signature and behaviour.
| level | The log level the message is reported in. |
| file | The file name the message is reported from. |
| line | The line number the message is reported from. |
| format,... | The formatted string to report. |
The log message will have the following format:
For example: 12:23:34 WARN: logging.c:27 Warning level message
The maximum length a log message will take is hardcoded to the value set for BJ_MAXIMUM_LOG_LEN.
A first buffer is filled in with the expansion of the message. This message is truncated to fit the BJ_MAXIMUM_LOG_LEN characters limit entirely. If this limit is already reached, the message is logged as-is.
Then, in the order of below priority:
| void bj_set_log_level | ( | int | level | ) |
Sets the default log level.
Once set, any call to bj_log_message, bj_log_msg and helper macros will only report a message is its level is at least level.
| level | The level to set. |
If not set, default is BJ_LOG_TRACE.
Referenced by main().