ucx/logging.h

Mon, 12 Aug 2013 14:39:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 12 Aug 2013 14:39:51 +0200
changeset 138
7800811078b8
parent 129
61edec666928
child 146
aa376dba1ba8
permissions
-rw-r--r--

documented map.h + changed return value of ucx_map_iter_next()

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

mercurial