Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
logging.c
Go to the documentation of this file.
1
10
11#include <banjo/assert.h>
12#include <banjo/main.h>
13#include <banjo/log.h>
14
15int main(int argc, char* argv[]) {
16 (void)argc;
17 (void)argv;
18
19 // Banjo has six log levels in ascending severity:
20 // BJ_LOG_TRACE (0) - Detailed trace information
21 // BJ_LOG_DEBUG (1) - Debug information
22 // BJ_LOG_INFO (2) - Informational messages
23 // BJ_LOG_WARN (3) - Warning messages
24 // BJ_LOG_ERROR (4) - Error messages
25 // BJ_LOG_FATAL (5) - Fatal error messages
26 //
27 // The default level is TRACE (0), which displays all messages.
28 const int default_level = bj_get_log_level();
29 bj_assert(default_level == 0);
30 bj_info("Default log level: %d\n", default_level);
31
32 // Change the minimum severity level that will be displayed. Messages below
33 // this level are silently discarded. Here we set it to INFO, so TRACE and
34 // DEBUG messages won't appear.
36
37 // The generic bj_log_msg(level, ...) function logs at any severity.
38 // Only messages at or above the current level (INFO) will display.
39 bj_log_msg(TRACE, "Trace level (won't display)");
40 bj_log_msg(INFO, "Information level message");
41 bj_log_msg(WARN, "Warning level message");
42
43 // Convenience functions are provided for each level: bj_trace(), bj_debug(),
44 // bj_info(), bj_warn(), bj_err(), bj_fatal(). These are clearer than using
45 // the generic bj_log_msg().
46 bj_err("This is an error message");
47
48 // All logging functions support printf-style formatting with variable
49 // arguments. They return the number of characters written (excluding the
50 // null terminator), which can be useful for buffer management or testing.
51 size_t written = bj_warn("Room #%d is closed, but you have '%s'", 42, "The Key Item");
52
53 bj_info("Previous log message was written in %ld characters (excluding '\\0')", written);
54 return 0;
55}
Assertion facility for Banjo API.
#define bj_assert(expr)
Runtime assertion macro.
Definition assert.h:34
int main(int argc, char *argv[])
Definition audio_pcm.c:177
int bj_get_log_level(void)
Gets the current log level set by bj_set_log_level.
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
#define bj_warn(...)
Log a message using the BJ_LOG_WARN level.
Definition log.h:155
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:169
void bj_set_log_level(int level)
Sets the default log level.
#define bj_log_msg(LEVEL,...)
Log a message using the given level LEVEL.
Definition log.h:96
@ BJ_LOG_INFO
Informational messages about execution.
Definition log.h:69
Logging utility functions.
Portable main() replacement with platform-aware entry shim.