summaryrefslogtreecommitdiff
path: root/include/wolf/log/log.h
blob: cc3e3762cb4b4abe6f9bdef15e2af2f47817f9d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef WOLF_LOG_H
#define WOLF_LOG_H

#include "port/stdbool.h"		/* for bool */
#include "port/string.h"		/* for strlcpy */
#include "i18n/gettext.h"		/* for _ */

/**
 * @addtogroup wolf_log Logging
 * @{
 */

/**
 * @file log.h
 * @brief functions for logging (syslog, eventlog, logfiles, stderr)
 * @author Andreas Baumann <abaumann@yahoo.com>
 */

#ifdef __cplusplus
extern "C" {
#endif

#if defined HAVE_SYSLOG_H
/**
 * @brief defined if we have a syslogd daemon (Unix usually)
 */
#define WOLF_LOG_HAVE_SYSLOG
#endif

#if defined HAVE_EVENTLOG
/**
 * @brief defined if we have an event logger (Windows only)
 */
#define WOLF_LOG_HAVE_EVENTLOG
#endif

/**
 * @brief severity levels. Similarly defined as in syslog.h
 */
typedef enum {
	WOLF_LOG_EMERG		= 0, /**< system is unusable */
	WOLF_LOG_ALERT		= 1, /**< action must be taken immediately */
	WOLF_LOG_CRIT		= 2, /**< critical conditions */
	WOLF_LOG_ERR		= 3, /**< error conditions */
	WOLF_LOG_WARNING	= 4, /**< warning conditions */
	WOLF_LOG_NOTICE		= 5, /**< normal but significant condition */
	WOLF_LOG_INFO		= 6, /**< informational */
	WOLF_LOG_DEBUG		= 7, /**< debug-level messages */
	WOLF_LOG_DEBUG0		= 7, /**< debug-level, same as WOLF_LOG_DEBUG */
	WOLF_LOG_DEBUG1		= 8, /**< debug-level messages */
	WOLF_LOG_DEBUG2		= 9, /**< debug-level messages */
	WOLF_LOG_DEBUG3		= 10, /**< debug-level messages */
	WOLF_LOG_DEBUG4		= 11, /**< debug-level messages */
	WOLF_LOG_DEBUG5		= 12 /**< debug-level messages */
} wolf_log_level_t;

#if defined HAVE_SYSLOG_H
/**
 * @brief Possible syslog facilities. Not the same on all Unix systems.
 */
typedef enum {
	WOLF_LOG_KERN		= 0,	/**< kernel messages */
	WOLF_LOG_USER		= 1,	/**< random user-level messages */
	WOLF_LOG_MAIL		= 2,	/**< mail system */
	WOLF_LOG_DAEMON		= 3,	/**< system daemons */
	WOLF_LOG_AUTH		= 4,	/**< security/authorization messages */
	WOLF_LOG_SYSLOG		= 5,	/**< messages generated internally by syslogd */
	WOLF_LOG_LPR		= 6,	/**< line printer subsystem */
	WOLF_LOG_NEWS		= 7,	/**< network news subsystem */
	WOLF_LOG_UUCP		= 8,	/**< UUCP subsystem */
	WOLF_LOG_CRON		= 9,	/**< cron/at subsystem */
	WOLF_LOG_AUTHPRIV	= 10,	/**< security/authorization messages (private) */
	WOLF_LOG_FTP		= 11,	/**< ftp daemon */
	WOLF_LOG_NTP		= 12,	/**< ntp daemon (FreeBSD only) */
	WOLF_LOG_SECURITY	= 13,	/**< security subsystems (firewalling, etc., FreeBSD only) */
	WOLF_LOG_CONSOLE	= 14,	/**< /dev/console output (FreeBSD only) */
	WOLF_LOG_AUDIT		= 15,	/**< audit subsystem (Solaris 10 only) */
	WOLF_LOG_LOCAL0		= 16,	/**< local facility 0 */
	WOLF_LOG_LOCAL1		= 17,	/**< local facility 1 */
	WOLF_LOG_LOCAL2		= 18,	/**< local facility 2 */
	WOLF_LOG_LOCAL3		= 19,	/**< local facility 3 */
	WOLF_LOG_LOCAL4		= 20,	/**< local facility 4 */
	WOLF_LOG_LOCAL5		= 21,	/**< local facility 5 */
	WOLF_LOG_LOCAL6		= 22,	/**< local facility 6 */
	WOLF_LOG_LOCAL7		= 23	/**< local facility 7 */
} wolf_log_syslog_facility_t;
#endif /* defined HAVE_SYSLOG_H */

#if defined HAVE_SYSLOG_H
/**
 * @brief Options for opening the system logger, as defined by the Open Group.
 *
 * Note: LOG_ODELAY, LOG_NDELAY, LOG_NOWAIT are more or less deprecated,
 * also LOG_PERROR is not POSIX and can be replaced with a stderr logger.
 */
typedef enum {
	WOLF_LOG_SYSLOG_PID	= 1,	/**< log the process ID with each message */
	WOLF_LOG_SYSLOG_CONS	= 2	/**< log to the system console on error */
} wolf_log_syslog_option_t;
#endif /* defined HAVE_SYSLOG_H */

/**
 * @brief The default options to pass to the system logger.
 */
#if defined HAVE_SYSLOG_H
#define WOLF_LOG_SYSLOG_DEFAULT_OPTIONS  WOLF_LOG_SYSLOG_PID | WOLF_LOG_SYSLOG_CONS
#endif /* defined HAVE_SYSLOG_H */
 
#if defined HAVE_SYSLOG_H
/**
 * Convert a wolf_log_syslog_facility_t to its string representation.
 *
 * @param facility  a wolf_log_syslog_facility_t
 * @return          a string constant like "DAEMON"
 */
const char *wolf_log_syslog_facility_to_str( wolf_log_syslog_facility_t facility );
#endif /* defined HAVE_SYSLOG_H */

#if defined HAVE_SYSLOG_H
/**
 * Convert a name of a syslog facility like "DAEMON" to the corresponding
 * wolf_log_syslog_facility_t
 *
 * @param facility  a string like "DAEMON"
 * @return          the matching wolf_log_syslog_facility_t or -1 if the
 *                  passed facility is not known
 */
wolf_log_syslog_facility_t wolf_log_str_to_syslog_facility( const char *facility );
#endif /* defined HAVE_SYSLOG_H */

#if defined HAVE_SYSLOG_H
/**
 * Returns if the current platform knows about the given syslog facility.
 *
 * @param facility  a wolf_log_syslog_facility_t
 * @return          true if the facility exists, false if not
 */
bool wolf_log_platform_has_syslog_facility( wolf_log_syslog_facility_t facility );
#endif /* defined HAVE_SYSLOG_H */

/**
 * Convert a wolf_log_level_t to its string representation.
 *
 * @param level     a wolf_log_level_t
 * @return          a string constant like "NOTICE"
 */
const char *wolf_log_level_to_str( wolf_log_level_t level );

/**
 * Convert a name of a level like "NOTICE" to the corresponding
 * wolf_log_level_t
 *
 * @param level     a string like "NOTICE"
 * @return          the matching wolf_log_level_t or -1 if the
 *                  passed level is not known
 */
wolf_log_level_t wolf_log_str_to_level( const char *level );

/**
 * Open a channel for logging to a file.
 *
 * @param filename  name of the logfile to log to
 * @param level     one out of wolf_log_level_t, the minimal level which
 *                  should be logged into the logfile
 */
void wolf_log_openlogtofile( const char *filename, wolf_log_level_t level );

#if defined HAVE_SYSLOG_H
/**
 * Open a channel to the Unix system logger.
 *
 * @param ident     identifies the programm using the logger
 * @param facility  one out of wolf_log_syslog_facility_t e. g.
 *                  wolf_log_facility_DAEMON
 * @param level     one out of wolf_log_level_t, the minimal level which
 *                  should be logged into syslog
 * @param options   a mask of options of type wolf_log_syslog_option_t,
 *                  (default is WOLF_LOG_SYSLOG_DEFAULT_OPTIONS)
 */
void wolf_log_openlogtosyslog(	const char *ident,
				wolf_log_syslog_facility_t facility,
				wolf_log_level_t level,
				int options );
#endif /* defined HAVE_SYSLOG_H */

#if defined HAVE_EVENTLOG
/**
 * Open a channel to the Windows event logger.
 *
 * @param server      name of the server to log to, NULL if local computer
 * @param log         name of the log, usually this should be 'Application'
 * @param source      name of the event log source
 * @param path_to_dll path to the message/category DLL for the localization
 *                    of the categories and messages to log. the string should
 *                    be expandable, i.e. about hard-coded drives and paths,
 *                    e. g. "%SystemRoot%\\System32\\myapp.dll"
 * @param nof_cats    number of categories in the messages/category DLL
 * @param level       one out of wolf_log_level_t, the minimal level which
 *                    should be logged into the event log
 *
 */
void wolf_log_openlogtoeventlog(	const char *server,
					const char *log,
					const char *source,
					const char *path_to_dll,
					int nof_cats,
					wolf_log_level_t level );
#endif /* defined HAVE_EVENTLOG */

/**
 * Open a channel for logging to stderr.
 *
 * @param level     one out of wolf_log_level_t, the minimal level which
 *                  should be logged into standard error
 */
void wolf_log_openlogtostderr( wolf_log_level_t level );

/**
 * Close logging to a logfile.
 */
void wolf_log_closelogtofile( void );

#if defined HAVE_SYSLOG_H
/**
 * Close logging to the Unix system logger.
 */
void wolf_log_closelogtosyslog( void );
#endif /* defined HAVE_SYSLOG_H */

#if defined HAVE_EVENTLOG
/**
 * Close logging to the Windows event logger.
 *
 */
void wolf_log_closelogtoeventlog( void );
#endif /* defined HAVE_EVENTLOG */

/**
 * Close logging to stderr.
 */
void wolf_log_closelogtostderr( void );

/**
 * Reopen logging to a logfile.
 */
void wolf_log_reopenlogtofile( void );

#if defined HAVE_SYSLOG_H
/**
 * Reopen logging to the Unix system logger.
 */
void wolf_log_reopenlogtosyslog( void );
#endif /* defined HAVE_SYSLOG_H */

#if defined HAVE_EVENTLOG
/**
 * Reopen logging to the Windows event logger.
 */
void wolf_log_reopenlogtoeventlog( void );
#endif /* defined HAVE_EVENTLOG */

/**
 * Write a formatted log message to the logger.
 *
 * @param level       one of wolf_log_level_t
 * @param category_id a category as defined in messages.h
 * @param message_id  a message id as defined in messages.h
 * @param format      snprintf like format string,
 *                    the rest of the parameters given
 *                    as defined in the format string
 */
void wolf_log( wolf_log_level_t level, int category_id, int message_id, const char *format, ... );

#ifdef __cplusplus
}
#endif

/** @} */ /* @addtogroup wolf_log */

#endif /* ifndef WOLF_LOG_H */