ucx/ucx/logging.h

changeset 157
0b33b9396851
child 162
18892c0a9adc
equal deleted inserted replaced
156:62f1a55535e7 157:0b33b9396851
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * Logging API.
30 *
31 * @file logging.h
32 * @author Mike Becker, Olaf Wintermann
33 */
34 #ifndef UCX_LOGGING_H
35 #define UCX_LOGGING_H
36
37 #include "ucx.h"
38 #include "map.h"
39 #include "string.h"
40 #include <stdio.h>
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 /* leave enough space for custom log levels */
47
48 /** Log level for error messages. */
49 #define UCX_LOGGER_ERROR 0x00
50
51 /** Log level for warning messages. */
52 #define UCX_LOGGER_WARN 0x10
53
54 /** Log level for information messages. */
55 #define UCX_LOGGER_INFO 0x20
56
57 /** Log level for debug messages. */
58 #define UCX_LOGGER_DEBUG 0x30
59
60 /** Log level for trace messages. */
61 #define UCX_LOGGER_TRACE 0x40
62
63 /**
64 * Output flag for the log level.
65 * If this flag is set, the log message will contain the log level.
66 * @see UcxLogger.mask
67 */
68 #define UCX_LOGGER_LEVEL 0x01
69
70 /**
71 * Output flag for the timestmap.
72 * If this flag is set, the log message will contain the timestmap.
73 * @see UcxLogger.mask
74 */
75 #define UCX_LOGGER_TIMESTAMP 0x02
76
77 /**
78 * Output flag for the source.
79 * If this flag is set, the log message will contain the source file and line
80 * number.
81 * @see UcxLogger.mask
82 */
83 #define UCX_LOGGER_SOURCE 0x04
84
85 /**
86 * The UCX Logger object.
87 */
88 typedef struct {
89 /** The stream this logger writes its messages to.*/
90 void *stream;
91
92 /**
93 * The write function that shall be used.
94 * For standard file or stdout loggers this might be standard fwrite
95 * (default).
96 */
97 write_func writer;
98
99 /**
100 * The date format for timestamp outputs including the delimiter
101 * (default: <code>"%F %T %z "</code>).
102 * @see UCX_LOGGER_TIMESTAMP
103 */
104 char *dateformat;
105
106 /**
107 * The level, this logger operates on.
108 * If a log command is issued, the message will only be logged, if the log
109 * level of the message is less or equal than the log level of the logger.
110 */
111 unsigned int level;
112
113 /**
114 * A configuration mask for automatic output.
115 * For each flag that is set, the logger automatically outputs some extra
116 * information like the timestamp or the source file and line number.
117 * See the documentation for the flags for details.
118 */
119 unsigned int mask;
120
121 /**
122 * A map of valid log levels for this logger.
123 *
124 * The keys represent all valid log levels and the values provide string
125 * representations, that are used, if the UCX_LOGGER_LEVEL flag is set.
126 *
127 * The exact data types are <code>unsigned int</code> for the key and
128 * <code>const char*</code> for the value.
129 *
130 * @see UCX_LOGGER_LEVEL
131 */
132 UcxMap* levels;
133 } UcxLogger;
134
135 /**
136 * Creates a new logger.
137 * @param stream the stream, which the logger shall write to
138 * @param level the level on which the logger shall operate
139 * @param mask configuration mask (cf. UcxLogger.mask)
140 * @return a new logger object
141 */
142 UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask);
143
144 /**
145 * Destroys the logger.
146 *
147 * The map containing the valid log levels is also automatically destroyed.
148 *
149 * @param logger the logger to destroy
150 */
151 void ucx_logger_free(UcxLogger* logger);
152
153 /**
154 * Internal log function - use macros instead.
155 *
156 * This function uses the <code>format</code> and variadic arguments for a
157 * printf()-style output of the log message.
158 *
159 * Dependent on the UcxLogger.mask some information is prepended. The complete
160 * format is:
161 *
162 * <code>[LEVEL] [TIMESTAMP] [SOURCEFILE]:[LINENO] message</code>
163 *
164 * <b>Attention:</b> the message (including automatically generated information)
165 * is limited to 4096 characters. The level description is limited to
166 * 256 characters and the timestamp string is limited to 128 characters.
167 *
168 * @param logger the logger to use
169 * @param level the level to log on
170 * @param file information about the source file
171 * @param line information about the source line number
172 * @param format format string
173 * @param ... arguments
174 * @see ucx_logger_log()
175 */
176 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
177 const unsigned int line, const char* format, ...);
178
179 /**
180 * Registers a custom log level.
181 * @param logger the logger
182 * @param level the log level as unsigned integer
183 * @param name a string literal describing the level
184 */
185 #define ucx_logger_register_level(logger, level, name) {\
186 unsigned int l; \
187 l = level; \
188 ucx_map_int_put(logger->levels, l, (void*) "[" name "]"); \
189 } while (0);
190
191 /**
192 * Logs a message at the specified level.
193 * @param logger the logger to use
194 * @param level the level to log the message on
195 * @param ... format string and arguments
196 * @see ucx_logger_logf()
197 */
198 #define ucx_logger_log(logger, level, ...) \
199 ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__)
200
201 /**
202 * Shortcut for logging an error message.
203 * @param logger the logger to use
204 * @param ... format string and arguments
205 * @see ucx_logger_logf()
206 */
207 #define ucx_logger_error(logger, ...) \
208 ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__)
209
210 /**
211 * Shortcut for logging an information message.
212 * @param logger the logger to use
213 * @param ... format string and arguments
214 * @see ucx_logger_logf()
215 */
216 #define ucx_logger_info(logger, ...) \
217 ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__)
218
219 /**
220 * Shortcut for logging a warning message.
221 * @param logger the logger to use
222 * @param ... format string and arguments
223 * @see ucx_logger_logf()
224 */
225 #define ucx_logger_warn(logger, ...) \
226 ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__)
227
228 /**
229 * Shortcut for logging a debug message.
230 * @param logger the logger to use
231 * @param ... format string and arguments
232 * @see ucx_logger_logf()
233 */
234 #define ucx_logger_debug(logger, ...) \
235 ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__)
236
237 /**
238 * Shortcut for logging a trace message.
239 * @param logger the logger to use
240 * @param ... format string and arguments
241 * @see ucx_logger_logf()
242 */
243 #define ucx_logger_trace(logger, ...) \
244 ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__)
245
246 #ifdef __cplusplus
247 }
248 #endif
249
250 #endif /* UCX_LOGGING_H */

mercurial