src/ucx/logging.h

Fri, 20 Dec 2019 14:44:50 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 20 Dec 2019 14:44:50 +0100
changeset 376
028039652b86
parent 295
7fc65395188e
permissions
-rw-r--r--

fixes logging of absolute paths in ucx_logger_logf()

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

mercurial