ucx
UAP Common Extensions
logging.h
Go to the documentation of this file.
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2017 Mike Becker, 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  */
34 #ifndef UCX_LOGGING_H
35 #define UCX_LOGGING_H
36 
37 #include "ucx.h"
38 #include "map.h"
39 #include "string.h"
40 #include <stdio.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /* leave enough space for custom log levels */
47 
49 #define UCX_LOGGER_ERROR 0x00
50 
52 #define UCX_LOGGER_WARN 0x10
53 
55 #define UCX_LOGGER_INFO 0x20
56 
58 #define UCX_LOGGER_DEBUG 0x30
59 
61 #define UCX_LOGGER_TRACE 0x40
62 
68 #define UCX_LOGGER_LEVEL 0x01
69 
75 #define UCX_LOGGER_TIMESTAMP 0x02
76 
83 #define UCX_LOGGER_SOURCE 0x04
84 
88 typedef struct {
90  void *stream;
91 
98 
104  char *dateformat;
105 
111  unsigned int level;
112 
119  unsigned int mask;
120 
133 } UcxLogger;
134 
142 UcxLogger *ucx_logger_new(void *stream, unsigned int level, unsigned int mask);
143 
151 void ucx_logger_free(UcxLogger* logger);
152 
176 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file,
177  const unsigned int line, const char* format, ...);
178 
185 #define ucx_logger_register_level(logger, level, name) {\
186  unsigned int l; \
187  l = level; \
188  ucx_map_int_put(logger->levels, l, (void*) "[" name "]"); \
189  } while (0);
190 
198 #define ucx_logger_log(logger, level, ...) \
199  ucx_logger_logf(logger, level, __FILE__, __LINE__, __VA_ARGS__)
200 
207 #define ucx_logger_error(logger, ...) \
208  ucx_logger_log(logger, UCX_LOGGER_ERROR, __VA_ARGS__)
209 
216 #define ucx_logger_info(logger, ...) \
217  ucx_logger_log(logger, UCX_LOGGER_INFO, __VA_ARGS__)
218 
225 #define ucx_logger_warn(logger, ...) \
226  ucx_logger_log(logger, UCX_LOGGER_WARN, __VA_ARGS__)
227 
234 #define ucx_logger_debug(logger, ...) \
235  ucx_logger_log(logger, UCX_LOGGER_DEBUG, __VA_ARGS__)
236 
243 #define ucx_logger_trace(logger, ...) \
244  ucx_logger_log(logger, UCX_LOGGER_TRACE, __VA_ARGS__)
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif /* UCX_LOGGING_H */
write_func writer
The write function that shall be used.
Definition: logging.h:97
Bounded string implementation.
Main UCX Header providing most common definitions.
The UCX Logger object.
Definition: logging.h:88
char * dateformat
The date format for timestamp outputs including the delimiter (default: "%F %T %z ")...
Definition: logging.h:104
unsigned int mask
A configuration mask for automatic output.
Definition: logging.h:119
UcxLogger * ucx_logger_new(void *stream, unsigned int level, unsigned int mask)
Creates a new logger.
Definition: logging.c:36
void * stream
The stream this logger writes its messages to.
Definition: logging.h:90
Structure for the UCX map.
Definition: map.h:81
Hash map implementation.
size_t(* write_func)(const void *, size_t, size_t, void *)
Function pointer to a write function.
Definition: ucx.h:119
UcxMap * levels
A map of valid log levels for this logger.
Definition: logging.h:132
unsigned int level
The level, this logger operates on.
Definition: logging.h:111
void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char *file, const unsigned int line, const char *format,...)
Internal log function - use macros instead.
Definition: logging.c:70
void ucx_logger_free(UcxLogger *logger)
Destroys the logger.
Definition: logging.c:62