e117af1fd2a487ecd88d5151728953cf7d934069
[protos/libecoli.git] / lib / ecoli_log.h
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
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.
15  *
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.
26  */
27
28 #ifndef ECOLI_LOG_
29 #define ECOLI_LOG_
30
31 #define EC_LOG_EMERG    0  /* system is unusable               */
32 #define EC_LOG_ALERT    1  /* action must be taken immediately */
33 #define EC_LOG_CRIT     2  /* critical conditions              */
34 #define EC_LOG_ERR      3  /* error conditions                 */
35 #define EC_LOG_WARNING  4  /* warning conditions               */
36 #define EC_LOG_NOTICE   5  /* normal but significant condition */
37 #define EC_LOG_INFO     6  /* informational                    */
38 #define EC_LOG_DEBUG    7  /* debug-level messages             */
39
40 #include <stdarg.h>
41
42 #define EC_LOG_TYPE_REGISTER(name)                                      \
43         static int name##_log_type;                                     \
44         static int local_log_type;                                      \
45         __attribute__((constructor, used))                              \
46         static void ec_log_register_##name(void)                        \
47         {                                                               \
48                 local_log_type = ec_log_type_register(#name);           \
49                 name##_log_type = local_log_type;                       \
50         }
51
52
53 /* return -1 on error, len(s) on success */
54 typedef int (*ec_log_t)(int type, unsigned int level, void *opaque,
55                         const char *str);
56
57 int ec_log_fct_register(ec_log_t usr_log, void *opaque);
58 void ec_log_fct_unregister(void);
59
60 int ec_log_type_register(const char *name);
61
62 const char *ec_log_name(int type);
63
64 /* same api than printf */
65 int ec_log(int type, unsigned int level, const char *format, ...)
66         __attribute__((format(__printf__, 3, 4)));
67
68 int ec_vlog(int type, unsigned int level, const char *format, va_list ap);
69
70 /* to use the macros, the user must have called EC_LOG_TYPE_REGISTER */
71 #define EC_LOG(level, args...) ec_log(local_log_type, level, args)
72 #define EC_VLOG(level, fmt, ap) ec_vlog(local_log_type, level, fmt, ap)
73
74 /* default log handler for the library, use printf */
75 int ec_log_default(int type, unsigned int level, void *opaque, const char *str);
76
77 #endif