Tue, 06 May 2014 14:35:29 +0200
logger: removed unnecessary boundary checks and limited string parts so they always respect buffer lengths
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@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@129 | 163 | * |
universe@138 | 164 | * <b>Attention:</b> the message (including automatically generated information) |
universe@171 | 165 | * is limited to 4096 characters. The level description is limited to |
universe@171 | 166 | * 256 characters and the timestamp string is limited to 128 characters. |
universe@138 | 167 | * |
universe@129 | 168 | * @param logger the logger to use |
universe@129 | 169 | * @param level the level to log on |
universe@129 | 170 | * @param file information about the source file |
universe@129 | 171 | * @param line information about the source line number |
universe@129 | 172 | * @param format format string |
universe@129 | 173 | * @param ... arguments |
universe@129 | 174 | * @see ucx_logger_log() |
universe@129 | 175 | */ |
universe@81 | 176 | void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, |
universe@81 | 177 | const unsigned int line, const char* format, ...); |
universe@129 | 178 | |
universe@129 | 179 | /** |
universe@129 | 180 | * Logs a message at the specified level. |
universe@129 | 181 | * @param logger the logger to use |
universe@129 | 182 | * @param level the level to log the message on |
universe@129 | 183 | * @param ... format string and arguments |
universe@129 | 184 | * @see ucx_logger_logf() |
universe@129 | 185 | */ |
universe@83 | 186 | #define ucx_logger_log(logger, level, ...) \ |
universe@83 | 187 | ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__) |
universe@129 | 188 | |
universe@129 | 189 | /** |
universe@129 | 190 | * Shortcut for logging an error message. |
universe@129 | 191 | * @param logger the logger to use |
universe@129 | 192 | * @param ... format string and arguments |
universe@129 | 193 | * @see ucx_logger_logf() |
universe@129 | 194 | */ |
universe@83 | 195 | #define ucx_logger_error(logger, ...) \ |
universe@83 | 196 | ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__) |
universe@146 | 197 | |
universe@129 | 198 | /** |
universe@129 | 199 | * Shortcut for logging an information message. |
universe@129 | 200 | * @param logger the logger to use |
universe@129 | 201 | * @param ... format string and arguments |
universe@129 | 202 | * @see ucx_logger_logf() |
universe@129 | 203 | */ |
universe@83 | 204 | #define ucx_logger_info(logger, ...) \ |
universe@83 | 205 | ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__) |
universe@146 | 206 | |
universe@129 | 207 | /** |
universe@129 | 208 | * Shortcut for logging a warning 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_warn(logger, ...) \ |
universe@83 | 214 | ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__) |
universe@146 | 215 | |
universe@129 | 216 | /** |
universe@129 | 217 | * Shortcut for logging a debug message. |
universe@129 | 218 | * @param logger the logger to use |
universe@129 | 219 | * @param ... format string and arguments |
universe@129 | 220 | * @see ucx_logger_logf() |
universe@129 | 221 | */ |
universe@129 | 222 | #define ucx_logger_debug(logger, ...) \ |
universe@129 | 223 | ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__) |
universe@146 | 224 | |
universe@129 | 225 | /** |
universe@129 | 226 | * Shortcut for logging a trace message. |
universe@129 | 227 | * @param logger the logger to use |
universe@129 | 228 | * @param ... format string and arguments |
universe@129 | 229 | * @see ucx_logger_logf() |
universe@129 | 230 | */ |
universe@83 | 231 | #define ucx_logger_trace(logger, ...) \ |
universe@83 | 232 | ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__) |
olaf@57 | 233 | |
olaf@57 | 234 | #ifdef __cplusplus |
olaf@57 | 235 | } |
olaf@57 | 236 | #endif |
olaf@57 | 237 | |
olaf@120 | 238 | #endif /* UCX_LOGGING_H */ |