322bb326471ca40739fee8001b097ef5ec8f993d
[strongswan.git] / Source / lib / utils / logger.h
1 /**
2 * @file logger.h
3 *
4 * @brief Interface of logger_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #ifndef LOGGER_H_
24 #define LOGGER_H_
25
26 #include <stdio.h>
27
28 #include <types.h>
29
30 typedef enum log_level_t log_level_t;
31
32 /**
33 * @brief Log Levels supported by the logger object.
34 *
35 * Logleves are devided in two different kinds:
36 * - levels to specify the type of the log
37 * - levels to specify the detail-level of the log
38 *
39 * Use combinations of these to build detailed loglevels, such
40 * as CONTROL|LEVEL2 fore a detailed cotrol level, or
41 * use RAW to see all raw data dumps (except private).
42 *
43 * @ingroup utils
44 */
45 enum log_level_t {
46 /**
47 * Control flow.
48 */
49 CONTROL = 1,
50 /**
51 * Error reporting.
52 */
53 ERROR = 2,
54 /**
55 * Logs important for the sysadmin.
56 */
57 AUDIT = 4,
58 /**
59 * Raw data dumps.
60 */
61 RAW = 8,
62 /**
63 * Private data dumps.
64 */
65 PRIVATE = 16,
66
67 /**
68 * Log most important output, can be omitted.
69 */
70 LEVEL0 = 0,
71 /**
72 * Log more detailed output.
73 */
74 LEVEL1 = 32,
75 /**
76 * Log even more detailed output.
77 */
78 LEVEL2 = LEVEL1 + 64,
79 /**
80 * Use maximum detailed output.
81 */
82 LEVEL3 = LEVEL2 + 128,
83
84 /**
85 * Summary for all types with all detail-levels.
86 */
87 FULL = LEVEL3 + CONTROL + ERROR + RAW + PRIVATE + AUDIT
88 };
89
90 typedef struct logger_t logger_t;
91
92 /**
93 * @brief Class to simplify logging.
94 *
95 * @b Constructors:
96 * - logger_create()
97 *
98 * @ingroup utils
99 */
100 struct logger_t {
101
102 /**
103 * @brief Log an entry, using printf()-like params.
104 *
105 * All specified loglevels must be activated that
106 * the log is done.
107 *
108 * @param this logger_t object
109 * @param loglevel or'ed set of log_level_t's
110 * @param format printf like format string
111 * @param ... printf like parameters
112 */
113 void (*log) (logger_t *this, log_level_t log_level, char *format, ...);
114
115 /**
116 * @brief Log some bytes, useful for debugging.
117 *
118 * All specified loglevels must be activated that
119 * the log is done.
120 *
121 * @param this logger_t object
122 * @param loglevel or'ed set of log_level_t's
123 * @param label a labeling name, logged with the bytes
124 * @param bytes pointer to the bytes to dump
125 * @param len number of bytes to dump
126 */
127 void (*log_bytes) (logger_t *this, log_level_t loglevel, char *label, char *bytes, size_t len);
128
129 /**
130 * @brief Log a chunk, useful for debugging.
131 *
132 * All specified loglevels must be activated that
133 * the log is done.
134 *
135 * @param this logger_t object
136 * @param loglevel or'ed set of log_level_t's
137 * @param label a labeling name, logged with the bytes
138 * @param chunk chunk to log
139 */
140 void (*log_chunk) (logger_t *this, log_level_t loglevel, char *label, chunk_t chunk);
141
142 /**
143 * @brief Enables a loglevel for the current logger_t object.
144 *
145 * @param this logger_t object
146 * @param log_level loglevel to enable
147 */
148 void (*enable_level) (logger_t *this, log_level_t log_level);
149
150 /**
151 * @brief Disables a loglevel for the current logger_t object.
152 *
153 * @param this logger_t object
154 * @param log_level loglevel to enable
155 */
156 void (*disable_level) (logger_t *this, log_level_t log_level);
157
158 /**
159 * @brief Set the output of the logger.
160 *
161 * Use NULL for syslog.
162 *
163 * @param this logger_t object
164 * @param output file, where log output should be written
165 */
166 void (*set_output) (logger_t *this, FILE *output);
167
168 /**
169 * @brief Get the currently used loglevel.
170 *
171 * @param this logger_t object
172 * @return currently used loglevel
173 */
174 log_level_t (*get_level) (logger_t *this);
175
176 /**
177 * @brief Destroys a logger_t object.
178 *
179 * @param this logger_t object
180 */
181 void (*destroy) (logger_t *this);
182 };
183
184 /**
185 * @brief Constructor to create a logger_t object.
186 *
187 * @param logger_name name for the logger_t object
188 * @param log_level or'ed set of log_levels to assign to the new logger_t object
189 * @param log_thread_id TRUE if thread id should also be logged
190 * @param output FILE * if log has to go on a file output, NULL for syslog
191 * @return logger_t object
192 *
193 * @ingroup utils
194 */
195 logger_t *logger_create(char *logger_name, log_level_t log_level, bool log_thread_id, FILE * output);
196
197
198 #endif /*LOGGER_H_*/