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()

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

mercurial