osd_log class¶
Common logging functionality.
Usage¶
#include <osd/osd.h>
Public Interface¶
-
typedef void
(* osd_log_fn)(struct osd_log_ctx *ctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args)¶ Logging function template
Implement a function with this signature and pass it to set_log_fn() if you want to implement custom logging.
-
osd_result
osd_log_new(struct osd_log_ctx ** ctx, int log_priority, osd_log_fn log_fn)¶ Create a new instance of osd_log_ctx
You may use the resulting log context on multiple threads.
- See
- osd_log_set_priority()
- See
- osd_log_set_fn()
- Parameters
ctx: the logging contextlog_priority: filter: only log messages greater or equal the given priority. Use on the LOG_* constants in stdlog.h Set to 0 to use the default logging priority.log_fn: logging callback. Set to NULL to disable logging output.
-
void
osd_log_free(struct osd_log_ctx ** ctx_p)¶ Free and NULL an osd_log_ctx object
- Parameters
ctx_p: the log context
-
void
osd_log_set_fn(struct osd_log_ctx * ctx, osd_log_fn log_fn)¶ Set logging function
The built-in logging writes to STDERR. It can be overridden by a custom function to log messages into the user’s logging functionality.
In many cases you want the log message to be associated with a context or object of your application, i.e. the object that uses OSD. In this case, set the context or
thispointer with osd_log_set_caller_ctx() and retrieve it inside yourlog_fn.An example in C++ could look like this:
static void MyClass::osdLogCallback(struct osd_log_ctx *gctx, int priority, const char *file, int line, const char *fn, const char *format, va_list args) { MyClass *myclassptr = static_cast<MyClass*>(osd_get_caller_ctx(gctx)); myclassptr->doLogging(format, args); } MyClass::MyClass() { // ... osd_log_set_caller_ctx(gctx, this); osd_log_set_fn(&MyClass::osdLogCallback); // ... } MyClass::doLogging(const char* format, va_list args) { printf("this = %p", this); vprintf(format, args); }
- Parameters
ctx: the log contextlog_fn: the used logging function
-
int
osd_log_get_priority(struct osd_log_ctx * ctx)¶ Get the logging priority
The logging priority is the lowest message type that is reported.
- Return
- the log priority
- See
- osd_log_set_priority()
- Parameters
ctx: the log context
-
void
osd_log_set_priority(struct osd_log_ctx * ctx, int priority)¶ Set the logging priority
The logging priority is the lowest message type that is reported.
Allowed values for
priorityareLOG_DEBUG,LOG_INFOandLOG_ERRas defined insyslog.h.For example setting
prioritywill toLOG_INFOwill result in all error and info messages to be shown, and all debug messages to be discarded.- See
- osd_log_get_priority()
- Parameters
ctx: the log contextpriority: new priority value
-
void
osd_log_set_caller_ctx(struct osd_log_ctx * ctx, void * caller_ctx)¶ Set a caller context pointer
This library does not use this pointer in any way, you’re free to set it to whatever your application needs.
- See
- osd_log_get_caller_ctx()
- See
- osd_log_set_fn() for a code example using this functionality
- Parameters
ctx: the log contextcaller_ctx: the caller context pointer
-
void*
osd_log_get_caller_ctx(struct osd_log_ctx * ctx)¶ Get the caller context pointer
- Return
- the caller context pointer
- See
- osd_log_set_caller_ctx()
- Parameters
ctx: the log context