universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@259: * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@103: */ universe@129: /** universe@129: * Logging API. universe@129: * universe@129: * @file logging.h universe@129: * @author Mike Becker, Olaf Wintermann universe@129: */ olaf@120: #ifndef UCX_LOGGING_H olaf@120: #define UCX_LOGGING_H olaf@57: universe@259: #include "ucx.h" universe@259: #include "map.h" universe@259: #include "string.h" olaf@57: #include olaf@57: olaf@57: #ifdef __cplusplus olaf@57: extern "C" { olaf@57: #endif olaf@57: olaf@57: /* leave enough space for custom log levels */ universe@146: universe@129: /** Log level for error messages. */ universe@80: #define UCX_LOGGER_ERROR 0x00 universe@146: universe@129: /** Log level for warning messages. */ universe@80: #define UCX_LOGGER_WARN 0x10 universe@146: universe@129: /** Log level for information messages. */ universe@80: #define UCX_LOGGER_INFO 0x20 universe@146: universe@129: /** Log level for debug messages. */ universe@129: #define UCX_LOGGER_DEBUG 0x30 universe@146: universe@129: /** Log level for trace messages. */ universe@129: #define UCX_LOGGER_TRACE 0x40 universe@80: universe@129: /** universe@129: * Output flag for the log level. universe@129: * If this flag is set, the log message will contain the log level. universe@129: * @see UcxLogger.mask universe@129: */ universe@80: #define UCX_LOGGER_LEVEL 0x01 universe@146: universe@129: /** universe@129: * Output flag for the timestmap. universe@129: * If this flag is set, the log message will contain the timestmap. universe@129: * @see UcxLogger.mask universe@129: */ universe@80: #define UCX_LOGGER_TIMESTAMP 0x02 universe@146: universe@129: /** universe@129: * Output flag for the source. universe@129: * If this flag is set, the log message will contain the source file and line universe@129: * number. universe@129: * @see UcxLogger.mask universe@129: */ universe@80: #define UCX_LOGGER_SOURCE 0x04 olaf@57: universe@129: /** universe@129: * The UCX Logger object. universe@129: */ olaf@57: typedef struct { universe@129: /** The stream this logger writes its messages to.*/ universe@83: void *stream; universe@146: universe@129: /** universe@129: * The write function that shall be used. universe@129: * For standard file or stdout loggers this might be standard fwrite universe@129: * (default). universe@129: */ universe@82: write_func writer; universe@146: universe@129: /** universe@170: * The date format for timestamp outputs including the delimiter universe@129: * (default: "%F %T %z "). universe@129: * @see UCX_LOGGER_TIMESTAMP universe@129: */ universe@82: char *dateformat; universe@146: universe@129: /** universe@129: * The level, this logger operates on. universe@129: * If a log command is issued, the message will only be logged, if the log universe@129: * level of the message is less or equal than the log level of the logger. universe@129: */ olaf@57: unsigned int level; universe@146: universe@129: /** universe@129: * A configuration mask for automatic output. universe@129: * For each flag that is set, the logger automatically outputs some extra universe@129: * information like the timestamp or the source file and line number. universe@129: * See the documentation for the flags for details. universe@129: */ universe@80: unsigned int mask; universe@146: universe@129: /** universe@129: * A map of valid log levels for this logger. universe@129: * universe@129: * The keys represent all valid log levels and the values provide string universe@129: * representations, that are used, if the UCX_LOGGER_LEVEL flag is set. universe@129: * universe@129: * The exact data types are unsigned int for the key and universe@129: * const char* for the value. universe@129: * universe@129: * @see UCX_LOGGER_LEVEL universe@129: */ universe@80: UcxMap* levels; olaf@57: } UcxLogger; olaf@57: universe@129: /** universe@129: * Creates a new logger. universe@129: * @param stream the stream, which the logger shall write to universe@129: * @param level the level on which the logger shall operate universe@129: * @param mask configuration mask (cf. UcxLogger.mask) universe@129: * @return a new logger object universe@129: */ universe@83: UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask); universe@146: universe@129: /** universe@129: * Destroys the logger. universe@129: * universe@129: * The map containing the valid log levels is also automatically destroyed. universe@129: * universe@129: * @param logger the logger to destroy universe@129: */ universe@80: void ucx_logger_free(UcxLogger* logger); olaf@57: universe@129: /** universe@129: * Internal log function - use macros instead. universe@129: * universe@129: * This function uses the format and variadic arguments for a universe@129: * printf()-style output of the log message. universe@129: * universe@129: * Dependent on the UcxLogger.mask some information is prepended. The complete universe@129: * format is: universe@129: * universe@129: * [LEVEL] [TIMESTAMP] [SOURCEFILE]:[LINENO] message universe@129: * universe@138: * Attention: the message (including automatically generated information) universe@171: * is limited to 4096 characters. The level description is limited to universe@171: * 256 characters and the timestamp string is limited to 128 characters. universe@138: * universe@129: * @param logger the logger to use universe@129: * @param level the level to log on universe@129: * @param file information about the source file universe@129: * @param line information about the source line number universe@129: * @param format format string universe@129: * @param ... arguments universe@129: * @see ucx_logger_log() universe@129: */ universe@81: void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, universe@81: const unsigned int line, const char* format, ...); universe@129: universe@129: /** universe@129: * Logs a message at the specified level. universe@129: * @param logger the logger to use universe@129: * @param level the level to log the message on universe@129: * @param ... format string and arguments universe@129: * @see ucx_logger_logf() universe@129: */ universe@83: #define ucx_logger_log(logger, level, ...) \ universe@83: ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__) universe@129: universe@129: /** universe@129: * Shortcut for logging an error message. universe@129: * @param logger the logger to use universe@129: * @param ... format string and arguments universe@129: * @see ucx_logger_logf() universe@129: */ universe@83: #define ucx_logger_error(logger, ...) \ universe@83: ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__) universe@146: universe@129: /** universe@129: * Shortcut for logging an information message. universe@129: * @param logger the logger to use universe@129: * @param ... format string and arguments universe@129: * @see ucx_logger_logf() universe@129: */ universe@83: #define ucx_logger_info(logger, ...) \ universe@83: ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__) universe@146: universe@129: /** universe@129: * Shortcut for logging a warning message. universe@129: * @param logger the logger to use universe@129: * @param ... format string and arguments universe@129: * @see ucx_logger_logf() universe@129: */ universe@83: #define ucx_logger_warn(logger, ...) \ universe@83: ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__) universe@146: universe@129: /** universe@129: * Shortcut for logging a debug message. universe@129: * @param logger the logger to use universe@129: * @param ... format string and arguments universe@129: * @see ucx_logger_logf() universe@129: */ universe@129: #define ucx_logger_debug(logger, ...) \ universe@129: ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__) universe@146: universe@129: /** universe@129: * Shortcut for logging a trace message. universe@129: * @param logger the logger to use universe@129: * @param ... format string and arguments universe@129: * @see ucx_logger_logf() universe@129: */ universe@83: #define ucx_logger_trace(logger, ...) \ universe@83: ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__) olaf@57: olaf@57: #ifdef __cplusplus olaf@57: } olaf@57: #endif olaf@57: olaf@120: #endif /* UCX_LOGGING_H */