Libecoli 0.11.1
Extensible COmmand LIne library
Loading...
Searching...
No Matches
Log

Log API. More...

Data Structures

struct  ec_log_type

Macros

#define EC_LOG_TYPE_REGISTER(name)
#define EC_LOG(level, args...)
#define EC_VLOG(level, fmt, ap)

Typedefs

typedef int(* ec_log_t) (int type, enum ec_log_level level, void *opaque, const char *str)

Enumerations

enum  ec_log_level {
  EC_LOG_EMERG = 0 , EC_LOG_ALERT = 1 , EC_LOG_CRIT = 2 , EC_LOG_ERR = 3 ,
  EC_LOG_WARNING = 4 , EC_LOG_NOTICE = 5 , EC_LOG_INFO = 6 , EC_LOG_DEBUG = 7
}

Functions

int ec_log_fct_register (ec_log_t usr_log, void *opaque)
int ec_log_type_register (struct ec_log_type *type)
const char * ec_log_name (int type)
int ec_log (int type, enum ec_log_level level, const char *format,...) __attribute__((format(__printf__
int int ec_vlog (int type, enum ec_log_level level, const char *format, va_list ap)
int ec_log_default_cb (int type, enum ec_log_level level, void *opaque, const char *str)
int ec_log_level_set (enum ec_log_level level)
enum ec_log_level ec_log_level_get (void)

Detailed Description

Log API.

This file provides logging helpers:

  • logging functions, supporting printf-like format
  • several debug levels (similar to syslog)
  • named log types
  • redirection of logs to user functions (default logs nothing)

Macro Definition Documentation

◆ EC_LOG_TYPE_REGISTER

#define EC_LOG_TYPE_REGISTER ( name)
Value:
static struct ec_log_type name##_log_type = { \
.n##ame = #name, \
}; \
static int ec_log_local_type; \
__attribute__((constructor, used)) static void ec_log_register_##name(void) \
{ \
ec_log_local_type = ec_log_type_register(&name##_log_type); \
ec_assert_print(ec_log_local_type >= 0, "cannot register log type.\n"); \
}
int ec_log_type_register(struct ec_log_type *type)

Register a log type.

This macro defines a function that will be called at startup (using the "constructor" attribute). This function registers the named type passed as argument, and sets a static global variable ec_log_local_type. This variable is used as the default log type for this file when using EC_LOG() or EC_VLOG().

This macro can be present several times in a file. In this case, the local log type is set to the last registered type.

On error, the function aborts.

Parameters
nameThe name of the log to be registered.

Definition at line 69 of file log.h.

◆ EC_LOG

#define EC_LOG ( level,
args... )
Value:
ec_log(ec_log_local_type, level, args)
int ec_log(int type, enum ec_log_level level, const char *format,...) __attribute__((format(__printf__

Log a formatted string using the local log type.

This macro requires that a log type is previously registered with EC_LOG_TYPE_REGISTER() since it uses the ec_log_local_type variable.

Parameters
levelThe log level.
argsThe format string, followed by optional arguments.
Returns
0 on success, -1 on error (errno is set).

Definition at line 184 of file log.h.

◆ EC_VLOG

#define EC_VLOG ( level,
fmt,
ap )
Value:
ec_vlog(ec_log_local_type, level, fmt, ap)
int int ec_vlog(int type, enum ec_log_level level, const char *format, va_list ap)

Log a formatted string using the local log type.

This macro requires that a log type is previously registered with EC_LOG_TYPE_REGISTER() since it uses the ec_log_local_type variable.

Parameters
levelThe log level.
fmtThe format string.
apThe list of arguments.
Returns
0 on success, -1 on error (errno is set).

Definition at line 202 of file log.h.

Typedef Documentation

◆ ec_log_t

typedef int(* ec_log_t) (int type, enum ec_log_level level, void *opaque, const char *str)

User log function type.

It is advised that a user-defined log function drops all messages that are less critical than ec_log_level_get(), as done by the default handler.

Parameters
typeThe log type identifier.
levelThe log level.
opaqueThe opaque pointer that was passed to ec_log_fct_register().
strThe string to log.
Returns
0 on success, -1 on error (errno is set).

Definition at line 98 of file log.h.

Enumeration Type Documentation

◆ ec_log_level

Log levels, from most critical to least critical.

Enumerator
EC_LOG_EMERG 

system is unusable

EC_LOG_ALERT 

action must be taken immediately

EC_LOG_CRIT 

critical conditions

EC_LOG_ERR 

error conditions

EC_LOG_WARNING 

warning conditions

EC_LOG_NOTICE 

normal but significant condition

EC_LOG_INFO 

informational

EC_LOG_DEBUG 

debug-level messages

Definition at line 29 of file log.h.

Function Documentation

◆ ec_log_fct_register()

int ec_log_fct_register ( ec_log_t usr_log,
void * opaque )

Register a user log function.

Parameters
usr_logFunction pointer that will be invoked for each log call. If the parameter is NULL, ec_log_default_cb() is used.
opaqueOpaque pointer passed to the log function.
Returns
0 on success, -1 on error (errno is set).

◆ ec_log_type_register()

int ec_log_type_register ( struct ec_log_type * type)

Register a named log type.

Register a new log type, which is identified by its name. The function returns a log identifier associated with the log name. If the name is already registered, the function just returns its identifier.

Parameters
typeThe log type to register.
Returns
The log type identifier on success (positive or zero), -1 on error (errno is set).

◆ ec_log_name()

const char * ec_log_name ( int type)

Return the log name associated with the log type identifier.

Parameters
typeThe log type identifier.
Returns
The name associated with the log type, or "unknown". It always returns a valid string (never NULL).

◆ ec_log()

int ec_log ( int type,
enum ec_log_level level,
const char * format,
... )

Log a formatted string.

Parameters
typeThe log type identifier.
levelThe log level.
formatThe format string, followed by optional arguments.
Returns
0 on success, -1 on error (errno is set).

◆ ec_vlog()

int int ec_vlog ( int type,
enum ec_log_level level,
const char * format,
va_list ap )

Log a formatted string.

Parameters
typeThe log type identifier.
levelThe log level.
formatThe format string.
apThe list of arguments.
Returns
0 on success, -1 on error (errno is set).

◆ ec_log_default_cb()

int ec_log_default_cb ( int type,
enum ec_log_level level,
void * opaque,
const char * str )

Default log handler.

This is the default log function that is used by the library. By default, it prints all logs whose level is WARNING or more critical. This level can be changed with ec_log_level_set().

Parameters
typeThe log type identifier.
levelThe log level.
opaqueUnused.
strThe string to be logged.
Returns
0 on success, -1 on error (errno is set).

◆ ec_log_level_set()

int ec_log_level_set ( enum ec_log_level level)

Set the global log level.

This level is used by the default log handler, ec_log_default_cb(). All messages that are at least as critical as the default level are displayed.

Parameters
levelThe log level to be set.
Returns
0 on success, -1 on error.

◆ ec_log_level_get()

enum ec_log_level ec_log_level_get ( void )

Get the global log level.

This level is used by the default log handler, ec_log_default_cb(). All messages that are at least as critical as the default level are displayed.

Returns
The current global log level