bio
 
Loading...
Searching...
No Matches
Logging

Write text to console or file. More...

Modules

 File logger
 A logger that writes messages to a file.
 

Data Structures

struct  bio_logger_t
 Handle to a logger. More...
 
struct  bio_log_options_t
 Logging options. More...
 
struct  bio_log_ctx_t
 Context for a log function. More...
 

Macros

#define BIO_LOG(LEVEL, ...)    (BIO_FORMAT_CHECK(__VA_ARGS__), bio_log(LEVEL, __FILE__, __LINE__, __VA_ARGS__))
 Log a message at a given log level.
 
#define BIO_TRACE(...)   BIO_LOG(BIO_LOG_LEVEL_TRACE, __VA_ARGS__)
 Log a message at BIO_LOG_LEVEL_TRACE level.
 
#define BIO_DEBUG(...)   BIO_LOG(BIO_LOG_LEVEL_DEBUG, __VA_ARGS__)
 Log a message at BIO_LOG_LEVEL_DEBUG level.
 
#define BIO_INFO(...)   BIO_LOG(BIO_LOG_LEVEL_INFO , __VA_ARGS__)
 Log a message at BIO_LOG_LEVEL_DEBUG level.
 
#define BIO_WARN(...)   BIO_LOG(BIO_LOG_LEVEL_WARN , __VA_ARGS__)
 Log a message at BIO_LOG_LEVEL_WARN level.
 
#define BIO_ERROR(...)   BIO_LOG(BIO_LOG_LEVEL_ERROR, __VA_ARGS__)
 Log a message at BIO_LOG_LEVEL_ERROR level.
 
#define BIO_FATAL(...)   BIO_LOG(BIO_LOG_LEVEL_FATAL, __VA_ARGS__)
 Log a message at BIO_LOG_LEVEL_FATAL level.
 
#define BIO_ERROR_FMT   "%s (%s[%d]) (from %s:%d)"
 Helper for logging a bio_error_t.
 
#define BIO_ERROR_FMT_ARGS(error)
 Helper for logging a bio_error_t.
 

Typedefs

typedef void(* bio_log_fn_t) (void *userdata, const bio_log_ctx_t *ctx, const char *msg)
 A log callback.
 

Enumerations

enum  bio_log_level_t {
  BIO_LOG_LEVEL_TRACE , BIO_LOG_LEVEL_DEBUG , BIO_LOG_LEVEL_INFO , BIO_LOG_LEVEL_WARN ,
  BIO_LOG_LEVEL_ERROR , BIO_LOG_LEVEL_FATAL
}
 Log levels. More...
 

Functions

void bio_log (bio_log_level_t level, const char *file, int line, const char *fmt,...)
 Raw logging function.
 
bio_logger_t bio_add_logger (bio_log_level_t min_level, bio_log_fn_t log_fn, void *userdata)
 Add a new logger.
 
void bio_remove_logger (bio_logger_t logger)
 Remove a previously configured logger.
 
void bio_set_min_log_level (bio_logger_t logger, bio_log_level_t level)
 Change the minimum log level for a given logger.
 

Detailed Description

Write text to console or file.

There are several logging macros that should be used instead of the raw bio_log function.

Remarks
By default, there is no logger configured. See File logger.
See also
bio_options_t::log_options

Macro Definition Documentation

◆ BIO_DEBUG

#define BIO_DEBUG (   ...)    BIO_LOG(BIO_LOG_LEVEL_DEBUG, __VA_ARGS__)

Log a message at BIO_LOG_LEVEL_DEBUG level.

◆ BIO_ERROR

#define BIO_ERROR (   ...)    BIO_LOG(BIO_LOG_LEVEL_ERROR, __VA_ARGS__)

Log a message at BIO_LOG_LEVEL_ERROR level.

◆ BIO_ERROR_FMT

#define BIO_ERROR_FMT   "%s (%s[%d]) (from %s:%d)"

Helper for logging a bio_error_t.

Example:

bio_error_t error = { 0 };
if (!bio_fstat(file, &stat, &error)) {
BIO_ERROR("Error while stat-ing: " BIO_ERROR_FMT, BIO_ERROR_FMT_ARGS(&error));
}
bool bio_fstat(bio_file_t file, bio_stat_t *stat, bio_error_t *error)
Retrieves statistics about a file.
#define BIO_ERROR(...)
Log a message at BIO_LOG_LEVEL_ERROR level.
Definition bio.h:54
#define BIO_ERROR_FMT_ARGS(error)
Helper for logging a bio_error_t.
Definition bio.h:80
#define BIO_ERROR_FMT
Helper for logging a bio_error_t.
Definition bio.h:73
An error returned from a function.
Definition bio.h:468
See also
BIO_ERROR_FMT_ARGS

◆ BIO_ERROR_FMT_ARGS

#define BIO_ERROR_FMT_ARGS (   error)
Value:
(bio_has_error((error)) ? bio_strerror((error)) : "No error"), \
(bio_has_error((error)) ? (error)->tag->name : "bio.error.core"), \
((error)->code), \
((error)->file ? (error)->file : "<no source info>"), \
((error)->line)
static bool bio_has_error(bio_error_t *error)
Convenient function to check whether an error was encountered.
Definition bio.h:1281
static const char * bio_strerror(bio_error_t *error)
Convenient function to format an error into a string.
Definition bio.h:1300

Helper for logging a bio_error_t.

See also
BIO_ERROR_FMT

◆ BIO_FATAL

#define BIO_FATAL (   ...)    BIO_LOG(BIO_LOG_LEVEL_FATAL, __VA_ARGS__)

Log a message at BIO_LOG_LEVEL_FATAL level.

◆ BIO_INFO

#define BIO_INFO (   ...)    BIO_LOG(BIO_LOG_LEVEL_INFO , __VA_ARGS__)

Log a message at BIO_LOG_LEVEL_DEBUG level.

◆ BIO_LOG

#define BIO_LOG (   LEVEL,
  ... 
)     (BIO_FORMAT_CHECK(__VA_ARGS__), bio_log(LEVEL, __FILE__, __LINE__, __VA_ARGS__))

Log a message at a given log level.

BIO_LOG(BIO_LOG_LEVEL_INFO, "Hello %s", "world");
#define BIO_LOG(LEVEL,...)
Log a message at a given log level.
Definition bio.h:38
@ BIO_LOG_LEVEL_INFO
Info message.
Definition bio.h:569
Parameters
LEVELa bio_log_level_t
...The log message. This can use a printf-like format string

◆ BIO_TRACE

#define BIO_TRACE (   ...)    BIO_LOG(BIO_LOG_LEVEL_TRACE, __VA_ARGS__)

Log a message at BIO_LOG_LEVEL_TRACE level.

◆ BIO_WARN

#define BIO_WARN (   ...)    BIO_LOG(BIO_LOG_LEVEL_WARN , __VA_ARGS__)

Log a message at BIO_LOG_LEVEL_WARN level.

Typedef Documentation

◆ bio_log_fn_t

typedef void(* bio_log_fn_t) (void *userdata, const bio_log_ctx_t *ctx, const char *msg)

A log callback.

This will be called for every message passed to bio_log.

Remarks
When bio_remove_logger is called, this will be invoked with ctx and msg set to NULL instead. The callback should perform cleanup or "flush" buffered messages.
Parameters
userdataArbitrary user data
ctxLog context
msgLog message
See also
bio_add_logger

Enumeration Type Documentation

◆ bio_log_level_t

Log levels.

Enumerator
BIO_LOG_LEVEL_TRACE 

Tracing message, for debugging.

BIO_LOG_LEVEL_DEBUG 

Debug message.

BIO_LOG_LEVEL_INFO 

Info message.

BIO_LOG_LEVEL_WARN 

Warning.

BIO_LOG_LEVEL_ERROR 

Error but usually recoverable.

BIO_LOG_LEVEL_FATAL 

Fatal, the program usually terminates after this.

Function Documentation

◆ bio_add_logger()

bio_logger_t bio_add_logger ( bio_log_level_t  min_level,
bio_log_fn_t  log_fn,
void *  userdata 
)

Add a new logger.

The logger will run in its own daemon coroutine.

bio will automatically call bio_remove_logger on all loggers when bio_terminate is called. This will terminate the daemon coroutine.

Parameters
min_levelMinimum log level for this logger
log_fnThe log function
userdataArbitrary data passed to the log function for every message
Returns
A logger handle

◆ bio_log()

void bio_log ( bio_log_level_t  level,
const char *  file,
int  line,
const char *  fmt,
  ... 
)

Raw logging function.

Use the logging macros instead.

◆ bio_remove_logger()

void bio_remove_logger ( bio_logger_t  logger)

Remove a previously configured logger.

The log function will be called with NULL arguments, except for userdata. This allows it to have a final chance to "flush" all buffered messages.

◆ bio_set_min_log_level()

void bio_set_min_log_level ( bio_logger_t  logger,
bio_log_level_t  level 
)

Change the minimum log level for a given logger.