Fri, 08 Feb 2013 10:37:24 +0100
changed logger to behave more like printf + added possibility to specify write function
olaf@57 | 1 | #ifndef LOGGING_H |
olaf@57 | 2 | #define LOGGING_H |
olaf@57 | 3 | |
universe@69 | 4 | #include "ucx.h" |
universe@80 | 5 | #include "map.h" |
olaf@57 | 6 | #include "string.h" |
olaf@57 | 7 | #include <stdio.h> |
olaf@57 | 8 | |
olaf@57 | 9 | #ifdef __cplusplus |
olaf@57 | 10 | extern "C" { |
olaf@57 | 11 | #endif |
olaf@57 | 12 | |
olaf@57 | 13 | /* leave enough space for custom log levels */ |
universe@80 | 14 | #define UCX_LOGGER_ERROR 0x00 |
universe@80 | 15 | #define UCX_LOGGER_WARN 0x10 |
universe@80 | 16 | #define UCX_LOGGER_INFO 0x20 |
universe@80 | 17 | #define UCX_LOGGER_TRACE 0x30 |
universe@80 | 18 | |
universe@80 | 19 | #define UCX_LOGGER_LEVEL 0x01 |
universe@80 | 20 | #define UCX_LOGGER_TIMESTAMP 0x02 |
universe@80 | 21 | #define UCX_LOGGER_SOURCE 0x04 |
olaf@57 | 22 | |
olaf@57 | 23 | typedef struct { |
olaf@57 | 24 | FILE *stream; |
universe@81 | 25 | size_t(*writer)(const void*, size_t, size_t, FILE*); |
olaf@57 | 26 | unsigned int level; |
universe@80 | 27 | unsigned int mask; |
universe@80 | 28 | UcxMap* levels; |
olaf@57 | 29 | } UcxLogger; |
olaf@57 | 30 | |
universe@80 | 31 | UcxLogger *ucx_logger_new(FILE *stream, unsigned int level, unsigned int mask); |
universe@80 | 32 | void ucx_logger_free(UcxLogger* logger); |
olaf@57 | 33 | |
universe@81 | 34 | /** |
universe@81 | 35 | * Sets the output stream and writer for this logger. |
universe@81 | 36 | * The parameters stream and writer may be NULL, if they shall remain unchanged |
universe@81 | 37 | * in the logger. |
universe@81 | 38 | * l |
universe@81 | 39 | * @param logger The logger |
universe@81 | 40 | * @param stream The stream the logger shall log to |
universe@81 | 41 | * @param writer A pointer to the write function |
universe@81 | 42 | */ |
universe@81 | 43 | void ucx_logger_setoutput(UcxLogger *logger, FILE *stream, |
universe@81 | 44 | size_t(*writer)(const void*,size_t,size_t,FILE*)); |
universe@81 | 45 | |
universe@81 | 46 | void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, |
universe@81 | 47 | const unsigned int line, const char* format, ...); |
universe@81 | 48 | #define ucx_logger_log(logger, level, format...) \ |
universe@81 | 49 | ucx_logger_logf(logger, level, __FILE__, __LINE__, format) |
universe@81 | 50 | #define ucx_logger_error(logger,format...) \ |
universe@81 | 51 | ucx_logger_log(logger, UCX_LOGGER_ERROR, format) |
universe@81 | 52 | #define ucx_logger_info(logger,format...) \ |
universe@81 | 53 | ucx_logger_log(logger, UCX_LOGGER_INFO, format) |
universe@81 | 54 | #define ucx_logger_warn(logger,format...) \ |
universe@81 | 55 | ucx_logger_log(logger, UCX_LOGGER_WARN, format) |
universe@81 | 56 | #define ucx_logger_trace(logger,format...) \ |
universe@81 | 57 | ucx_logger_log(logger, UCX_LOGGER_TRACE, format) |
olaf@57 | 58 | |
olaf@57 | 59 | #ifdef __cplusplus |
olaf@57 | 60 | } |
olaf@57 | 61 | #endif |
olaf@57 | 62 | |
olaf@57 | 63 | #endif /* LOGGING_H */ |