libcgroup
Loading...
Searching...
No Matches
7. Logging

Logging

Libcgroup allows applications to register a callback function which libcgroup will call when it wants to log something. Each log message has associated a log level. As described in previous chapter, most libcgroup functions return an error code, which described root cause of the failure and log messages might provide further details about these failures and other notable events.

The logging callback can be set at any time, but setting the callback before any other libcgroup function (including cgroup_init()) is highly recommended. If no logger is set before cgroup_init() is called, default logger is automatically set, logging CGROUP_LOG_ERROR messages to stdout.
Setting log level
Some of the functions below set the log level as integer. Application can set directly a value of enum cgroup_log_level or use value -1 to set the log level automatically. In this case, libcgroup inspects environment variable CGROUP_LOGLEVEL if it is set and contains any of these values: ERROR, WARNING, INFO, DEBUG or integer number representing value from enum cgroup_log_level. If CGROUP_LOGLEVEL is not set or its value is not valid, CGROUP_LOG_ERROR is set as default log level.
Example:
Following short example shows custom libcgroup logger sending all log messages to stderr:
static void my_logger(void *userdata, int level, const char *fmt, va_list ap)
{
vfprintf(stderr, fmt, ap);
}
int main(int argc, char **argv)
{
int ret;
cgroup_set_logger(my_logger, -1, NULL);
ret = cgroup_init();
if (ret) {
...
}
...
void cgroup_set_logger(cgroup_logger_callback logger, int loglevel, void *userdata)
Definition log.c:42
enum  cgroup_log_level {
  CGROUP_LOG_CONT = 0 , CGROUP_LOG_ERROR , CGROUP_LOG_WARNING , CGROUP_LOG_INFO ,
  CGROUP_LOG_DEBUG
}
 
typedef void(* cgroup_logger_callback) (void *userdata, int level, const char *fmt, va_list ap)
 
void cgroup_set_logger (cgroup_logger_callback logger, int loglevel, void *userdata)
 
void cgroup_set_default_logger (int loglevel)
 
void cgroup_set_loglevel (int loglevel)
 
int cgroup_get_loglevel (void)
 
void cgroup_log (int loglevel, const char *fmt,...)
 
int cgroup_parse_log_level_str (const char *levelstr)
 

Detailed Description

Enumeration Type Documentation

◆ cgroup_log_level

Level of importance of a log message.

Enumerator
CGROUP_LOG_CONT 

Continue printing the log message, with the previous log level. Used to print log messages without the line break.

CGROUP_LOG_ERROR 

Something serious happened and libcgroup failed to perform requested operation.

CGROUP_LOG_WARNING 

Something bad happened but libcgroup recovered from the error.

CGROUP_LOG_INFO 

Something interesting happened and the message might be useful to the user.

CGROUP_LOG_DEBUG 

Debugging messages useful to libcgroup developers.

Function Documentation

◆ cgroup_get_loglevel()

int cgroup_get_loglevel ( void )
extern

Retrieve the current loglevel.

Returns
the current loglevel from with libcgroup

◆ cgroup_log()

void cgroup_log ( int loglevel,
const char * fmt,
... )
extern

Libcgroup log function. This is for applications which are too lazy to set up their own complex logging and miss-use libcgroup for that purpose. I.e. this function should be used only by simple command-line tools. This logging automatically benefits from CGROUP_LOGLEVEL env. variable.

◆ cgroup_parse_log_level_str()

int cgroup_parse_log_level_str ( const char * levelstr)
extern

Parse levelstr string for information about desired loglevel. The levelstr is usually a value of the CGROUP_LOGLEVEL environment variable.

Parameters
levelstrString containing desired loglevel.

◆ cgroup_set_default_logger()

void cgroup_set_default_logger ( int loglevel)
extern

Set libcgroup logging to stdout. All messages with the given loglevel or below will be sent to standard output. Previous logger set by cgroup_set_logger() is replaced.

Parameters
loglevelThe log level. Use value -1 to automatically discover the level from CGROUP_LOGLEVEL environment variable.

◆ cgroup_set_logger()

void cgroup_set_logger ( cgroup_logger_callback logger,
int loglevel,
void * userdata )
extern

Set libcgroup logging callback. All log messages with equal or lower log level will be sent to the application's callback. There can be only one callback logger set, the previous callback is replaced with the new one by calling this function. Use NULL as the logger callback to completely disable libcgroup logging.

Parameters
loggerThe callback.
loglevelThe log level. Use value -1 to automatically discover the level from CGROUP_LOGLEVEL environment variable.
userdataApplication's data which will be provided back to the callback.

◆ cgroup_set_loglevel()

void cgroup_set_loglevel ( int loglevel)
extern

Change current loglevel.

Parameters
loglevelThe log level. Use value -1 to automatically discover the level from CGROUP_LOGLEVEL environment variable.