Logging

Logging — Printing log messages to files, to syslog or to the terminal

Synopsis


#include <monitor/monitor.h>


void        (*mon_log_cb)                   (int level,
                                             const char *fmt,
                                             va_list ap);
void        mon_log_set_cb                  (mon_log_cb cb);
mon_log_cb  mon_log_get_cb                  (void);
void        mon_log                         (int level,
                                             const char *fmt,
                                             ...);
void        mon_vlog                        (int level,
                                             const char *fmt,
                                             va_list ap);
int         mon_log_set_level               (int level);
int         mon_log_set_level_str           (const char *level_str);
void        mon_log_to_stderr               (int level,
                                             const char *fmt,
                                             va_list ap);
void        mon_log_default                 (int level,
                                             const char *fmt,
                                             va_list ap);
void        mon_log_default_close           (int freeall);
void        mon_log_default_reopen          (void);
int         mon_log_default_parse_cfg       (mon_cfg_node *cfg,
                                             const char *def_ident,
                                             const char *def_target,
                                             int def_level);
void        mon_log_default_parse_env       (void);
int         mon_log_default_set_ident       (const char *ident);
int         mon_log_default_set_target      (const char *logtarget);

int         mon_loginfo_register            (const char *name,
                                             int *addr);
void        mon_loginfo_unregister          (const char *name);
void        mon_loginfo_set                 (const char *name,
                                             int value);
void        mon_loginfo_set_default         (int value);

Description

Details

mon_log_cb ()

void        (*mon_log_cb)                   (int level,
                                             const char *fmt,
                                             va_list ap);

Specifies the type of the function which is passed to mon_log_set_cb().

This function is passed a log level and a message.

level :the log level.
fmt :the format string.
ap :a va_list pointing to the arguments.

mon_log_set_cb ()

void        mon_log_set_cb                  (mon_log_cb cb);

Sets the log callback to the specified function.

cb :the log callback function.

mon_log_get_cb ()

mon_log_cb  mon_log_get_cb                  (void);

Returns the current log callback function.

Returns :the log callback function.

mon_log ()

void        mon_log                         (int level,
                                             const char *fmt,
                                             ...);

Logs a message, similar to syslog. If the level is higher than the current log level, the message is passed to the callback function set by mon_log_set_cb(); otherwise the message is dropped.

level :the severity of the message.
fmt :format string.
... :format parameters.

mon_vlog ()

void        mon_vlog                        (int level,
                                             const char *fmt,
                                             va_list ap);

Same as mon_log, but accepts a va_list instead of a variable number of arguments.

level :the severity of the message.
fmt :format string.
ap :format parameters.

mon_log_set_level ()

int         mon_log_set_level               (int level);

Sets the log level. Messages that are less severe than the log level will not be logged.

level :the log level.
Returns :0 if successful or EINVAL if level is invalid.

mon_log_set_level_str ()

int         mon_log_set_level_str           (const char *level_str);

Sets the log level parsed from a string. The known log levels in decreasing priority order are "crit", "err", "warning", "notice", "info", and "debug".

level_str :the log level in string format.
Returns :0 if successful or EINVAL if level_str is not a valid log level.

mon_log_to_stderr ()

void        mon_log_to_stderr               (int level,
                                             const char *fmt,
                                             va_list ap);

Logs messages to the standard error. The address of this function can be passed to mon_log_set_cb().

level :the log level. It is ignored by this function.
fmt :the format string.
ap :a va_list containing the format parameters.

mon_log_default ()

void        mon_log_default                 (int level,
                                             const char *fmt,
                                             va_list ap);

Implements the default log callback function used by most of the monitoring system. It supports user-modifyable log target and log identity settings. The address of this function can be passed to mon_log_set_cb().

level :the log level.
fmt :the format string.
ap :a va_list containing the format parameters.

mon_log_default_close ()

void        mon_log_default_close           (int freeall);

Closes the log target of the mon_log_default() callback.

freeall :if TRUE, all internal resources (like the log identifier or the target descriptor) will be deallocated.

mon_log_default_reopen ()

void        mon_log_default_reopen          (void);

Re-opens the log target used by the mon_log_default() callback.


mon_log_default_parse_cfg ()

int         mon_log_default_parse_cfg       (mon_cfg_node *cfg,
                                             const char *def_ident,
                                             const char *def_target,
                                             int def_level);

Sets up logging based on values in a configuration tree. The log target is set to the value of the "LogTarget" configuration key, the identity to the value of the "LogIdent" key, and the log level to the value of the "LogLevel" key. If one or more of these keys are missing, the respective default value as specified in def_ident, def_target and def_level will be used. See mon_log_set_level_str() for the possible values of the "LogLevel" key. See mon_log_default_set_target() for the format of the "LogTarget" key.

cfg :the root of the configuration tree.
def_ident :default identity string if the configuration does not contain the "LogIdent" key.
def_target :default log target if the configuration does not contain the "LogTarget" key.
def_level :default log level if the configuration does not contain the "LogLevel" key.
Returns :0 if successfull or an error code.

mon_log_default_parse_env ()

void        mon_log_default_parse_env       (void);

Sets up logging based on the value of an environment variable. The monitoring system uses the MERCURY_OPTIONS variable by default.


mon_log_default_set_ident ()

int         mon_log_default_set_ident       (const char *ident);

Sets the identity string used in log messages.

ident :the identity string.
Returns :0 if successful or an error code.

mon_log_default_set_target ()

int         mon_log_default_set_target      (const char *logtarget);

Sets the log target. logtarget must have the form "selector:[target]". The known selectors are:

  • syslog: send messages to syslog. If the target is not empty, it contains the facility to be used: "daemon", "user", or "local0" through "local7".

  • stderr: send messages to the standard error. target is ignored and should be left empty.

  • file: send messages to the file target. It is advised to always specify the full path.

logtarget :the log target.
Returns :0 if successful or an error code.

mon_loginfo_register ()

int         mon_loginfo_register            (const char *name,
                                             int *addr);

Register a per-component log level indicator. If mon_loginfo_set() was called previously for the component, the log level will be set automatically.

name :the name of the component.
addr :pointer to the log level variable.
Returns :0 if successful or an error code.

mon_loginfo_unregister ()

void        mon_loginfo_unregister          (const char *name);

Unregisters a per-component log level indicator.

name :the name of the component.

mon_loginfo_set ()

void        mon_loginfo_set                 (const char *name,
                                             int value);

Sets a per-component log level indicator. May be called before the indicator is registered; in this case, the log level will be set when mon_loginfo_register() is called for the component.

name :the name of the component.
value :the desired log level for the component.

mon_loginfo_set_default ()

void        mon_loginfo_set_default         (int value);

Set the default log level to use when no explicit mon_loginfo_set() was called for a component.

value :the desired log level.