Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
logging.c

Demonstrate the use of log facilities.

Demonstrate the use of log facilities

#include <banjo/assert.h>
#include <banjo/main.h>
#include <banjo/log.h>
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
// Usually, log levels go from 0 to 5 in that order:
// BJ_LOG_TRACE < BJ_LOG_DEBUG < BJ_LOG_INFO,
// BJ_LOG_WARN < BJ_LOG_ERROR < BJ_LOG_FATAL.
// The default log level on application start is 0 (TRACE)
const int default_level = bj_log_get_level();
bj_assert(default_level == 0);
bj_info("Default log level: %d\n", default_level);
// To set the current log level:
// Since level are signed int, you can provide any custom log level outside
// the range [0;5].
// Any message sent in a level equal to higher than the current will
// output:
bj_log(TRACE, "Trace level (won't display)");
bj_log(INFO, "Information level message");
bj_log(WARN, "Warning level message");
// Also ther is bj_info(), bj_trace(), etc:
bj_err("This is an error message");
// Logging functions accept variable arguments and string formatting a-la printf:
size_t written = bj_warn("Room #%d is closed, but you have '%s'", 42, "The Key Item");
// Written contains the number of characters actually written:
bj_info("Previous log message was written in %ld characters (excluding '\\0')", written);
}
void bj_log_set_level(int level)
Sets the default log level.
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:103
#define bj_warn(...)
Log a message using the BJ_LOG_WARN level.
Definition log.h:117
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:131
int bj_log_get_level(void)
Gets the current log level set by bj_log_set_level.
#define bj_log(LEVEL,...)
Log a message using the given level LEVEL.
Definition log.h:60
@ BJ_LOG_INFO
Informational messages about execution.
Definition log.h:34
Logging utility functions.