1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
11 * This file provide logging helpers:
12 * - logging functions, supporting printf-like format
13 * - several debug level (similar to syslog)
15 * - redirection of log to a user functions (default logs nothing)
24 #include <ecoli_assert.h>
27 EC_LOG_EMERG = 0, /* system is unusable */
28 EC_LOG_ALERT = 1, /* action must be taken immediately */
29 EC_LOG_CRIT = 2, /* critical conditions */
30 EC_LOG_ERR = 3, /* error conditions */
31 EC_LOG_WARNING = 4, /* warning conditions */
32 EC_LOG_NOTICE = 5, /* normal but significant condition */
33 EC_LOG_INFO = 6, /* informational */
34 EC_LOG_DEBUG = 7, /* debug-level messages */
38 * Register a log type.
40 * This macro defines a function that will be called at startup (using
41 * the "constructor" attribute). This function registers the named type
42 * passed as argument, and sets a static global variable
43 * "ec_log_local_type". This variable is used as the default log type
44 * for this file when using EC_LOG() or EC_VLOG().
46 * This macro can be present several times in a file. In this case, the
47 * local log type is set to the last registered type.
49 * On error, the function aborts.
52 * The name of the log to be registered.
54 #define EC_LOG_TYPE_REGISTER(name) \
55 static int name##_log_type; \
56 static int ec_log_local_type; \
57 __attribute__((constructor, used)) \
58 static void ec_log_register_##name(void) \
60 ec_log_local_type = ec_log_type_register(#name); \
61 ec_assert_print(ec_log_local_type >= 0, \
62 "cannot register log type.\n"); \
63 name##_log_type = ec_log_local_type; \
67 * User log function type.
69 * It is advised that a user-defined log function drops all messages
70 * that are at least as critical as ec_log_level_get(), as done by the
74 * The log type identifier.
78 * The opaque pointer that was passed to ec_log_fct_register().
82 * 0 on success, -1 on error (errno is set).
84 typedef int (*ec_log_t)(int type, enum ec_log_level level, void *opaque,
88 * Register a user log function.
91 * Function pointer that will be invoked for each log call.
92 * If the parameter is NULL, ec_log_default_cb() is used.
94 * Opaque pointer passed to the log function.
96 * 0 on success, -1 on error (errno is set).
98 int ec_log_fct_register(ec_log_t usr_log, void *opaque);
101 * Register a named log type.
103 * Register a new log type, which is identified by its name. The
104 * function returns a log identifier associated to the log name. If the
105 * name is already registered, the function just returns its identifier.
108 * The name of the log type.
110 * The log type identifier on success (positive or zero), -1 on
111 * error (errno is set).
113 int ec_log_type_register(const char *name);
116 * Return the log name associated to the log type identifier.
119 * The log type identifier.
121 * The name associated to the log type, or "unknown". It always return
122 * a valid string (never NULL).
124 const char *ec_log_name(int type);
127 * Log a formatted string.
130 * The log type identifier.
134 * The format string, followed by optional arguments.
136 * 0 on success, -1 on error (errno is set).
138 int ec_log(int type, enum ec_log_level level, const char *format, ...)
139 __attribute__((format(__printf__, 3, 4)));
142 * Log a formatted string.
145 * The log type identifier.
151 * The list of arguments.
153 * 0 on success, -1 on error (errno is set).
155 int ec_vlog(int type, enum ec_log_level level, const char *format, va_list ap);
158 * Log a formatted string using the local log type.
160 * This macro requires that a log type is previously register with
161 * EC_LOG_TYPE_REGISTER() since it uses the "ec_log_local_type"
167 * The format string, followed by optional arguments.
169 * 0 on success, -1 on error (errno is set).
171 #define EC_LOG(level, args...) ec_log(ec_log_local_type, level, args)
174 * Log a formatted string using the local log type.
176 * This macro requires that a log type is previously register with
177 * EC_LOG_TYPE_REGISTER() since it uses the "ec_log_local_type"
185 * The list of arguments.
187 * 0 on success, -1 on error (errno is set).
189 #define EC_VLOG(level, fmt, ap) ec_vlog(ec_log_local_type, level, fmt, ap)
192 * Default log handler.
194 * This is the default log function that is used by the library. By
195 * default, it prints all logs whose level is WARNING or more critical.
196 * This level can be changed with ec_log_level_set().
199 * The log type identifier.
205 * The string to be logged.
207 * 0 on success, -1 on error (errno is set).
209 int ec_log_default_cb(int type, enum ec_log_level level, void *opaque,
213 * Set the global log level.
215 * This level is used by the default log handler, ec_log_default_cb().
216 * All messages that are at least as critical as the default level are
222 * The log level to be set.
224 * 0 on success, -1 on error.
226 int ec_log_level_set(enum ec_log_level level);
229 * Get the global log level.
231 * This level is used by the default log handler, ec_log_default_cb().
232 * All messages that are at least as critical as the default level are
236 * The log level to be set.
238 * 0 on success, -1 on error.
240 enum ec_log_level ec_log_level_get(void);