2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * This file provide logging helpers:
32 * - logging functions, supporting printf-like format
33 * - several debug level (similar to syslog)
35 * - redirection of log to a user functions (default logs nothing)
43 #include <ecoli_assert.h>
46 EC_LOG_EMERG = 0, /* system is unusable */
47 EC_LOG_ALERT = 1, /* action must be taken immediately */
48 EC_LOG_CRIT = 2, /* critical conditions */
49 EC_LOG_ERR = 3, /* error conditions */
50 EC_LOG_WARNING = 4, /* warning conditions */
51 EC_LOG_NOTICE = 5, /* normal but significant condition */
52 EC_LOG_INFO = 6, /* informational */
53 EC_LOG_DEBUG = 7, /* debug-level messages */
57 * Register a log type.
59 * This macro defines a function that will be called at startup (using
60 * the "constructor" attribute). This function register the named type
61 * passed as argument, and sets a static global variable
62 * "ec_log_local_type". This variable is used as the default log type
63 * for this file when using EC_LOG() or EC_VLOG().
65 * This macro can be present several times in a file. In this case, the
66 * local log type is set to the last registered type.
68 * On error, the function aborts.
71 * The name of the log to be registered.
73 #define EC_LOG_TYPE_REGISTER(name) \
74 static int name##_log_type; \
75 static int ec_log_local_type; \
76 __attribute__((constructor, used)) \
77 static void ec_log_register_##name(void) \
79 ec_log_local_type = ec_log_type_register(#name); \
80 ec_assert_print(ec_log_local_type >= 0, \
81 "cannot register log type.\n"); \
82 name##_log_type = ec_log_local_type; \
86 * User log function type.
88 * It is advised that a user-defined log function drops all messages
89 * that are at least as critical as ec_log_level_get(), as done by the
93 * The log type identifier.
97 * The opaque pointer that was passed to ec_log_fct_register().
101 * 0 on success, -1 on error (errno is set).
103 typedef int (*ec_log_t)(int type, enum ec_log_level level, void *opaque,
107 * Register a user log function.
110 * Function pointer that will be invoked for each log call.
111 * If the parameter is NULL, ec_log_default_cb() is used.
113 * Opaque pointer passed to the log function.
115 * 0 on success, -1 on error (errno is set).
117 int ec_log_fct_register(ec_log_t usr_log, void *opaque);
120 * Register a named log type.
122 * Register a new log type, which is identified by its name. The
123 * function returns a log identifier associated to the log name. If the
124 * name is already registered, the function just returns its identifier.
127 * The name of the log type.
129 * The log type identifier on success (positive or zero), -1 on
130 * error (errno is set).
132 int ec_log_type_register(const char *name);
135 * Return the log name associated to the log type identifier.
138 * The log type identifier.
140 * The name associated to the log type, or "unknown". It always return
141 * a valid string (never NULL).
143 const char *ec_log_name(int type);
146 * Log a formatted string.
149 * The log type identifier.
153 * The format string, followed by optional arguments.
155 * 0 on success, -1 on error (errno is set).
157 int ec_log(int type, enum ec_log_level level, const char *format, ...)
158 __attribute__((format(__printf__, 3, 4)));
161 * Log a formatted string.
164 * The log type identifier.
170 * The list of arguments.
172 * 0 on success, -1 on error (errno is set).
174 int ec_vlog(int type, enum ec_log_level level, const char *format, va_list ap);
177 * Log a formatted string using the local log type.
179 * This macro requires that a log type is previously register with
180 * EC_LOG_TYPE_REGISTER() since it uses the "ec_log_local_type"
186 * The format string, followed by optional arguments.
188 * 0 on success, -1 on error (errno is set).
190 #define EC_LOG(level, args...) ec_log(ec_log_local_type, level, args)
193 * Log a formatted string using the local log type.
195 * This macro requires that a log type is previously register with
196 * EC_LOG_TYPE_REGISTER() since it uses the "ec_log_local_type"
204 * The list of arguments.
206 * 0 on success, -1 on error (errno is set).
208 #define EC_VLOG(level, fmt, ap) ec_vlog(ec_log_local_type, level, fmt, ap)
211 * Default log handler.
213 * This is the default log function that is used by the library. By
214 * default, it prints all logs whose level is WARNING or more critical.
215 * This level can be changed with ec_log_level_set().
218 * The log type identifier.
224 * The string to be logged.
226 * 0 on success, -1 on error (errno is set).
228 int ec_log_default_cb(int type, enum ec_log_level level, void *opaque,
232 * Set the global log level.
234 * This level is used by the default log handler, ec_log_default_cb().
235 * All messages that are at least as critical as the default level are
241 * The log level to be set.
243 * 0 on success, -1 on error.
245 int ec_log_level_set(enum ec_log_level level);
248 * Get the global log level.
250 * This level is used by the default log handler, ec_log_default_cb().
251 * All messages that are at least as critical as the default level are
255 * The log level to be set.
257 * 0 on success, -1 on error.
259 enum ec_log_level ec_log_level_get(void);