]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTLogging.cxx
debug build off by default, started filtering of class names in HLT logging
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTLogging.cxx
1 // @(#) $Id$
2
3 /**************************************************************************
4  * This file is property of and copyright by the ALICE HLT Project        * 
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 #if __GNUC__>= 3
26 using namespace std;
27 #endif
28
29 #include "AliHLTStdIncludes.h"
30 #include "AliHLTLogging.h"
31 #include "AliHLTComponentHandler.h"
32 #include "TString.h"
33 #include "Varargs.h"
34 #include <string>
35 #include <sstream>
36 #include <iostream>
37
38 /** ROOT macro for the implementation of ROOT specific class methods */
39 ClassImp(AliHLTLogging);
40
41 AliHLTLogging::AliHLTLogging()
42   :
43   //fLocalLogFilter(kHLTLogDefault),
44   fLocalLogFilter(kHLTLogAll),
45   fpDefaultKeyword(NULL),
46   fpCurrentKeyword(NULL)
47 {
48   // see header file for class documentation
49   // or
50   // refer to README to build package
51   // or
52   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
53 }
54
55 AliHLTLogging::AliHLTLogging(const AliHLTLogging&)
56   :
57   fLocalLogFilter(kHLTLogAll),
58   fpDefaultKeyword(NULL),
59   fpCurrentKeyword(NULL)
60 {
61   // see header file for class documentation
62   HLTFatal("copy constructor untested");
63 }
64
65 AliHLTLogging& AliHLTLogging::operator=(const AliHLTLogging&)
66
67   // see header file for class documentation
68   HLTFatal("assignment operator untested");
69   return *this;
70 }
71
72 ostringstream AliHLTLogging::fgLogstr;
73 AliHLTComponentLogSeverity AliHLTLogging::fgGlobalLogFilter=kHLTLogAll;
74 AliHLTfctLogging AliHLTLogging::fgLoggingFunc=NULL;
75 AliHLTLogging::AliHLTDynamicMessage AliHLTLogging::fgAliLoggingFunc=NULL;
76 int AliHLTLogging::fgUseAliLog=1;
77
78 TString AliHLTLogging::fgBlackList="";
79 TString AliHLTLogging::fgWhiteList="";
80
81 AliHLTLogging::~AliHLTLogging()
82 {
83   // see header file for class documentation
84 }
85
86 // the array will be grown dynamically, this is just an initial size 
87 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   if (fgLoggingFunc!=NULL && fgLoggingFunc!=pFun) {
95     (*fgLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");    
96   }
97   fgLoggingFunc=pFun;
98   
99   return 0;
100 }
101
102 int AliHLTLogging::InitAliLogTrap(AliHLTComponentHandler* pHandler)
103 {
104   // see header file for class documentation
105   int iResult=0;
106   if (pHandler) {
107     AliHLTComponentLogSeverity loglevel=pHandler->GetLocalLoggingLevel();
108     pHandler->SetLocalLoggingLevel(kHLTLogError);
109     pHandler->LoadLibrary("libAliHLTUtil.so");
110     pHandler->SetLocalLoggingLevel(loglevel);
111     InitAliDynamicMessageCallback pFunc=(InitAliDynamicMessageCallback)pHandler->FindSymbol("libAliHLTUtil.so", "InitAliDynamicMessageCallback"); 
112     if (pFunc) {
113       iResult=(*pFunc)();
114     } else {
115       Message(NULL, kHLTLogError, "AliHLTLogging::InitAliLogTrap", "init logging",
116               "can not initialize AliLog callback");
117       iResult=-ENOSYS;
118     }
119   } else {
120     iResult=-EINVAL;
121   }
122   
123   return iResult;
124 }
125
126 int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity,
127                            const char* origin, const char* keyword,
128                            const char* message) 
129 {
130   // see header file for class documentation
131   int iResult=0;
132   if (param==NULL) {
133     // this is currently just to get rid of the warning "unused parameter"
134   }
135
136   const char* strSeverity="";
137   switch (severity) {
138   case kHLTLogBenchmark: 
139     strSeverity="benchmark";
140     break;
141   case kHLTLogDebug:
142     strSeverity="debug";
143     break;
144   case kHLTLogInfo:
145     strSeverity="info";
146     break;
147   case kHLTLogWarning:
148     strSeverity="warning";
149     break;
150   case kHLTLogError:
151     strSeverity="error";
152     break;
153   case kHLTLogFatal:
154     strSeverity="fatal";
155     break;
156   default:
157     break;
158   }
159   TString out="HLT Log ";
160   out+=strSeverity;
161   if (origin && origin[0]!=0) {out+=": <"; out+=origin; out+="> ";}
162   out+=" "; out+=message;
163   if (keyword!=NULL && strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0) {
164     out+=" ("; out+=keyword; out +=")";
165   }
166   cout << out.Data() << endl;
167   return iResult;
168 }
169
170 #if 0
171 int AliHLTLogging::AliMessage(AliHLTComponentLogSeverity severity, 
172                               const char* originClass, const char* originFunc,
173                               const char* file, int line, const char* message) 
174 {
175   // see header file for class documentation
176
177   switch (severity) {
178   case kHLTLogBenchmark: 
179     AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
180     break;
181   case kHLTLogDebug:
182     AliLog::Message(AliLog::kDebug, message, "HLT", originClass, originFunc, file, line);
183     break;
184   case kHLTLogInfo:
185     AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
186     break;
187   case kHLTLogWarning:
188     AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
189     break;
190   case kHLTLogError:
191     AliLog::Message(AliLog::kError, message, "HLT", originClass, originFunc, file, line);
192     break;
193   case kHLTLogFatal:
194     AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
195     break;
196   default:
197     break;
198   }
199   return 0;
200 }
201 #endif
202
203 const char* AliHLTLogging::BuildLogString(const char *format, va_list ap) 
204 {
205   // see header file for class documentation
206
207   int iResult=0;
208 #ifdef R__VA_COPY
209   va_list bap;
210   R__VA_COPY(bap, ap);
211 #endif //R__VA_COPY
212
213   // take the first argument from the list as format string if no
214   // format was given
215   const char* fmt = format;
216   if (fmt==NULL) fmt=va_arg(ap, const char*);
217
218   fgAliHLTLoggingTarget[0]=0;
219   while (fmt!=NULL) {
220     iResult=vsnprintf(fgAliHLTLoggingTarget.GetArray(), fgAliHLTLoggingTarget.GetSize(), fmt, ap);
221     if (iResult==-1)
222       // for compatibility with older version of vsnprintf
223       iResult=fgAliHLTLoggingTarget.GetSize()*2;
224     else if (iResult<fgAliHLTLoggingTarget.GetSize())
225       break;
226
227     // terminate if buffer is already at the limit
228     if (fgAliHLTLoggingTarget.GetSize()>=fgkALIHLTLOGGINGMAXBUFFERSIZE) 
229     {
230       fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
231       break;
232     }
233
234     // check limitation and grow the buffer
235     if (iResult>fgkALIHLTLOGGINGMAXBUFFERSIZE) iResult=fgkALIHLTLOGGINGMAXBUFFERSIZE;
236     fgAliHLTLoggingTarget.Set(iResult+1);
237
238     // copy the original list and skip the first argument if this was the format string
239 #ifdef R__VA_COPY
240     va_end(ap);
241     R__VA_COPY(ap, bap);
242 #else
243     fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
244     break;
245 #endif //R__VA_COPY
246     if (format==NULL) va_arg(ap, const char*);
247   }     
248 #ifdef R__VA_COPY
249   va_end(bap);
250 #endif //R__VA_COPY
251
252   return fgAliHLTLoggingTarget.GetArray();
253 }
254
255 int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
256                            const char* origin, const char* keyword,
257                            const char* format, ... ) 
258 {
259   // see header file for class documentation
260   int iResult=CheckFilter(severity);
261   if (iResult>0) {
262     va_list args;
263     va_start(args, format);
264     if (fgLoggingFunc) {
265       iResult = (*fgLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
266     } else {
267       if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
268         iResult=(*fgAliLoggingFunc)(severity, NULL, origin, NULL, 0, AliHLTLogging::BuildLogString(format, args ));
269       else
270         iResult=Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
271     }
272     va_end(args);
273   }
274   return iResult;
275 }
276
277 int AliHLTLogging::LoggingVarargs(AliHLTComponentLogSeverity severity, 
278                                   const char* originClass, const char* originFunc,
279                                   const char* file, int line,  ... ) const
280 {
281   // see header file for class documentation
282
283   if (file==NULL && line==0) {
284     // this is currently just to get rid of the warning "unused parameter"
285   }
286   int iResult=1; //CheckFilter(severity); // check moved to makro
287   if (iResult>0) {
288     const char* separator="";
289     TString origin;
290     if (originClass) {
291         origin+=originClass;
292         separator="::";
293     }
294     if (originFunc) {
295         origin+=separator;
296         origin+=originFunc;
297     }
298     va_list args;
299     va_start(args, line);
300
301     if (fgLoggingFunc) {
302       iResult=(*fgLoggingFunc)(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
303     } else {
304       if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
305         iResult=(*fgAliLoggingFunc)(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args ));
306       else
307         iResult=Message(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
308     }
309     va_end(args);
310   }
311   return iResult;
312 }
313
314 int AliHLTLogging::CheckFilter(AliHLTComponentLogSeverity severity) const
315 {
316   // see header file for class documentation
317
318   int iResult=severity==kHLTLogNone || (severity&fgGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
319   return iResult;
320 }
321
322 void AliHLTLogging::SetGlobalLoggingLevel(AliHLTComponentLogSeverity level)
323 {
324   // see header file for class documentation
325
326   fgGlobalLogFilter=level;
327 }
328
329 AliHLTComponentLogSeverity AliHLTLogging::GetGlobalLoggingLevel()
330 {
331   // see header file for class documentation
332
333   return fgGlobalLogFilter;
334 }
335
336 void AliHLTLogging::SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
337 {
338   // see header file for class documentation
339
340   fLocalLogFilter=level;
341 }
342
343
344 AliHLTComponentLogSeverity AliHLTLogging::GetLocalLoggingLevel()
345 {
346   // see header file for class documentation
347
348   return fLocalLogFilter;
349 }
350
351 int AliHLTLogging::CheckGroup(const char* originClass) const
352 {
353   // see header file for class documentation
354
355   return 1;
356 }
357
358 int AliHLTLogging::SetBlackList(const char* classnames)
359 {
360   // see header file for class documentation
361
362   if (classnames)
363     fgBlackList=classnames;
364   return 0;
365 }
366
367 int AliHLTLogging::SetWhiteList(const char* classnames)
368 {
369   // see header file for class documentation
370
371   if (classnames)
372     fgWhiteList=classnames;
373   return 0;
374 }