-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlcl.m
188 lines (169 loc) · 6.05 KB
/
lcl.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
//
// lcl.m -- LibComponentLogging
//
//
// Copyright (c) 2008-2015 Arne Harren <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "lcl.h"
#include <string.h>
// Active log levels, indexed by log component.
_lcl_level_narrow_t _lcl_component_level[_lcl_component_t_count];
// Log component identifiers, indexed by log component.
const char * const _lcl_component_identifier[_lcl_component_t_count] = {
# define _lcl_component(_identifier, _header, _name) \
#_identifier,
#ifdef __lcl_use_config_include_lcl_config_components_h
# include "lcl_config_components.h"
#else
_lcl_component(Main, "main", "Main")
#endif
# undef _lcl_component
};
// Log component headers, indexed by log component.
const char * const _lcl_component_header[_lcl_component_t_count] = {
# define _lcl_component(_identifier, _header, _name) \
_header,
#ifdef __lcl_use_config_include_lcl_config_components_h
# include "lcl_config_components.h"
#else
_lcl_component(Main, "main", "Main")
#endif
# undef _lcl_component
};
// Log component names, indexed by log component.
const char * const _lcl_component_name[_lcl_component_t_count] = {
# define _lcl_component(_identifier, _header, _name) \
_name,
#ifdef __lcl_use_config_include_lcl_config_components_h
# include "lcl_config_components.h"
#else
_lcl_component(Main, "main", "Main")
#endif
# undef _lcl_component
};
// Log level headers, indexed by log level.
const char * const _lcl_level_header[_lcl_level_t_count] = {
"-",
"CRITICAL",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"TRACE"
};
const char * const _lcl_level_header_1[_lcl_level_t_count] = {
"-",
"C",
"E",
"W",
"I",
"D",
"T"
};
const char * const _lcl_level_header_3[_lcl_level_t_count] = {
"---",
"CRI",
"ERR",
"WRN",
"INF",
"DBG",
"TRC"
};
// Log level names, indexed by log level.
const char * const _lcl_level_name[_lcl_level_t_count] = {
"Off",
"Critical",
"Error",
"Warning",
"Info",
"Debug",
"Trace"
};
// Version.
#define __lcl_version_to_string( _text) __lcl_version_to_string0(_text)
#define __lcl_version_to_string0(_text) #_text
const char * const _lcl_version = __lcl_version_to_string(_LCL_VERSION_MAJOR)
"." __lcl_version_to_string(_LCL_VERSION_MINOR)
"." __lcl_version_to_string(_LCL_VERSION_BUILD)
"" _LCL_VERSION_SUFFIX;
// Configures the given log level for the given log component.
uint32_t lcl_configure_by_component(_lcl_component_t component, _lcl_level_t level) {
// unsupported level, clip to last level
if (level > _lcl_level_t_last) {
level = _lcl_level_t_last;
}
// configure the component
if (component <= _lcl_component_t_last) {
_lcl_component_level[component] = level;
return 1;
}
return 0;
}
// Configures the given log level for the given log component(s).
static uint32_t _lcl_configure_by_text(uint32_t count, const char * const *texts,
_lcl_level_narrow_t *levels, const char *text,
_lcl_level_t level) {
// no text given, quit
if (text == NULL || text[0] == '\0') {
return 0;
}
// unsupported level, clip to last level
if (level > _lcl_level_t_last) {
level = _lcl_level_t_last;
}
// configure the components
uint32_t num_configured = 0;
size_t text_len = strlen(text);
if (text[text_len-1] == '*') {
// text ends with '*', wildcard suffix was specified
text_len--;
for (uint32_t c = 0; c < count; c++) {
if (strncmp(text, texts[c], text_len) == 0) {
levels[c] = level;
num_configured++;
}
}
} else {
// no wildcard suffix was specified
for (uint32_t c = 0; c < count; c++) {
if (strcmp(text, texts[c]) == 0) {
levels[c] = level;
num_configured++;
}
}
}
return num_configured;
}
// Configures the given log level for the given log component(s) by identifier.
uint32_t lcl_configure_by_identifier(const char *identifier, _lcl_level_t level) {
return _lcl_configure_by_text(_lcl_component_t_count, _lcl_component_identifier,
_lcl_component_level, identifier, level);
}
// Configures the given log level for the given log component(s) by header.
uint32_t lcl_configure_by_header(const char *header, _lcl_level_t level) {
return _lcl_configure_by_text(_lcl_component_t_count, _lcl_component_header,
_lcl_component_level, header, level);
}
// Configures the given log level for the given log component(s) by name.
uint32_t lcl_configure_by_name(const char *name, _lcl_level_t level) {
return _lcl_configure_by_text(_lcl_component_t_count, _lcl_component_name,
_lcl_component_level, name, level);
}