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)
21 #include <ecoli_assert.h>
24 EC_LOG_EMERG = 0, /* system is unusable */
25 EC_LOG_ALERT = 1, /* action must be taken immediately */
26 EC_LOG_CRIT = 2, /* critical conditions */
27 EC_LOG_ERR = 3, /* error conditions */
28 EC_LOG_WARNING = 4, /* warning conditions */
29 EC_LOG_NOTICE = 5, /* normal but significant condition */
30 EC_LOG_INFO = 6, /* informational */
31 EC_LOG_DEBUG = 7, /* debug-level messages */
35 * Register a log type.
37 * This macro defines a function that will be called at startup (using
38 * the "constructor" attribute). This function registers the named type
39 * passed as argument, and sets a static global variable
40 * "ec_log_local_type". This variable is used as the default log type
41 * for this file when using EC_LOG() or EC_VLOG().
43 * This macro can be present several times in a file. In this case, the
44 * local log type is set to the last registered type.
46 * On error, the function aborts.
49 * The name of the log to be registered.
51 #define EC_LOG_TYPE_REGISTER(name) \
52 static int name##_log_type; \
53 static int ec_log_local_type; \
54 __attribute__((constructor, used)) \
55 static void ec_log_register_##name(void) \
57 ec_log_local_type = ec_log_type_register(#name); \
58 ec_assert_print(ec_log_local_type >= 0, \
59 "cannot register log type.\n"); \
60 name##_log_type = ec_log_local_type; \
64 * User log function type.
66 * It is advised that a user-defined log function drops all messages
67 * that are at least as critical as ec_log_level_get(), as done by the
71 * The log type identifier.
75 * The opaque pointer that was passed to ec_log_fct_register().
79 * 0 on success, -1 on error (errno is set).
81 typedef int (*ec_log_t)(int type, enum ec_log_level level, void *opaque,
85 * Register a user log function.
88 * Function pointer that will be invoked for each log call.
89 * If the parameter is NULL, ec_log_default_cb() is used.
91 * Opaque pointer passed to the log function.
93 * 0 on success, -1 on error (errno is set).
95 int ec_log_fct_register(ec_log_t usr_log, void *opaque);
98 * Register a named log type.
100 * Register a new log type, which is identified by its name. The
101 * function returns a log identifier associated to the log name. If the
102 * name is already registered, the function just returns its identifier.
105 * The name of the log type.
107 * The log type identifier on success (positive or zero), -1 on
108 * error (errno is set).
110 int ec_log_type_register(const char *name);
113 * Return the log name associated to the log type identifier.
116 * The log type identifier.
118 * The name associated to the log type, or "unknown". It always return
119 * a valid string (never NULL).
121 const char *ec_log_name(int type);
124 * Log a formatted string.
127 * The log type identifier.
131 * The format string, followed by optional arguments.
133 * 0 on success, -1 on error (errno is set).
135 int ec_log(int type, enum ec_log_level level, const char *format, ...)
136 __attribute__((format(__printf__, 3, 4)));
139 * Log a formatted string.
142 * The log type identifier.
148 * The list of arguments.
150 * 0 on success, -1 on error (errno is set).
152 int ec_vlog(int type, enum ec_log_level level, const char *format, va_list ap);
155 * Log a formatted string using the local log type.
157 * This macro requires that a log type is previously register with
158 * EC_LOG_TYPE_REGISTER() since it uses the "ec_log_local_type"
164 * The format string, followed by optional arguments.
166 * 0 on success, -1 on error (errno is set).
168 #define EC_LOG(level, args...) ec_log(ec_log_local_type, level, args)
171 * Log a formatted string using the local log type.
173 * This macro requires that a log type is previously register with
174 * EC_LOG_TYPE_REGISTER() since it uses the "ec_log_local_type"
182 * The list of arguments.
184 * 0 on success, -1 on error (errno is set).
186 #define EC_VLOG(level, fmt, ap) ec_vlog(ec_log_local_type, level, fmt, ap)
189 * Default log handler.
191 * This is the default log function that is used by the library. By
192 * default, it prints all logs whose level is WARNING or more critical.
193 * This level can be changed with ec_log_level_set().
196 * The log type identifier.
202 * The string to be logged.
204 * 0 on success, -1 on error (errno is set).
206 int ec_log_default_cb(int type, enum ec_log_level level, void *opaque,
210 * Set the global log level.
212 * This level is used by the default log handler, ec_log_default_cb().
213 * All messages that are at least as critical as the default level are
219 * The log level to be set.
221 * 0 on success, -1 on error.
223 int ec_log_level_set(enum ec_log_level level);
226 * Get the global log level.
228 * This level is used by the default log handler, ec_log_default_cb().
229 * All messages that are at least as critical as the default level are
233 * The log level to be set.
235 * 0 on success, -1 on error.
237 enum ec_log_level ec_log_level_get(void);