2 * Copyright (c) 2013, 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 log of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this log 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.
33 #include "cfzy_htable.h"
35 static struct cfzy_htable *level_htable = NULL;
36 static int default_level = CFZY_LOG_NOTICE;
38 /* initialize log susbsystem */
39 int cfzy_log_init(void)
41 level_htable = cfzy_htable_alloc();
42 if (level_htable == NULL)
47 /* free all structures associated to log subsystem */
48 void cfzy_log_exit(void)
50 cfzy_htable_free(level_htable, free);
53 /* set default log level (for log types that are not registered) */
54 int cfzy_log_set_default_level(int level)
56 if (level != -1 && (level < CFZY_LOG_ERR || level > CFZY_LOG_DEBUG))
58 default_level = level;
62 /* set log level for a specific log type: the string is added in the
64 int cfzy_log_set_level(const char *logtype, int level)
68 if (level != -1 && (level < CFZY_LOG_ERR || level > CFZY_LOG_DEBUG))
70 l = cfzy_htable_lookup(level_htable, logtype);
72 /* if log is already registered, it's easy */
78 /* else allocate a new (int *) and store it in the htable */
79 l = malloc(sizeof(*l));
84 cfzy_htable_add(level_htable, logtype, l);
88 /* print a log if given level is <= to configured log level */
89 int cfzy_log_printf(const char *logtype, int level, const char *fmt, ...)
95 l = cfzy_htable_lookup(level_htable, logtype);
97 if (l == NULL && level > default_level)
99 else if (l != NULL && level > *l)
103 ret = vfprintf(stderr, fmt, ap);