92e2145db17a54c136831a3321f3409632cd9a43
[protos/libecoli.git] / lib / ecoli_log.c
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 #define _GNU_SOURCE /* for vasprintf */
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include <ecoli_malloc.h>
35 #include <ecoli_log.h>
36
37 static ec_log_t ec_log_fct = ec_log_default_cb;
38 static void *ec_log_opaque;
39
40 struct ec_log_type {
41         char *name;
42         enum ec_log_level level;
43 };
44
45 static struct ec_log_type *log_types;
46 static size_t log_types_len;
47 static enum ec_log_level global_level = EC_LOG_WARNING;
48
49 int ec_log_level_set(enum ec_log_level level)
50 {
51         if (level < 0 || level > EC_LOG_DEBUG)
52                 return -1;
53         global_level = level;
54
55         return 0;
56 }
57
58 enum ec_log_level ec_log_level_get(void)
59 {
60         return global_level;
61 }
62
63 int ec_log_default_cb(int type, enum ec_log_level level, void *opaque,
64                 const char *str)
65 {
66         (void)opaque;
67
68         if (level > ec_log_level_get())
69                 return 0;
70
71         if (fprintf(stderr, "[%d] %-12s %s", level, ec_log_name(type), str) < 0)
72                 return -1;
73
74         return 0;
75 }
76
77 int ec_log_fct_register(ec_log_t usr_log, void *opaque)
78 {
79         errno = 0;
80
81         if (usr_log == NULL) {
82                 ec_log_fct = ec_log_default_cb;
83                 ec_log_opaque = NULL;
84         } else {
85                 ec_log_fct = usr_log;
86                 ec_log_opaque = opaque;
87         }
88
89         return 0;
90 }
91
92 static int
93 ec_log_lookup(const char *name)
94 {
95         size_t i;
96
97         for (i = 0; i < log_types_len; i++) {
98                 if (log_types[i].name == NULL)
99                         continue;
100                 if (strcmp(name, log_types[i].name) == 0)
101                         return i;
102         }
103
104         return -1;
105 }
106
107 int
108 ec_log_type_register(const char *name)
109 {
110         struct ec_log_type *new_types;
111         char *copy;
112         int id;
113
114         id = ec_log_lookup(name);
115         if (id >= 0)
116                 return id;
117
118         new_types = ec_realloc(log_types,
119                 sizeof(*new_types) * (log_types_len + 1));
120         if (new_types == NULL)
121                 return -1; /* errno is set */
122         log_types = new_types;
123
124         copy = ec_strdup(name);
125         if (copy == NULL)
126                 return -1; /* errno is set */
127
128         id = log_types_len++;
129         log_types[id].name = copy;
130         log_types[id].level = EC_LOG_DEBUG;
131
132         return id;
133 }
134
135 const char *
136 ec_log_name(int type)
137 {
138         if (type < 0 || (unsigned int)type >= log_types_len)
139                 return "unknown";
140         return log_types[type].name;
141 }
142
143 int ec_vlog(int type, enum ec_log_level level, const char *format, va_list ap)
144 {
145         char *s;
146         int ret;
147
148         ret = vasprintf(&s, format, ap);
149         if (ret < 0)
150                 return ret;
151
152         ret = ec_log_fct(type, level, ec_log_opaque, s);
153         free(s);
154
155         return ret;
156 }
157
158 int ec_log(int type, enum ec_log_level level, const char *format, ...)
159 {
160         va_list ap;
161         int ret;
162
163         va_start(ap, format);
164         ret = ec_vlog(type, level, format, ap);
165         va_end(ap);
166
167         return ret;
168 }