Debian Codegraph @

src/utils/davix_logger.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2013
4 * Author: Adrien Devresse <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include <vector>
23 #include <cstdio>
24 #include <cstdarg>
25 #include <davix_internal.hpp>
26 #include <utils/davix_logger.hpp>
27 #include <utils/stringutils.hpp>
28
29 // This bit of code allows one to set the debug level via an envar when
30 // the Davix library is being used via a plugin.
31 //
32 namespace
33 {
34 int getTraceValue()
35 {
36 const char *dbg = getenv("DAVIX_DEBUG");
37 if (dbg)
38 {char dbgval = tolower(*dbg);
39 if (dbgval == 'c') return DAVIX_LOG_CRITICAL;
40 if (dbgval == 'w') return DAVIX_LOG_WARNING;
41 if (dbgval == 'v') return DAVIX_LOG_VERBOSE;
42 if (dbgval == 'd') return DAVIX_LOG_DEBUG;
43 if (dbgval == 't') return DAVIX_LOG_TRACE;
44 if (dbgval == 'a') return DAVIX_LOG_ALL;
45 }
46 return 0;
47 }
48 }
49
50 #ifdef HAVE_ATOMIC
51 #include <atomic>
52 static std::atomic<int> internal_log_level(getTraceValue());
53 static std::atomic<int> internal_log_scope(DAVIX_LOG_SCOPE_ALL);
54 #else
55 #warning "Setting / getting davix loglevel is not thread-safe!"
56 int internal_log_level = getTraceValue();
57 int internal_log_scope = DAVIX_LOG_SCOPE_ALL;
58 #endif
59
60 const int BUFFER_SIZE = 8192;
61 const char* prefix = "DAVIX";
62
63 static void (*_fhandler)(void* userdata, int mgs_level, const char* msg) = NULL;
64 static void* _log_handler_userdata = NULL;
65
66
67
68
69
70
71 namespace Davix{
72
73 const char* SCOPE_FILE = "file";
74 const char* SCOPE_HTTP = "http";
75 const char* SCOPE_POSIX = "posix";
76 const char* SCOPE_XML = "xml";
77 const char* SCOPE_SSL = "ssl";
78 const char* SCOPE_HEADER = "header";
79 const char* SCOPE_BODY = "body";
80 const char* SCOPE_CHAIN = "chain";
81 const char* SCOPE_CORE = "core";
82 const char* SCOPE_GRID = "grid";
83 const char* SCOPE_SOCKET = "socket";
84 const char* SCOPE_LOCKS = "locks";
85 const char* SCOPE_S3 = "s3";
86 const char* SCOPE_SENSITIVE = "sensitive";
87 const char* SCOPE_ALL = "all";
88
89
90 const std::string davix_log_scope[] = {
91 SCOPE_FILE,
92 SCOPE_HTTP,
93 SCOPE_POSIX,
94 SCOPE_XML,
95 SCOPE_SSL,
96 SCOPE_HEADER,
97 SCOPE_BODY,
98 SCOPE_CHAIN,
99 SCOPE_CORE,
100 SCOPE_GRID,
101 SCOPE_SOCKET,
102 SCOPE_LOCKS,
103 SCOPE_S3,
104 SCOPE_SENSITIVE,
105 SCOPE_ALL
106 };
107
108 const int num_of_scopes = sizeof(davix_log_scope)/sizeof(*davix_log_scope);
109
110
111 int getLogLevel(){
112 return internal_log_level;
113 }
114
115 void setLogLevel(int logLevel){
116 internal_log_level = logLevel;
117 }
118
119 int getLogScope(){
120 return internal_log_scope;
121 }
122
123 void setLogScope(int log_scope){
124 internal_log_scope = log_scope;
125 }
126
127 void setLogScope(const std::string &scope){
128 int mask=0;
129 std::vector<std::string> vec_scopes;
130 StrUtil::split( scope, ',', vec_scopes);
131
132 for(std::vector<std::string>::iterator it = vec_scopes.begin(); it < vec_scopes.end(); ++it){
133 for(int i = 0; i < num_of_scopes; ++i){
134 if(StrUtil::compare_ncase((*it), davix_log_scope[i]) == 0){
135 switch(i){
136 case 0:
137 mask |= DAVIX_LOG_FILE;
138 break;
139 case 1:
140 mask |= DAVIX_LOG_HTTP;
141 break;
142 case 2:
143 mask |= DAVIX_LOG_POSIX;
144 break;
145 case 3:
146 mask |= DAVIX_LOG_XML;
147 break;
148 case 4:
149 mask |= DAVIX_LOG_SSL;
150 break;
151 case 5:
152 mask |= DAVIX_LOG_HEADER;
153 break;
154 case 6:
155 mask |= DAVIX_LOG_BODY;
156 break;
157 case 7:
158 mask |= DAVIX_LOG_CHAIN;
159 break;
160 case 8:
161 mask |= DAVIX_LOG_CORE;
162 break;
163 case 9:
164 mask |= DAVIX_LOG_GRID;
165 break;
166 case 10:
167 mask |= DAVIX_LOG_SOCKET;
168 break;
169 case 11:
170 mask |= DAVIX_LOG_LOCKS;
171 break;
172 case 12:
173 mask |= DAVIX_LOG_S3;
174 break;
175 case 13:
176 mask |= DAVIX_LOG_SENSITIVE;
177 break;
178 case 14:
179 mask |= DAVIX_LOG_SCOPE_ALL;
180 break;
181 default:
182 break;
183 }
184 }
185 }
186 }
187 setLogScope(mask);
188 }
189
190
191
192 void logStr(int scope, int log_level, const std::string & str){
193 if(_fhandler){
194 _fhandler(_log_handler_userdata, log_level, str.c_str());
195 }else{
196 if(scope & DAVIX_LOG_HEADER){ // log header, we do not want headers to be prefixed
197 fmt::print(stderr, "{}\n", str);
198 }else{ // davix logs
199 fmt::print(stderr,"{}({}): {}\n", prefix, getScopeName(scope), str);
200 }
201 }
202 }
203
204
205
206 std::string getScopeName(int scope_mask){
207 std::string scope_name;
208 switch(scope_mask){
209 case DAVIX_LOG_FILE:
210 scope_name = SCOPE_FILE;
211 break;
212 case DAVIX_LOG_HTTP:
213 scope_name = SCOPE_HTTP;
214 break;
215 case DAVIX_LOG_POSIX:
216 scope_name = SCOPE_POSIX;
217 break;
218 case DAVIX_LOG_XML:
219 scope_name = SCOPE_XML;
220 break;
221 case DAVIX_LOG_SSL:
222 scope_name = SCOPE_SSL;
223 break;
224 case DAVIX_LOG_HEADER:
225 scope_name = SCOPE_HEADER;
226 break;
227 case DAVIX_LOG_BODY:
228 scope_name = SCOPE_BODY;
229 break;
230 case DAVIX_LOG_CHAIN:
231 scope_name = SCOPE_CHAIN;
232 break;
233 case DAVIX_LOG_CORE:
234 scope_name = SCOPE_CORE;
235 break;
236 case DAVIX_LOG_GRID:
237 scope_name = SCOPE_GRID;
238 break;
239 case DAVIX_LOG_SOCKET:
240 scope_name = SCOPE_SOCKET;
241 break;
242 case DAVIX_LOG_LOCKS:
243 scope_name = SCOPE_LOCKS;
244 break;
245 case DAVIX_LOG_S3:
246 scope_name = SCOPE_S3;
247 break;
248 default:
249 scope_name = "Unknown";
250 break;
251 }
252 return scope_name;
253 }
254
255
256
257
258
259
260 } // Davix
261
262
263
264 //
265 // Old legacy API
266 //
267
268
269 extern "C" void davix_set_log_level(int log_mask){
270 internal_log_level = log_mask;
271 }
272
273 extern "C" int davix_get_log_level(){
274 return internal_log_level;
275 }
276
277
278 extern "C" int davix_get_log_scope(){
279 return internal_log_scope;
280 }
281
282
283 extern "C" void davix_vlogger2(int log_mask, int log_level, const char* msg, va_list ap){
284 char buffer[BUFFER_SIZE];
285
286 vsnprintf(buffer, BUFFER_SIZE-1, msg, ap);
287 buffer[BUFFER_SIZE-1] ='\0';
288 Davix::logStr(log_mask, log_level, buffer);
289 }
290
291 extern "C" void davix_vlogger(int log_mask, const char* msg, va_list va){
292 davix_vlogger2(log_mask, DAVIX_LOG_DEBUG, msg, va);
293 }
294
295
296
297 extern "C" void davix_logger(int log_mask, const char * msg, ...){
298 va_list va;
299 va_start(va, msg);
300 davix_vlogger(log_mask, msg, va);
301 va_end(va);
302 }
303
304
305 extern "C" void davix_set_log_handler( void (*fhandler)(void* userdata, int mgs_level, const char* msg), void* userdata){
306 _fhandler = fhandler;
307 _log_handler_userdata = userdata;
308 }