src/ucx/logging.h

changeset 390
d345541018fa
parent 389
92e482410453
child 391
f094a53c1178
     1.1 --- a/src/ucx/logging.h	Mon Dec 30 09:54:10 2019 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,253 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2017 Mike Becker, 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.h"
    1.41 -#include "map.h"
    1.42 -#include "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 - * The source file name is reduced to the actual file name. This is necessary to
   1.168 - * get consistent behavior over different definitions of the __FILE__ macro.
   1.169 - *
   1.170 - * <b>Attention:</b> the message (including automatically generated information)
   1.171 - * is limited to 4096 characters. The level description is limited to
   1.172 - * 256 characters and the timestamp string is limited to 128 characters.
   1.173 - * 
   1.174 - * @param logger the logger to use
   1.175 - * @param level the level to log on
   1.176 - * @param file information about the source file
   1.177 - * @param line information about the source line number
   1.178 - * @param format format string
   1.179 - * @param ... arguments
   1.180 - * @see ucx_logger_log()
   1.181 - */
   1.182 -void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
   1.183 -        const unsigned int line, const char* format, ...);
   1.184 -
   1.185 -/**
   1.186 - * Registers a custom log level.
   1.187 - * @param logger the logger
   1.188 - * @param level the log level as unsigned integer
   1.189 - * @param name a string literal describing the level
   1.190 - */
   1.191 -#define ucx_logger_register_level(logger, level, name) {\
   1.192 -        unsigned int l; \
   1.193 -            l = level; \
   1.194 -            ucx_map_int_put(logger->levels, l, (void*) "[" name "]"); \
   1.195 -        } while (0);
   1.196 -
   1.197 -/**
   1.198 - * Logs a message at the specified level.
   1.199 - * @param logger the logger to use
   1.200 - * @param level the level to log the message on
   1.201 - * @param ... format string and arguments
   1.202 - * @see ucx_logger_logf()
   1.203 - */
   1.204 -#define ucx_logger_log(logger, level, ...) \
   1.205 -    ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__)
   1.206 -
   1.207 -/**
   1.208 - * Shortcut for logging an error message.
   1.209 - * @param logger the logger to use
   1.210 - * @param ... format string and arguments
   1.211 - * @see ucx_logger_logf()
   1.212 - */
   1.213 -#define ucx_logger_error(logger, ...) \
   1.214 -    ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__)
   1.215 -
   1.216 -/**
   1.217 - * Shortcut for logging an information message.
   1.218 - * @param logger the logger to use
   1.219 - * @param ... format string and arguments
   1.220 - * @see ucx_logger_logf()
   1.221 - */
   1.222 -#define ucx_logger_info(logger, ...) \
   1.223 -    ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__)
   1.224 -
   1.225 -/**
   1.226 - * Shortcut for logging a warning message.
   1.227 - * @param logger the logger to use
   1.228 - * @param ... format string and arguments
   1.229 - * @see ucx_logger_logf()
   1.230 - */
   1.231 -#define ucx_logger_warn(logger, ...) \
   1.232 -    ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__)
   1.233 -
   1.234 -/**
   1.235 - * Shortcut for logging a debug message.
   1.236 - * @param logger the logger to use
   1.237 - * @param ... format string and arguments
   1.238 - * @see ucx_logger_logf()
   1.239 - */
   1.240 -#define ucx_logger_debug(logger, ...) \
   1.241 -    ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__)
   1.242 -
   1.243 -/**
   1.244 - * Shortcut for logging a trace message.
   1.245 - * @param logger the logger to use
   1.246 - * @param ... format string and arguments
   1.247 - * @see ucx_logger_logf()
   1.248 - */
   1.249 -#define ucx_logger_trace(logger, ...) \
   1.250 -    ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__)
   1.251 -
   1.252 -#ifdef __cplusplus
   1.253 -}
   1.254 -#endif
   1.255 -
   1.256 -#endif /* UCX_LOGGING_H */

mercurial