ucx/logging.h

changeset 129
61edec666928
parent 120
8170f658f017
child 138
7800811078b8
equal deleted inserted replaced
128:b79b1ce438dd 129:61edec666928
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28 /**
29 * Logging API.
30 *
31 * @file logging.h
32 * @author Mike Becker, Olaf Wintermann
33 */
29 #ifndef UCX_LOGGING_H 34 #ifndef UCX_LOGGING_H
30 #define UCX_LOGGING_H 35 #define UCX_LOGGING_H
31 36
32 #include "ucx.h" 37 #include "ucx.h"
33 #include "map.h" 38 #include "map.h"
37 #ifdef __cplusplus 42 #ifdef __cplusplus
38 extern "C" { 43 extern "C" {
39 #endif 44 #endif
40 45
41 /* leave enough space for custom log levels */ 46 /* leave enough space for custom log levels */
47 /** Log level for error messages. */
42 #define UCX_LOGGER_ERROR 0x00 48 #define UCX_LOGGER_ERROR 0x00
49 /** Log level for warning messages. */
43 #define UCX_LOGGER_WARN 0x10 50 #define UCX_LOGGER_WARN 0x10
51 /** Log level for information messages. */
44 #define UCX_LOGGER_INFO 0x20 52 #define UCX_LOGGER_INFO 0x20
45 #define UCX_LOGGER_TRACE 0x30 53 /** Log level for debug messages. */
46 54 #define UCX_LOGGER_DEBUG 0x30
55 /** Log level for trace messages. */
56 #define UCX_LOGGER_TRACE 0x40
57
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 */
47 #define UCX_LOGGER_LEVEL 0x01 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 */
48 #define UCX_LOGGER_TIMESTAMP 0x02 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 */
49 #define UCX_LOGGER_SOURCE 0x04 76 #define UCX_LOGGER_SOURCE 0x04
50 77
78 /**
79 * The UCX Logger object.
80 */
51 typedef struct { 81 typedef struct {
82 /** The stream this logger writes its messages to.*/
52 void *stream; 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 */
53 write_func writer; 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 */
54 char *dateformat; 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 */
55 unsigned int level; 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 */
56 unsigned int mask; 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 */
57 UcxMap* levels; 120 UcxMap* levels;
58 } UcxLogger; 121 } UcxLogger;
59 122
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 */
60 UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask); 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 */
61 void ucx_logger_free(UcxLogger* logger); 138 void ucx_logger_free(UcxLogger* logger);
62 139
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 * @param logger the logger to use
152 * @param level the level to log on
153 * @param file information about the source file
154 * @param line information about the source line number
155 * @param format format string
156 * @param ... arguments
157 * @see ucx_logger_log()
158 */
63 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, 159 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
64 const unsigned int line, const char* format, ...); 160 const unsigned int line, const char* format, ...);
161
162 /**
163 * Logs a message at the specified level.
164 * @param logger the logger to use
165 * @param level the level to log the message on
166 * @param ... format string and arguments
167 * @see ucx_logger_logf()
168 */
65 #define ucx_logger_log(logger, level, ...) \ 169 #define ucx_logger_log(logger, level, ...) \
66 ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__) 170 ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__)
171
172 /**
173 * Shortcut for logging an error message.
174 * @param logger the logger to use
175 * @param ... format string and arguments
176 * @see ucx_logger_logf()
177 */
67 #define ucx_logger_error(logger, ...) \ 178 #define ucx_logger_error(logger, ...) \
68 ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__) 179 ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__)
180 /**
181 * Shortcut for logging an information message.
182 * @param logger the logger to use
183 * @param ... format string and arguments
184 * @see ucx_logger_logf()
185 */
69 #define ucx_logger_info(logger, ...) \ 186 #define ucx_logger_info(logger, ...) \
70 ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__) 187 ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__)
188 /**
189 * Shortcut for logging a warning message.
190 * @param logger the logger to use
191 * @param ... format string and arguments
192 * @see ucx_logger_logf()
193 */
71 #define ucx_logger_warn(logger, ...) \ 194 #define ucx_logger_warn(logger, ...) \
72 ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__) 195 ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__)
196 /**
197 * Shortcut for logging a debug message.
198 * @param logger the logger to use
199 * @param ... format string and arguments
200 * @see ucx_logger_logf()
201 */
202 #define ucx_logger_debug(logger, ...) \
203 ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__)
204 /**
205 * Shortcut for logging a trace message.
206 * @param logger the logger to use
207 * @param ... format string and arguments
208 * @see ucx_logger_logf()
209 */
73 #define ucx_logger_trace(logger, ...) \ 210 #define ucx_logger_trace(logger, ...) \
74 ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__) 211 ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__)
75 212
76 #ifdef __cplusplus 213 #ifdef __cplusplus
77 } 214 }

mercurial