Update Debian Vcs-* fields to point to git repository
[onak.git] / log.h
1 /*
2  * log.h - Simple logging framework.
3  *
4  * Copyright 2003 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifndef __LOG_H__
21 #define __LOG_H__
22
23 #include <assert.h>
24
25 #define log_assert(expr) \
26         if (!(expr)) { \
27                 logthing(LOGTHING_CRITICAL, \
28                         "Assertion %s failed in %s, line %d", \
29                         #expr, \
30                         __FILE__, \
31                         __LINE__); \
32         } \
33         assert(expr)
34
35 /*
36  *      loglevels - levels of severity for a log entry
37  *
38  *      These provide various different levels of severity for a log entry. In
39  *      acesending order they are:
40  *
41  *      LOGTHING_TRACE
42  *      LOGTHING_DEBUG
43  *      LOGTHING_INFO
44  *      LOGTHING_NOTICE
45  *      LOGTHING_ERROR
46  *      LOGTHING_SERIOUS
47  *      LOGTHING_CRITICAL
48  *
49  *      By default the log threshold is set to LOGTHING_NOTICE, meaning
50  *      anything with a lower priority won't be output.
51  */
52 typedef enum {
53         LOGTHING_TRACE = 0,
54         LOGTHING_DEBUG = 1,
55         LOGTHING_INFO = 2,
56         LOGTHING_NOTICE = 3,
57         LOGTHING_ERROR = 4,
58         LOGTHING_SERIOUS = 5,
59         LOGTHING_CRITICAL = 6
60 } loglevels;
61
62 /*
63  *      initlogthing - initialize the logging module
64  *      @appname: The application name to use in the log.
65  *      @filename: The filename to log to. NULL means stderr.
66  *
67  *      This function sets up the logging module ready to log. The appname is
68  *      written as part of every log entry and the filename is the file we
69  *      should log to. If the appname is NULL then none is written. If the
70  *      filename is NULL all output is sent to stderr.
71  */
72 int initlogthing(const char *appname, const char *filename);
73
74 /*
75  *      cleanuplogthing - clean up the logging module
76  *
77  *      This function cleans up the logging module after use.
78  */
79 void cleanuplogthing(void);
80
81 /*
82  *      setlogthreshold - set the threshold for log output
83  *      @loglevel: The minimum log level we should output
84  *
85  *      Sets the threshold for log output; anything logged with a log level
86  *      lower than this will be silently dropped. Returns the old log threshold
87  *      value.
88  */
89 loglevels setlogthreshold(loglevels loglevel);
90
91 /*
92  *      logthing - output a log entry
93  *      @loglevel: The level of the log.
94  *      @format: A format string, followed by any parameters required.
95  *
96  *      This function outputs a log entry. A leading time/date stamp and a
97  *      trailing newline are automatically added. The loglevel is compared to
98  *      the current log threshold and if equal or above the log entry is
99  *      output. The format parameter is of the same nature as that used in
100  *      printf.
101  */
102 int logthing(loglevels loglevel, const char *format, ...);
103
104 #endif /* __LOG_H__ */