1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/ucx/logging.h Tue Oct 17 16:15:41 2017 +0200 1.3 @@ -0,0 +1,238 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2017 Olaf Wintermann. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 +/** 1.32 + * Logging API. 1.33 + * 1.34 + * @file logging.h 1.35 + * @author Mike Becker, Olaf Wintermann 1.36 + */ 1.37 +#ifndef UCX_LOGGING_H 1.38 +#define UCX_LOGGING_H 1.39 + 1.40 +#include <ucx/ucx.h> 1.41 +#include <ucx/map.h> 1.42 +#include <ucx/string.h> 1.43 +#include <stdio.h> 1.44 + 1.45 +#ifdef __cplusplus 1.46 +extern "C" { 1.47 +#endif 1.48 + 1.49 +/* leave enough space for custom log levels */ 1.50 + 1.51 +/** Log level for error messages. */ 1.52 +#define UCX_LOGGER_ERROR 0x00 1.53 + 1.54 +/** Log level for warning messages. */ 1.55 +#define UCX_LOGGER_WARN 0x10 1.56 + 1.57 +/** Log level for information messages. */ 1.58 +#define UCX_LOGGER_INFO 0x20 1.59 + 1.60 +/** Log level for debug messages. */ 1.61 +#define UCX_LOGGER_DEBUG 0x30 1.62 + 1.63 +/** Log level for trace messages. */ 1.64 +#define UCX_LOGGER_TRACE 0x40 1.65 + 1.66 +/** 1.67 + * Output flag for the log level. 1.68 + * If this flag is set, the log message will contain the log level. 1.69 + * @see UcxLogger.mask 1.70 + */ 1.71 +#define UCX_LOGGER_LEVEL 0x01 1.72 + 1.73 +/** 1.74 + * Output flag for the timestmap. 1.75 + * If this flag is set, the log message will contain the timestmap. 1.76 + * @see UcxLogger.mask 1.77 + */ 1.78 +#define UCX_LOGGER_TIMESTAMP 0x02 1.79 + 1.80 +/** 1.81 + * Output flag for the source. 1.82 + * If this flag is set, the log message will contain the source file and line 1.83 + * number. 1.84 + * @see UcxLogger.mask 1.85 + */ 1.86 +#define UCX_LOGGER_SOURCE 0x04 1.87 + 1.88 +/** 1.89 + * The UCX Logger object. 1.90 + */ 1.91 +typedef struct { 1.92 + /** The stream this logger writes its messages to.*/ 1.93 + void *stream; 1.94 + 1.95 + /** 1.96 + * The write function that shall be used. 1.97 + * For standard file or stdout loggers this might be standard fwrite 1.98 + * (default). 1.99 + */ 1.100 + write_func writer; 1.101 + 1.102 + /** 1.103 + * The date format for timestamp outputs including the delimiter 1.104 + * (default: <code>"%F %T %z "</code>). 1.105 + * @see UCX_LOGGER_TIMESTAMP 1.106 + */ 1.107 + char *dateformat; 1.108 + 1.109 + /** 1.110 + * The level, this logger operates on. 1.111 + * If a log command is issued, the message will only be logged, if the log 1.112 + * level of the message is less or equal than the log level of the logger. 1.113 + */ 1.114 + unsigned int level; 1.115 + 1.116 + /** 1.117 + * A configuration mask for automatic output. 1.118 + * For each flag that is set, the logger automatically outputs some extra 1.119 + * information like the timestamp or the source file and line number. 1.120 + * See the documentation for the flags for details. 1.121 + */ 1.122 + unsigned int mask; 1.123 + 1.124 + /** 1.125 + * A map of valid log levels for this logger. 1.126 + * 1.127 + * The keys represent all valid log levels and the values provide string 1.128 + * representations, that are used, if the UCX_LOGGER_LEVEL flag is set. 1.129 + * 1.130 + * The exact data types are <code>unsigned int</code> for the key and 1.131 + * <code>const char*</code> for the value. 1.132 + * 1.133 + * @see UCX_LOGGER_LEVEL 1.134 + */ 1.135 + UcxMap* levels; 1.136 +} UcxLogger; 1.137 + 1.138 +/** 1.139 + * Creates a new logger. 1.140 + * @param stream the stream, which the logger shall write to 1.141 + * @param level the level on which the logger shall operate 1.142 + * @param mask configuration mask (cf. UcxLogger.mask) 1.143 + * @return a new logger object 1.144 + */ 1.145 +UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask); 1.146 + 1.147 +/** 1.148 + * Destroys the logger. 1.149 + * 1.150 + * The map containing the valid log levels is also automatically destroyed. 1.151 + * 1.152 + * @param logger the logger to destroy 1.153 + */ 1.154 +void ucx_logger_free(UcxLogger* logger); 1.155 + 1.156 +/** 1.157 + * Internal log function - use macros instead. 1.158 + * 1.159 + * This function uses the <code>format</code> and variadic arguments for a 1.160 + * printf()-style output of the log message. 1.161 + * 1.162 + * Dependent on the UcxLogger.mask some information is prepended. The complete 1.163 + * format is: 1.164 + * 1.165 + * <code>[LEVEL] [TIMESTAMP] [SOURCEFILE]:[LINENO] message</code> 1.166 + * 1.167 + * <b>Attention:</b> the message (including automatically generated information) 1.168 + * is limited to 4096 characters. The level description is limited to 1.169 + * 256 characters and the timestamp string is limited to 128 characters. 1.170 + * 1.171 + * @param logger the logger to use 1.172 + * @param level the level to log on 1.173 + * @param file information about the source file 1.174 + * @param line information about the source line number 1.175 + * @param format format string 1.176 + * @param ... arguments 1.177 + * @see ucx_logger_log() 1.178 + */ 1.179 +void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, 1.180 + const unsigned int line, const char* format, ...); 1.181 + 1.182 +/** 1.183 + * Logs a message at the specified level. 1.184 + * @param logger the logger to use 1.185 + * @param level the level to log the message on 1.186 + * @param ... format string and arguments 1.187 + * @see ucx_logger_logf() 1.188 + */ 1.189 +#define ucx_logger_log(logger, level, ...) \ 1.190 + ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__) 1.191 + 1.192 +/** 1.193 + * Shortcut for logging an error message. 1.194 + * @param logger the logger to use 1.195 + * @param ... format string and arguments 1.196 + * @see ucx_logger_logf() 1.197 + */ 1.198 +#define ucx_logger_error(logger, ...) \ 1.199 + ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__) 1.200 + 1.201 +/** 1.202 + * Shortcut for logging an information message. 1.203 + * @param logger the logger to use 1.204 + * @param ... format string and arguments 1.205 + * @see ucx_logger_logf() 1.206 + */ 1.207 +#define ucx_logger_info(logger, ...) \ 1.208 + ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__) 1.209 + 1.210 +/** 1.211 + * Shortcut for logging a warning message. 1.212 + * @param logger the logger to use 1.213 + * @param ... format string and arguments 1.214 + * @see ucx_logger_logf() 1.215 + */ 1.216 +#define ucx_logger_warn(logger, ...) \ 1.217 + ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__) 1.218 + 1.219 +/** 1.220 + * Shortcut for logging a debug message. 1.221 + * @param logger the logger to use 1.222 + * @param ... format string and arguments 1.223 + * @see ucx_logger_logf() 1.224 + */ 1.225 +#define ucx_logger_debug(logger, ...) \ 1.226 + ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__) 1.227 + 1.228 +/** 1.229 + * Shortcut for logging a trace message. 1.230 + * @param logger the logger to use 1.231 + * @param ... format string and arguments 1.232 + * @see ucx_logger_logf() 1.233 + */ 1.234 +#define ucx_logger_trace(logger, ...) \ 1.235 + ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__) 1.236 + 1.237 +#ifdef __cplusplus 1.238 +} 1.239 +#endif 1.240 + 1.241 +#endif /* UCX_LOGGING_H */