2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
32 * @author Mike Becker, Olaf Wintermann
46 /* leave enough space for custom log levels */
48 /** Log level for error messages. */
49 #define UCX_LOGGER_ERROR 0x00
51 /** Log level for warning messages. */
52 #define UCX_LOGGER_WARN 0x10
54 /** Log level for information messages. */
55 #define UCX_LOGGER_INFO 0x20
57 /** Log level for debug messages. */
58 #define UCX_LOGGER_DEBUG 0x30
60 /** Log level for trace messages. */
61 #define UCX_LOGGER_TRACE 0x40
64 * Output flag for the log level.
65 * If this flag is set, the log message will contain the log level.
68 #define UCX_LOGGER_LEVEL 0x01
71 * Output flag for the timestmap.
72 * If this flag is set, the log message will contain the timestmap.
75 #define UCX_LOGGER_TIMESTAMP 0x02
78 * Output flag for the source.
79 * If this flag is set, the log message will contain the source file and line
83 #define UCX_LOGGER_SOURCE 0x04
86 * The UCX Logger object.
89 /** The stream this logger writes its messages to.*/
93 * The write function that shall be used.
94 * For standard file or stdout loggers this might be standard fwrite
100 * The date format for timestamp outputs including the delimiter
101 * (default: <code>"%F %T %z "</code>).
102 * @see UCX_LOGGER_TIMESTAMP
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.
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.
122 * A map of valid log levels for this logger.
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.
127 * The exact data types are <code>unsigned int</code> for the key and
128 * <code>const char*</code> for the value.
130 * @see UCX_LOGGER_LEVEL
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
142 UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask);
145 * Destroys the logger.
147 * The map containing the valid log levels is also automatically destroyed.
149 * @param logger the logger to destroy
151 void ucx_logger_free(UcxLogger* logger);
154 * Internal log function - use macros instead.
156 * This function uses the <code>format</code> and variadic arguments for a
157 * printf()-style output of the log message.
159 * Dependent on the UcxLogger.mask some information is prepended. The complete
162 * <code>[LEVEL] [TIMESTAMP] [SOURCEFILE]:[LINENO] message</code>
164 * The source file name is reduced to the actual file name. This is necessary to
165 * get consistent behavior over different definitions of the __FILE__ macro.
167 * <b>Attention:</b> the message (including automatically generated information)
168 * is limited to 4096 characters. The level description is limited to
169 * 256 characters and the timestamp string is limited to 128 characters.
171 * @param logger the logger to use
172 * @param level the level to log on
173 * @param file information about the source file
174 * @param line information about the source line number
175 * @param format format string
176 * @param ... arguments
177 * @see ucx_logger_log()
179 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
180 const unsigned int line, const char* format, ...);
183 * Registers a custom log level.
184 * @param logger the logger
185 * @param level the log level as unsigned integer
186 * @param name a string literal describing the level
188 #define ucx_logger_register_level(logger, level, name) {\
191 ucx_map_int_put(logger->levels, l, (void*) "[" name "]"); \
195 * Logs a message at the specified level.
196 * @param logger the logger to use
197 * @param level the level to log the message on
198 * @param ... format string and arguments
199 * @see ucx_logger_logf()
201 #define ucx_logger_log(logger, level, ...) \
202 ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__)
205 * Shortcut for logging an error message.
206 * @param logger the logger to use
207 * @param ... format string and arguments
208 * @see ucx_logger_logf()
210 #define ucx_logger_error(logger, ...) \
211 ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__)
214 * Shortcut for logging an information message.
215 * @param logger the logger to use
216 * @param ... format string and arguments
217 * @see ucx_logger_logf()
219 #define ucx_logger_info(logger, ...) \
220 ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__)
223 * Shortcut for logging a warning message.
224 * @param logger the logger to use
225 * @param ... format string and arguments
226 * @see ucx_logger_logf()
228 #define ucx_logger_warn(logger, ...) \
229 ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__)
232 * Shortcut for logging a debug message.
233 * @param logger the logger to use
234 * @param ... format string and arguments
235 * @see ucx_logger_logf()
237 #define ucx_logger_debug(logger, ...) \
238 ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__)
241 * Shortcut for logging a trace message.
242 * @param logger the logger to use
243 * @param ... format string and arguments
244 * @see ucx_logger_logf()
246 #define ucx_logger_trace(logger, ...) \
247 ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__)
253 #endif /* UCX_LOGGING_H */