Line data Source code
1 : // $Id$
2 :
3 : ///**************************************************************************
4 : ///* This file is property of and copyright by the *
5 : ///* ALICE Experiment at CERN, All rights reserved. *
6 : ///* *
7 : ///* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 : ///* for The ALICE HLT Project. *
9 : ///* *
10 : ///* Permission to use, copy, modify and distribute this software and its *
11 : ///* documentation strictly for non-commercial purposes is hereby granted *
12 : ///* without fee, provided that the above copyright notice appears in all *
13 : ///* copies and that both the copyright notice and this permission notice *
14 : ///* appear in the supporting documentation. The authors make no claims *
15 : ///* about the suitability of this software for any purpose. It is *
16 : ///* provided "as is" without express or implied warranty. *
17 : ///**************************************************************************
18 :
19 : /// @file AliHLTLogging.cxx
20 : /// @author Matthias Richter, Timm Steinbeck
21 : /// @date
22 : /// @brief Implementation of HLT logging primitives.
23 : ///
24 :
25 : #include "AliHLTStdIncludes.h"
26 : #include "AliHLTLogging.h"
27 : #include "AliHLTComponentHandler.h"
28 : #include "TString.h"
29 : #include "Varargs.h"
30 : #include <string>
31 : #include <sstream>
32 : #include <iostream>
33 :
34 : using std::cout;
35 : using std::cerr;
36 : using std::endl;
37 :
38 : /** ROOT macro for the implementation of ROOT specific class methods */
39 126 : ClassImp(AliHLTLogging);
40 :
41 : AliHLTLogging::AliHLTLogging()
42 : :
43 617 : fLocalLogFilter(fgLocalLogDefault),
44 617 : fpDefaultKeyword(NULL),
45 617 : fpCurrentKeyword(NULL)
46 1580 : {
47 : // see header file for class documentation
48 : // or
49 : // refer to README to build package
50 : // or
51 : // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
52 790 : }
53 :
54 : AliHLTLogging::AliHLTLogging(const AliHLTLogging&)
55 : :
56 0 : fLocalLogFilter(kHLTLogAll),
57 0 : fpDefaultKeyword(NULL),
58 0 : fpCurrentKeyword(NULL)
59 0 : {
60 : // see header file for class documentation
61 0 : HLTFatal("copy constructor untested");
62 0 : }
63 :
64 : AliHLTLogging& AliHLTLogging::operator=(const AliHLTLogging&)
65 : {
66 : // see header file for class documentation
67 0 : HLTFatal("assignment operator untested");
68 0 : return *this;
69 : }
70 :
71 126 : ostringstream AliHLTLogging::fgLogstr;
72 : AliHLTComponentLogSeverity AliHLTLogging::fgGlobalLogFilter=kHLTLogAll;
73 : AliHLTComponentLogSeverity AliHLTLogging::fgLocalLogDefault=kHLTLogAll;
74 : AliHLTfctLogging AliHLTLogging::fgLoggingFunc=NULL;
75 : AliHLTLogging::AliHLTDynamicMessage AliHLTLogging::fgAliLoggingFunc=NULL;
76 : int AliHLTLogging::fgUseAliLog=1;
77 :
78 126 : TString AliHLTLogging::fgBlackList="";
79 126 : TString AliHLTLogging::fgWhiteList="";
80 :
81 : AliHLTLogging::~AliHLTLogging()
82 346 : {
83 : // see header file for class documentation
84 1389 : }
85 :
86 : // the array will be grown dynamically, this is just an initial size
87 126 : TArrayC AliHLTLogging::fgAliHLTLoggingTarget(200);
88 : // the maximum size of the array
89 : const int AliHLTLogging::fgkALIHLTLOGGINGMAXBUFFERSIZE=10000;
90 :
91 : int AliHLTLogging::Init(AliHLTfctLogging pFun)
92 : {
93 : // see header file for class documentation
94 0 : if (fgLoggingFunc!=NULL && fgLoggingFunc!=pFun) {
95 0 : (*fgLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");
96 0 : }
97 0 : fgLoggingFunc=pFun;
98 :
99 0 : return 0;
100 : }
101 :
102 : int AliHLTLogging::InitAliLogTrap(AliHLTComponentHandler* pHandler)
103 : {
104 : // see header file for class documentation
105 : // init the AliRoot logging trap
106 : // AliLog messages are redirected to PubSub,
107 : int iResult=0;
108 0 : if (pHandler) {
109 : // set temporary loglevel of component handler
110 0 : AliHLTComponentLogSeverity loglevel=pHandler->GetLocalLoggingLevel();
111 0 : pHandler->SetLocalLoggingLevel(kHLTLogError);
112 :
113 : // load library containing AliRoot dependencies and initialization handler
114 0 : pHandler->LoadLibrary(ALILOG_WRAPPER_LIBRARY, 0/* do not activate agents */);
115 :
116 : // restore loglevel
117 0 : pHandler->SetLocalLoggingLevel(loglevel);
118 :
119 : // find the symbol
120 0 : InitAliDynamicMessageCallback pFunc=(InitAliDynamicMessageCallback)pHandler->FindSymbol(ALILOG_WRAPPER_LIBRARY, "InitAliDynamicMessageCallback");
121 0 : if (pFunc) {
122 0 : iResult=(*pFunc)();
123 0 : } else {
124 0 : Message(NULL, kHLTLogError, "AliHLTLogging::InitAliLogTrap", "init logging",
125 : "can not initialize AliLog callback");
126 : iResult=-ENOSYS;
127 : }
128 0 : } else {
129 : iResult=-EINVAL;
130 : }
131 :
132 0 : return iResult;
133 : }
134 :
135 : int AliHLTLogging::InitAliLogFunc(AliHLTComponentHandler* pHandler)
136 : {
137 : // see header file for class documentation
138 : int iResult=0;
139 6 : if (pHandler) {
140 : // set temporary loglevel of component handler
141 3 : AliHLTComponentLogSeverity loglevel=pHandler->GetLocalLoggingLevel();
142 3 : pHandler->SetLocalLoggingLevel(kHLTLogError);
143 :
144 : // load library containing AliRoot dependencies and initialization handler
145 3 : pHandler->LoadLibrary(ALILOG_WRAPPER_LIBRARY, 0/* do not activate agents */);
146 :
147 : // restore loglevel
148 3 : pHandler->SetLocalLoggingLevel(loglevel);
149 :
150 : // find the symbol
151 3 : fgAliLoggingFunc=(AliHLTLogging::AliHLTDynamicMessage)pHandler->FindSymbol(ALILOG_WRAPPER_LIBRARY, "AliDynamicMessage");
152 3 : if (fgAliLoggingFunc==NULL) {
153 0 : Message(NULL, kHLTLogError, "AliHLTLogging::InitAliLogFunc", "init logging",
154 : "symbol lookup failure: can not find AliDynamicMessage, switching to HLT logging system");
155 : iResult=-ENOSYS;
156 0 : }
157 3 : } else {
158 : iResult=-EINVAL;
159 : }
160 :
161 3 : return iResult;
162 : }
163 :
164 : int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity,
165 : const char* origin, const char* keyword,
166 : const char* message)
167 : {
168 : // see header file for class documentation
169 : int iResult=0;
170 : if (param==NULL) {
171 : // this is currently just to get rid of the warning "unused parameter"
172 : }
173 :
174 : const char* strSeverity="";
175 0 : switch (severity) {
176 : case kHLTLogBenchmark:
177 : strSeverity="benchmark";
178 0 : break;
179 : case kHLTLogDebug:
180 : strSeverity="debug";
181 0 : break;
182 : case kHLTLogInfo:
183 : strSeverity="info";
184 0 : break;
185 : case kHLTLogWarning:
186 : strSeverity="warning";
187 0 : break;
188 : case kHLTLogError:
189 : strSeverity="error";
190 0 : break;
191 : case kHLTLogFatal:
192 : strSeverity="fatal";
193 0 : break;
194 : case kHLTLogImportant:
195 : strSeverity="notify";
196 0 : break;
197 : default:
198 : break;
199 : }
200 0 : TString out="HLT Log ";
201 0 : out+=strSeverity;
202 0 : if (origin && origin[0]!=0) {out+=": <"; out+=origin; out+="> ";}
203 0 : out+=" "; out+=message;
204 0 : if (keyword!=NULL && strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0) {
205 0 : out+=" ("; out+=keyword; out +=")";
206 : }
207 0 : cout << out.Data() << endl;
208 : return iResult;
209 0 : }
210 :
211 : #if 0
212 : int AliHLTLogging::AliMessage(AliHLTComponentLogSeverity severity,
213 : const char* originClass, const char* originFunc,
214 : const char* file, int line, const char* message)
215 : {
216 : // see header file for class documentation
217 :
218 : switch (severity) {
219 : case kHLTLogBenchmark:
220 : AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
221 : break;
222 : case kHLTLogDebug:
223 : AliLog::Message(AliLog::kDebug, message, "HLT", originClass, originFunc, file, line);
224 : break;
225 : case kHLTLogInfo:
226 : AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
227 : break;
228 : case kHLTLogWarning:
229 : AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
230 : break;
231 : case kHLTLogError:
232 : AliLog::Message(AliLog::kError, message, "HLT", originClass, originFunc, file, line);
233 : break;
234 : case kHLTLogFatal:
235 : AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
236 : break;
237 : default:
238 : break;
239 : }
240 : return 0;
241 : }
242 : #endif
243 :
244 : const char* AliHLTLogging::BuildLogString(const char *format, va_list &ap, bool bAppend)
245 : {
246 : // see header file for class documentation
247 :
248 : int iResult=0;
249 : #ifdef R__VA_COPY
250 42 : va_list bap;
251 42 : R__VA_COPY(bap, ap);
252 : #endif //R__VA_COPY
253 :
254 : // take the first argument from the list as format string if no
255 : // format was given
256 : const char* fmt = format;
257 168 : if (fmt==NULL) fmt=va_arg(ap, const char*);
258 :
259 : unsigned int iOffset=0;
260 42 : if (bAppend) {
261 0 : iOffset=strlen(fgAliHLTLoggingTarget.GetArray());
262 0 : } else {
263 42 : fgAliHLTLoggingTarget[0]=0;
264 : }
265 42 : while (fmt!=NULL) {
266 42 : iResult=vsnprintf(fgAliHLTLoggingTarget.GetArray()+iOffset, fgAliHLTLoggingTarget.GetSize()-iOffset, fmt, ap);
267 42 : if (iResult==-1)
268 : // for compatibility with older version of vsnprintf
269 0 : iResult=fgAliHLTLoggingTarget.GetSize()*2;
270 : else
271 42 : iResult+=iOffset;
272 :
273 42 : if (iResult<fgAliHLTLoggingTarget.GetSize())
274 : // everything in the limit
275 : break;
276 :
277 : // terminate if buffer is already at the limit
278 0 : if (fgAliHLTLoggingTarget.GetSize()>=fgkALIHLTLOGGINGMAXBUFFERSIZE)
279 : {
280 0 : fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
281 0 : break;
282 : }
283 :
284 : // check limitation and grow the buffer
285 0 : if (iResult>fgkALIHLTLOGGINGMAXBUFFERSIZE) iResult=fgkALIHLTLOGGINGMAXBUFFERSIZE;
286 0 : fgAliHLTLoggingTarget.Set(iResult+1);
287 :
288 : // copy the original list and skip the first argument if this was the format string
289 : #ifdef R__VA_COPY
290 0 : va_end(ap);
291 0 : R__VA_COPY(ap, bap);
292 : #else
293 : fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
294 : break;
295 : #endif //R__VA_COPY
296 0 : if (format==NULL) va_arg(ap, const char*);
297 : }
298 : #ifdef R__VA_COPY
299 42 : va_end(bap);
300 : #endif //R__VA_COPY
301 :
302 84 : return fgAliHLTLoggingTarget.GetArray();
303 42 : }
304 :
305 : const char* AliHLTLogging::SetLogString(const void* p, const char* pfmt, const char *format, ...)
306 : {
307 : // see header file for class documentation
308 0 : if (!p || !pfmt) return NULL;
309 0 : TString formatstr=format;
310 0 : TString pstr;
311 : #ifdef __DEBUG
312 : pstr.Form(pfmt, p);
313 : #endif
314 0 : formatstr.ReplaceAll("_pfmt_", pstr);
315 0 : va_list args;
316 0 : va_start(args, format);
317 :
318 0 : const char* message=BuildLogString(formatstr.Data(), args);
319 0 : va_end(args);
320 :
321 : return message;
322 0 : }
323 :
324 : int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
325 : const char* origin, const char* keyword,
326 : const char* format, ... )
327 : {
328 : // see header file for class documentation
329 0 : int iResult=CheckFilter(severity);
330 0 : if (iResult>0) {
331 0 : va_list args;
332 0 : va_start(args, format);
333 0 : if (fgLoggingFunc) {
334 0 : iResult = (*fgLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
335 0 : } else {
336 0 : if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
337 0 : iResult=(*fgAliLoggingFunc)(severity, NULL, origin, NULL, 0, AliHLTLogging::BuildLogString(format, args ));
338 : else
339 0 : iResult=Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
340 : }
341 0 : va_end(args);
342 0 : }
343 0 : return iResult;
344 : }
345 :
346 : int AliHLTLogging::LoggingVarargs(AliHLTComponentLogSeverity severity,
347 : const char* originClass, const char* originFunc,
348 : const char* file, int line, ... ) const
349 : {
350 : // see header file for class documentation
351 : int iResult=0;
352 :
353 78 : va_list args;
354 39 : va_start(args, line);
355 :
356 39 : iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args ));
357 39 : va_end(args);
358 :
359 39 : return iResult;
360 39 : }
361 :
362 : int AliHLTLogging::SendMessage(AliHLTComponentLogSeverity severity,
363 : const char* originClass, const char* originFunc,
364 : const char* file, int line,
365 : const char* message) const
366 : {
367 : // see header file for class documentation
368 : int iResult=0;
369 : const char* separator="";
370 84 : TString origin;
371 42 : if (originClass) {
372 42 : origin+=originClass;
373 : separator="::";
374 42 : }
375 42 : if (originFunc) {
376 42 : origin+=separator;
377 42 : origin+=originFunc;
378 : }
379 :
380 42 : if (fgLoggingFunc) {
381 0 : iResult=(*fgLoggingFunc)(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), message);
382 0 : } else {
383 42 : if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
384 84 : iResult=(*fgAliLoggingFunc)(severity, originClass, originFunc, file, line, message);
385 : else
386 0 : iResult=Message(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), message);
387 : }
388 : return iResult;
389 42 : }
390 :
391 : int AliHLTLogging::CheckFilter(AliHLTComponentLogSeverity severity) const
392 : {
393 : // see header file for class documentation
394 :
395 1605 : int iResult=severity==kHLTLogNone || ((severity&fgGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0);
396 390 : return iResult;
397 : }
398 :
399 : void AliHLTLogging::SetGlobalLoggingLevel(AliHLTComponentLogSeverity level)
400 : {
401 : // see header file for class documentation
402 :
403 6 : fgGlobalLogFilter=level;
404 3 : }
405 :
406 : AliHLTComponentLogSeverity AliHLTLogging::GetGlobalLoggingLevel()
407 : {
408 : // see header file for class documentation
409 :
410 0 : return fgGlobalLogFilter;
411 : }
412 :
413 : void AliHLTLogging::SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
414 : {
415 : // see header file for class documentation
416 :
417 36 : fLocalLogFilter=level;
418 18 : }
419 :
420 :
421 : AliHLTComponentLogSeverity AliHLTLogging::GetLocalLoggingLevel()
422 : {
423 : // see header file for class documentation
424 :
425 6 : return fLocalLogFilter;
426 : }
427 :
428 : void AliHLTLogging::SetLocalLoggingDefault(AliHLTComponentLogSeverity level)
429 : {
430 : // see header file for class documentation
431 0 : fgLocalLogDefault=level;
432 0 : }
433 :
434 : int AliHLTLogging::CheckGroup(const char* /*originClass*/) const
435 : {
436 : // see header file for class documentation
437 :
438 0 : return 1;
439 : }
440 :
441 : int AliHLTLogging::SetBlackList(const char* classnames)
442 : {
443 : // see header file for class documentation
444 :
445 0 : if (classnames)
446 0 : fgBlackList=classnames;
447 0 : return 0;
448 : }
449 :
450 : int AliHLTLogging::SetWhiteList(const char* classnames)
451 : {
452 : // see header file for class documentation
453 :
454 0 : if (classnames)
455 0 : fgWhiteList=classnames;
456 0 : return 0;
457 : }
|