ucx/logging.h

changeset 129
61edec666928
parent 120
8170f658f017
child 138
7800811078b8
     1.1 --- a/ucx/logging.h	Tue Jul 23 14:43:45 2013 +0200
     1.2 +++ b/ucx/logging.h	Wed Jul 24 14:26:17 2013 +0200
     1.3 @@ -25,7 +25,12 @@
     1.4   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     1.5   * POSSIBILITY OF SUCH DAMAGE.
     1.6   */
     1.7 -
     1.8 +/**
     1.9 + * Logging API.
    1.10 + * 
    1.11 + * @file   logging.h
    1.12 + * @author Mike Becker, Olaf Wintermann
    1.13 + */
    1.14  #ifndef UCX_LOGGING_H
    1.15  #define UCX_LOGGING_H
    1.16  
    1.17 @@ -39,37 +44,169 @@
    1.18  #endif
    1.19  
    1.20  /* leave enough space for custom log levels */
    1.21 +/** Log level for error messages. */
    1.22  #define UCX_LOGGER_ERROR        0x00
    1.23 +/** Log level for warning messages. */
    1.24  #define UCX_LOGGER_WARN         0x10
    1.25 +/** Log level for information messages. */
    1.26  #define UCX_LOGGER_INFO         0x20
    1.27 -#define UCX_LOGGER_TRACE        0x30
    1.28 +/** Log level for debug messages. */
    1.29 +#define UCX_LOGGER_DEBUG        0x30
    1.30 +/** Log level for trace messages. */
    1.31 +#define UCX_LOGGER_TRACE        0x40
    1.32  
    1.33 +/**
    1.34 + * Output flag for the log level. 
    1.35 + * If this flag is set, the log message will contain the log level.
    1.36 + * @see UcxLogger.mask
    1.37 + */
    1.38  #define UCX_LOGGER_LEVEL        0x01
    1.39 +/**
    1.40 + * Output flag for the timestmap.
    1.41 + * If this flag is set, the log message will contain the timestmap.
    1.42 + * @see UcxLogger.mask
    1.43 + */
    1.44  #define UCX_LOGGER_TIMESTAMP    0x02
    1.45 +/**
    1.46 + * Output flag for the source.
    1.47 + * If this flag is set, the log message will contain the source file and line
    1.48 + * number.
    1.49 + * @see UcxLogger.mask
    1.50 + */
    1.51  #define UCX_LOGGER_SOURCE       0x04
    1.52  
    1.53 +/**
    1.54 + * The UCX Logger object.
    1.55 + */
    1.56  typedef struct {
    1.57 +    /** The stream this logger writes its messages to.*/
    1.58      void *stream;
    1.59 +    /**
    1.60 +     * The write function that shall be used.
    1.61 +     * For standard file or stdout loggers this might be standard fwrite
    1.62 +     * (default).
    1.63 +     */
    1.64      write_func writer;
    1.65 +    /**
    1.66 +     * The date format for timestamp outputs
    1.67 +     * (default: <code>"%F %T %z "</code>).
    1.68 +     * @see UCX_LOGGER_TIMESTAMP
    1.69 +     */
    1.70      char *dateformat;
    1.71 +    /**
    1.72 +     * The level, this logger operates on.
    1.73 +     * If a log command is issued, the message will only be logged, if the log
    1.74 +     * level of the message is less or equal than the log level of the logger.
    1.75 +     */
    1.76      unsigned int level;
    1.77 +    /**
    1.78 +     * A configuration mask for automatic output. 
    1.79 +     * For each flag that is set, the logger automatically outputs some extra
    1.80 +     * information like the timestamp or the source file and line number.
    1.81 +     * See the documentation for the flags for details.
    1.82 +     */
    1.83      unsigned int mask;
    1.84 +    /**
    1.85 +     * A map of valid log levels for this logger.
    1.86 +     * 
    1.87 +     * The keys represent all valid log levels and the values provide string
    1.88 +     * representations, that are used, if the UCX_LOGGER_LEVEL flag is set.
    1.89 +     * 
    1.90 +     * The exact data types are <code>unsigned int</code> for the key and
    1.91 +     * <code>const char*</code> for the value.
    1.92 +     * 
    1.93 +     * @see UCX_LOGGER_LEVEL
    1.94 +     */
    1.95      UcxMap* levels;
    1.96  } UcxLogger;
    1.97  
    1.98 +/**
    1.99 + * Creates a new logger.
   1.100 + * @param stream the stream, which the logger shall write to
   1.101 + * @param level the level on which the logger shall operate
   1.102 + * @param mask configuration mask (cf. UcxLogger.mask)
   1.103 + * @return a new logger object
   1.104 + */
   1.105  UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask);
   1.106 +/**
   1.107 + * Destroys the logger.
   1.108 + * 
   1.109 + * The map containing the valid log levels is also automatically destroyed.
   1.110 + * 
   1.111 + * @param logger the logger to destroy
   1.112 + */
   1.113  void ucx_logger_free(UcxLogger* logger);
   1.114  
   1.115 +/**
   1.116 + * Internal log function - use macros instead.
   1.117 + * 
   1.118 + * This function uses the <code>format</code> and variadic arguments for a
   1.119 + * printf()-style output of the log message.
   1.120 + * 
   1.121 + * Dependent on the UcxLogger.mask some information is prepended. The complete
   1.122 + * format is:
   1.123 + * 
   1.124 + * <code>[LEVEL] [TIMESTAMP] [SOURCEFILE]:[LINENO] message</code>
   1.125 + * 
   1.126 + * @param logger the logger to use
   1.127 + * @param level the level to log on
   1.128 + * @param file information about the source file
   1.129 + * @param line information about the source line number
   1.130 + * @param format format string
   1.131 + * @param ... arguments
   1.132 + * @see ucx_logger_log()
   1.133 + */
   1.134  void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
   1.135          const unsigned int line, const char* format, ...);
   1.136 +
   1.137 +/**
   1.138 + * Logs a message at the specified level.
   1.139 + * @param logger the logger to use
   1.140 + * @param level the level to log the message on
   1.141 + * @param ... format string and arguments
   1.142 + * @see ucx_logger_logf()
   1.143 + */
   1.144  #define ucx_logger_log(logger, level, ...) \
   1.145      ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__)
   1.146 +
   1.147 +/**
   1.148 + * Shortcut for logging an error message.
   1.149 + * @param logger the logger to use
   1.150 + * @param ... format string and arguments
   1.151 + * @see ucx_logger_logf()
   1.152 + */
   1.153  #define ucx_logger_error(logger, ...) \
   1.154      ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__)
   1.155 +/**
   1.156 + * Shortcut for logging an information message.
   1.157 + * @param logger the logger to use
   1.158 + * @param ... format string and arguments
   1.159 + * @see ucx_logger_logf()
   1.160 + */
   1.161  #define ucx_logger_info(logger, ...) \
   1.162      ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__)
   1.163 +/**
   1.164 + * Shortcut for logging a warning message.
   1.165 + * @param logger the logger to use
   1.166 + * @param ... format string and arguments
   1.167 + * @see ucx_logger_logf()
   1.168 + */
   1.169  #define ucx_logger_warn(logger, ...) \
   1.170      ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__)
   1.171 +/**
   1.172 + * Shortcut for logging a debug message.
   1.173 + * @param logger the logger to use
   1.174 + * @param ... format string and arguments
   1.175 + * @see ucx_logger_logf()
   1.176 + */
   1.177 +#define ucx_logger_debug(logger, ...) \
   1.178 +    ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__)
   1.179 +/**
   1.180 + * Shortcut for logging a trace message.
   1.181 + * @param logger the logger to use
   1.182 + * @param ... format string and arguments
   1.183 + * @see ucx_logger_logf()
   1.184 + */
   1.185  #define ucx_logger_trace(logger, ...) \
   1.186      ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__)
   1.187  

mercurial