1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
8 * This file provide logging helpers:
9 * - logging functions, supporting printf-like format
10 * - several debug level (similar to syslog)
12 * - redirection of log to a user functions (default logs nothing)
20 #include <ecoli_assert.h>
23 EC_LOG_EMERG = 0, /* system is unusable */
24 EC_LOG_ALERT = 1, /* action must be taken immediately */
25 EC_LOG_CRIT = 2, /* critical conditions */
26 EC_LOG_ERR = 3, /* error conditions */
27 EC_LOG_WARNING = 4, /* warning conditions */
28 EC_LOG_NOTICE = 5, /* normal but significant condition */
29 EC_LOG_INFO = 6, /* informational */
30 EC_LOG_DEBUG = 7, /* debug-level messages */
34 * Register a log type.
36 * This macro defines a function that will be called at startup (using
37 * the "constructor" attribute). This function register the named type
38 * passed as argument, and sets a static global variable
39 * "ec_log_local_type". This variable is used as the default log type
40 * for this file when using EC_LOG() or EC_VLOG().
42 * This macro can be present several times in a file. In this case, the
43 * local log type is set to the last registered type.
45 * On error, the function aborts.
48 * The name of the log to be registered.
50 #define EC_LOG_TYPE_REGISTER(name) \
51 static int name##_log_type; \
52 static int ec_log_local_type; \
53 __attribute__((constructor, used)) \
54 static void ec_log_register_##name(void) \
56 ec_log_local_type = ec_log_type_register(#name); \
57 ec_assert_print(ec_log_local_type >= 0, \
58 "cannot register log type.\n"); \
59 name##_log_type = ec_log_local_type; \
63 * User log function type.
65 * It is advised that a user-defined log function drops all messages
66 * that are at least as critical as ec_log_level_get(), as done by the
70 * The log type identifier.
74 * The opaque pointer that was passed to ec_log_fct_register().
78 * 0 on success, -1 on error (errno is set).
80 typedef int (*ec_log_t)(int type, enum ec_log_level level, void *opaque,
84 * Register a user log function.
87 * Function pointer that will be invoked for each log call.
88 * If the parameter is NULL, ec_log_default_cb() is used.
90 * Opaque pointer passed to the log function.
92 * 0 on success, -1 on error (errno is set).
94 int ec_log_fct_register(ec_log_t usr_log, void *opaque);
97 * Register a named log type.
99 * Register a new log type, which is identified by its name. The
100 * function returns a log identifier associated to the log name. If the
101 * name is already registered, the function just returns its identifier.
104 * The name of the log type.
106 * The log type identifier on success (positive or zero), -1 on
107 * error (errno is set).
109 int ec_log_type_register(const char *name);
112 * Return the log name associated to the log type identifier.
115 * The log type identifier.
117 * The name associated to the log type, or "unknown". It always return
118 * a valid string (never NULL).
120 const char *ec_log_name(int type);
123 * Log a formatted string.
126 * The log type identifier.
130 * The format string, followed by optional arguments.
132 * 0 on success, -1 on error (errno is set).
134 int ec_log(int type, enum ec_log_level level, const char *format, ...)
135 __attribute__((format(__printf__, 3, 4)));
138 * Log a formatted string.
141 * The log type identifier.
147 * The list of arguments.
149 * 0 on success, -1 on error (errno is set).
151 int ec_vlog(int type, enum ec_log_level level, const char *format, va_list ap);
154 * Log a formatted string using the local log type.
156 * This macro requires that a log type is previously register with
157 * EC_LOG_TYPE_REGISTER() since it uses the "ec_log_local_type"
163 * The format string, followed by optional arguments.
165 * 0 on success, -1 on error (errno is set).
167 #define EC_LOG(level, args...) ec_log(ec_log_local_type, level, args)
170 * Log a formatted string using the local log type.
172 * This macro requires that a log type is previously register with
173 * EC_LOG_TYPE_REGISTER() since it uses the "ec_log_local_type"
181 * The list of arguments.
183 * 0 on success, -1 on error (errno is set).
185 #define EC_VLOG(level, fmt, ap) ec_vlog(ec_log_local_type, level, fmt, ap)
188 * Default log handler.
190 * This is the default log function that is used by the library. By
191 * default, it prints all logs whose level is WARNING or more critical.
192 * This level can be changed with ec_log_level_set().
195 * The log type identifier.
201 * The string to be logged.
203 * 0 on success, -1 on error (errno is set).
205 int ec_log_default_cb(int type, enum ec_log_level level, void *opaque,
209 * Set the global log level.
211 * This level is used by the default log handler, ec_log_default_cb().
212 * All messages that are at least as critical as the default level are
218 * The log level to be set.
220 * 0 on success, -1 on error.
222 int ec_log_level_set(enum ec_log_level level);
225 * Get the global log level.
227 * This level is used by the default log handler, ec_log_default_cb().
228 * All messages that are at least as critical as the default level are
232 * The log level to be set.
234 * 0 on success, -1 on error.
236 enum ec_log_level ec_log_level_get(void);