1 /* |
1 /* |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 * |
3 * |
4 * Copyright 2016 Olaf Wintermann. All rights reserved. |
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
5 * |
5 * |
6 * Redistribution and use in source and binary forms, with or without |
6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are met: |
7 * modification, are permitted provided that the following conditions are met: |
8 * |
8 * |
9 * 1. Redistributions of source code must retain the above copyright |
9 * 1. Redistributions of source code must retain the above copyright |
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 #include "logging.h" |
29 #include "ucx/logging.h" |
|
30 |
30 #include <stdlib.h> |
31 #include <stdlib.h> |
31 #include <string.h> |
32 #include <string.h> |
32 #include <stdarg.h> |
33 #include <stdarg.h> |
33 #include <time.h> |
34 #include <time.h> |
34 |
35 |
47 ucx_map_int_put(logger->levels, l, (void*) "[ERROR]"); |
48 ucx_map_int_put(logger->levels, l, (void*) "[ERROR]"); |
48 l = UCX_LOGGER_WARN; |
49 l = UCX_LOGGER_WARN; |
49 ucx_map_int_put(logger->levels, l, (void*) "[WARNING]"); |
50 ucx_map_int_put(logger->levels, l, (void*) "[WARNING]"); |
50 l = UCX_LOGGER_INFO; |
51 l = UCX_LOGGER_INFO; |
51 ucx_map_int_put(logger->levels, l, (void*) "[INFO]"); |
52 ucx_map_int_put(logger->levels, l, (void*) "[INFO]"); |
|
53 l = UCX_LOGGER_DEBUG; |
|
54 ucx_map_int_put(logger->levels, l, (void*) "[DEBUG]"); |
52 l = UCX_LOGGER_TRACE; |
55 l = UCX_LOGGER_TRACE; |
53 ucx_map_int_put(logger->levels, l, (void*) "[TRACE]"); |
56 ucx_map_int_put(logger->levels, l, (void*) "[TRACE]"); |
54 } |
57 } |
55 |
58 |
56 return logger; |
59 return logger; |
66 |
69 |
67 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, |
70 void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, |
68 const unsigned int line, const char *format, ...) { |
71 const unsigned int line, const char *format, ...) { |
69 if (level <= logger->level) { |
72 if (level <= logger->level) { |
70 char msg[UCX_LOGGER_MSGMAX]; |
73 char msg[UCX_LOGGER_MSGMAX]; |
71 char *text; |
74 const char *text; |
72 size_t k = 0; |
75 size_t k = 0; |
73 size_t n; |
76 size_t n; |
74 |
77 |
75 if ((logger->mask & UCX_LOGGER_LEVEL) > 0) { |
78 if ((logger->mask & UCX_LOGGER_LEVEL) > 0) { |
76 text = (char*) ucx_map_int_get(logger->levels, level); |
79 text = (const char*) ucx_map_int_get(logger->levels, level); |
|
80 if (!text) { |
|
81 text = "[UNKNOWN]"; |
|
82 } |
77 n = strlen(text); |
83 n = strlen(text); |
78 n = n > 256 ? 256 : n; |
84 n = n > 256 ? 256 : n; |
79 memcpy(msg+k, text, n); |
85 memcpy(msg+k, text, n); |
80 k += n; |
86 k += n; |
81 msg[k++] = ' '; |
87 msg[k++] = ' '; |
89 memcpy(msg+k, file, n); |
95 memcpy(msg+k, file, n); |
90 k += n; |
96 k += n; |
91 k += sprintf(msg+k, ":%u ", line); |
97 k += sprintf(msg+k, ":%u ", line); |
92 } |
98 } |
93 |
99 |
94 msg[k++] = '-'; msg[k++] = ' '; |
100 if (k > 0) { |
|
101 msg[k++] = '-'; msg[k++] = ' '; |
|
102 } |
95 |
103 |
96 va_list args; |
104 va_list args; |
97 va_start (args, format); |
105 va_start (args, format); |
98 k += vsnprintf(msg+k, UCX_LOGGER_MSGMAX-k-1, format, args); |
106 k += vsnprintf(msg+k, UCX_LOGGER_MSGMAX-k-1, format, args); |
99 va_end (args); |
107 va_end (args); |