ucx/logging.h

changeset 5
88625853ae74
parent 1
1bcaac272cdf
child 17
11dffb40cd91
equal deleted inserted replaced
4:ae5a98f0545c 5:88625853ae74
1 #ifndef LOGGING_H 1 /*
2 #define LOGGING_H 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 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
3 36
4 #include "ucx.h" 37 #include "ucx.h"
38 #include "map.h"
5 #include "string.h" 39 #include "string.h"
6 #include <stdio.h> 40 #include <stdio.h>
7 41
8 #ifdef __cplusplus 42 #ifdef __cplusplus
9 extern "C" { 43 extern "C" {
10 #endif 44 #endif
11 45
12 /* leave enough space for custom log levels */ 46 /* leave enough space for custom log levels */
13 #define UCX_LOGGER_ERROR 0x00 47 /** Log level for error messages. */
14 #define UCX_LOGGER_WARN 0x10 48 #define UCX_LOGGER_ERROR 0x00
15 #define UCX_LOGGER_INFO 0x20 49 /** Log level for warning messages. */
16 #define UCX_LOGGER_TRACE 0x30 50 #define UCX_LOGGER_WARN 0x10
17 51 /** Log level for information messages. */
52 #define UCX_LOGGER_INFO 0x20
53 /** Log level for debug messages. */
54 #define UCX_LOGGER_DEBUG 0x30
55 /** Log level for trace messages. */
56 #define UCX_LOGGER_TRACE 0x40
57
58 /**
59 * Output flag for the log level.
60 * If this flag is set, the log message will contain the log level.
61 * @see UcxLogger.mask
62 */
63 #define UCX_LOGGER_LEVEL 0x01
64 /**
65 * Output flag for the timestmap.
66 * If this flag is set, the log message will contain the timestmap.
67 * @see UcxLogger.mask
68 */
69 #define UCX_LOGGER_TIMESTAMP 0x02
70 /**
71 * Output flag for the source.
72 * If this flag is set, the log message will contain the source file and line
73 * number.
74 * @see UcxLogger.mask
75 */
76 #define UCX_LOGGER_SOURCE 0x04
77
78 /**
79 * The UCX Logger object.
80 */
18 typedef struct { 81 typedef struct {
19 FILE *stream; 82 /** The stream this logger writes its messages to.*/
83 void *stream;
84 /**
85 * The write function that shall be used.
86 * For standard file or stdout loggers this might be standard fwrite
87 * (default).
88 */
89 write_func writer;
90 /**
91 * The date format for timestamp outputs
92 * (default: <code>"%F %T %z "</code>).
93 * @see UCX_LOGGER_TIMESTAMP
94 */
95 char *dateformat;
96 /**
97 * The level, this logger operates on.
98 * If a log command is issued, the message will only be logged, if the log
99 * level of the message is less or equal than the log level of the logger.
100 */
20 unsigned int level; 101 unsigned int level;
102 /**
103 * A configuration mask for automatic output.
104 * For each flag that is set, the logger automatically outputs some extra
105 * information like the timestamp or the source file and line number.
106 * See the documentation for the flags for details.
107 */
108 unsigned int mask;
109 /**
110 * A map of valid log levels for this logger.
111 *
112 * The keys represent all valid log levels and the values provide string
113 * representations, that are used, if the UCX_LOGGER_LEVEL flag is set.
114 *
115 * The exact data types are <code>unsigned int</code> for the key and
116 * <code>const char*</code> for the value.
117 *
118 * @see UCX_LOGGER_LEVEL
119 */
120 UcxMap* levels;
21 } UcxLogger; 121 } UcxLogger;
22 122
23 UcxLogger *ucx_logger_new(FILE *stream, unsigned int level); 123 /**
24 /* neither provide a free function nor a parameter for an allocator */ 124 * Creates a new logger.
25 125 * @param stream the stream, which the logger shall write to
26 void ucx_logger_log(UcxLogger *logger, unsigned int level, 126 * @param level the level on which the logger shall operate
27 const sstr_t message); 127 * @param mask configuration mask (cf. UcxLogger.mask)
28 #define ucx_logger_error(l,m) ucx_logger_log(l, UCX_LOGGER_ERROR, m) 128 * @return a new logger object
29 #define ucx_logger_info(l,m) ucx_logger_log(l, UCX_LOGGER_INFO, m) 129 */
30 #define ucx_logger_warn(l,m) ucx_logger_log(l, UCX_LOGGER_WARN, m) 130 UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask);
31 #define ucx_logger_trace(l,m) ucx_logger_log(l, UCX_LOGGER_TRACE, m) 131 /**
132 * Destroys the logger.
133 *
134 * The map containing the valid log levels is also automatically destroyed.
135 *
136 * @param logger the logger to destroy
137 */
138 void ucx_logger_free(UcxLogger* logger);
139
140 /**
141 * Internal log function - use macros instead.
142 *
143 * This function uses the <code>format</code> and variadic arguments for a
144 * printf()-style output of the log message.
145 *
146 * Dependent on the UcxLogger.mask some information is prepended. The complete
147 * format is:
148 *
149 * <code>[LEVEL] [TIMESTAMP] [SOURCEFILE]:[LINENO] message</code>
150 *
151 * @param logger the logger to use
152 * @param level the level to log on
153 * @param file information about the source file
154 * @param line information about the source line number
155 * @param format format string
156 * @param ... arguments
157 * @see ucx_logger_log()
158 */
159 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
160 const unsigned int line, const char* format, ...);
161
162 /**
163 * Logs a message at the specified level.
164 * @param logger the logger to use
165 * @param level the level to log the message on
166 * @param ... format string and arguments
167 * @see ucx_logger_logf()
168 */
169 #define ucx_logger_log(logger, level, ...) \
170 ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__)
171
172 /**
173 * Shortcut for logging an error message.
174 * @param logger the logger to use
175 * @param ... format string and arguments
176 * @see ucx_logger_logf()
177 */
178 #define ucx_logger_error(logger, ...) \
179 ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__)
180 /**
181 * Shortcut for logging an information message.
182 * @param logger the logger to use
183 * @param ... format string and arguments
184 * @see ucx_logger_logf()
185 */
186 #define ucx_logger_info(logger, ...) \
187 ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__)
188 /**
189 * Shortcut for logging a warning message.
190 * @param logger the logger to use
191 * @param ... format string and arguments
192 * @see ucx_logger_logf()
193 */
194 #define ucx_logger_warn(logger, ...) \
195 ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__)
196 /**
197 * Shortcut for logging a debug message.
198 * @param logger the logger to use
199 * @param ... format string and arguments
200 * @see ucx_logger_logf()
201 */
202 #define ucx_logger_debug(logger, ...) \
203 ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__)
204 /**
205 * Shortcut for logging a trace message.
206 * @param logger the logger to use
207 * @param ... format string and arguments
208 * @see ucx_logger_logf()
209 */
210 #define ucx_logger_trace(logger, ...) \
211 ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__)
32 212
33 #ifdef __cplusplus 213 #ifdef __cplusplus
34 } 214 }
35 #endif 215 #endif
36 216
37 #endif /* LOGGING_H */ 217 #endif /* UCX_LOGGING_H */

mercurial