]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTLogging.cxx
Ignoring temporary files
[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   // init the AliRoot logging trap
106   // AliLog messages are redirected to PubSub,
107   int iResult=0;
108   if (pHandler) {
109     // set temporary loglevel of component handler
110     AliHLTComponentLogSeverity loglevel=pHandler->GetLocalLoggingLevel();
111     pHandler->SetLocalLoggingLevel(kHLTLogError);
112
113     // load library containing AliRoot dependencies and initialization handler
114     pHandler->LoadLibrary(ALILOG_WRAPPER_LIBRARY, 0/* do not activate agents */);
115
116     // restore loglevel
117     pHandler->SetLocalLoggingLevel(loglevel);
118
119     // find the symbol
120     InitAliDynamicMessageCallback pFunc=(InitAliDynamicMessageCallback)pHandler->FindSymbol(ALILOG_WRAPPER_LIBRARY, "InitAliDynamicMessageCallback"); 
121     if (pFunc) {
122       iResult=(*pFunc)();
123     } else {
124       Message(NULL, kHLTLogError, "AliHLTLogging::InitAliLogTrap", "init logging",
125               "can not initialize AliLog callback");
126       iResult=-ENOSYS;
127     }
128   } else {
129     iResult=-EINVAL;
130   }
131   
132   return iResult;
133 }
134
135 int AliHLTLogging::InitAliLogFunc(AliHLTComponentHandler* pHandler)
136 {
137   // see header file for class documentation
138   int iResult=0;
139   if (pHandler) {
140     // set temporary loglevel of component handler
141     AliHLTComponentLogSeverity loglevel=pHandler->GetLocalLoggingLevel();
142     pHandler->SetLocalLoggingLevel(kHLTLogError);
143
144     // load library containing AliRoot dependencies and initialization handler
145     pHandler->LoadLibrary(ALILOG_WRAPPER_LIBRARY, 0/* do not activate agents */);
146
147     // restore loglevel
148     pHandler->SetLocalLoggingLevel(loglevel);
149
150     // find the symbol
151     fgAliLoggingFunc=(AliHLTLogging::AliHLTDynamicMessage)pHandler->FindSymbol(ALILOG_WRAPPER_LIBRARY, "AliDynamicMessage");
152     if (fgAliLoggingFunc==NULL) {
153       Message(NULL, kHLTLogError, "AliHLTLogging::InitAliLogFunc", "init logging",
154               "symbol lookp failure: can not find AliDynamicMessage, switching to HLT logging system");
155       iResult=-ENOSYS;
156     }
157   } else {
158     iResult=-EINVAL;
159   }
160   
161   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   switch (severity) {
176   case kHLTLogBenchmark: 
177     strSeverity="benchmark";
178     break;
179   case kHLTLogDebug:
180     strSeverity="debug";
181     break;
182   case kHLTLogInfo:
183     strSeverity="info";
184     break;
185   case kHLTLogWarning:
186     strSeverity="warning";
187     break;
188   case kHLTLogError:
189     strSeverity="error";
190     break;
191   case kHLTLogFatal:
192     strSeverity="fatal";
193     break;
194   case kHLTLogImportant:
195     strSeverity="notify";
196     break;
197   default:
198     break;
199   }
200   TString out="HLT Log ";
201   out+=strSeverity;
202   if (origin && origin[0]!=0) {out+=": <"; out+=origin; out+="> ";}
203   out+=" "; out+=message;
204   if (keyword!=NULL && strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0) {
205     out+=" ("; out+=keyword; out +=")";
206   }
207   cout << out.Data() << endl;
208   return iResult;
209 }
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) 
245 {
246   // see header file for class documentation
247
248   int iResult=0;
249 #ifdef R__VA_COPY
250   va_list bap;
251   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   if (fmt==NULL) fmt=va_arg(ap, const char*);
258
259   fgAliHLTLoggingTarget[0]=0;
260   while (fmt!=NULL) {
261     iResult=vsnprintf(fgAliHLTLoggingTarget.GetArray(), fgAliHLTLoggingTarget.GetSize(), fmt, ap);
262     if (iResult==-1)
263       // for compatibility with older version of vsnprintf
264       iResult=fgAliHLTLoggingTarget.GetSize()*2;
265     else if (iResult<fgAliHLTLoggingTarget.GetSize())
266       break;
267
268     // terminate if buffer is already at the limit
269     if (fgAliHLTLoggingTarget.GetSize()>=fgkALIHLTLOGGINGMAXBUFFERSIZE) 
270     {
271       fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
272       break;
273     }
274
275     // check limitation and grow the buffer
276     if (iResult>fgkALIHLTLOGGINGMAXBUFFERSIZE) iResult=fgkALIHLTLOGGINGMAXBUFFERSIZE;
277     fgAliHLTLoggingTarget.Set(iResult+1);
278
279     // copy the original list and skip the first argument if this was the format string
280 #ifdef R__VA_COPY
281     va_end(ap);
282     R__VA_COPY(ap, bap);
283 #else
284     fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
285     break;
286 #endif //R__VA_COPY
287     if (format==NULL) va_arg(ap, const char*);
288   }     
289 #ifdef R__VA_COPY
290   va_end(bap);
291 #endif //R__VA_COPY
292
293   return fgAliHLTLoggingTarget.GetArray();
294 }
295
296 int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
297                            const char* origin, const char* keyword,
298                            const char* format, ... ) 
299 {
300   // see header file for class documentation
301   int iResult=CheckFilter(severity);
302   if (iResult>0) {
303     va_list args;
304     va_start(args, format);
305     if (fgLoggingFunc) {
306       iResult = (*fgLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
307     } else {
308       if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
309         iResult=(*fgAliLoggingFunc)(severity, NULL, origin, NULL, 0, AliHLTLogging::BuildLogString(format, args ));
310       else
311         iResult=Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
312     }
313     va_end(args);
314   }
315   return iResult;
316 }
317
318 int AliHLTLogging::LoggingVarargs(AliHLTComponentLogSeverity severity, 
319                                   const char* originClass, const char* originFunc,
320                                   const char* file, int line,  ... ) const
321 {
322   // see header file for class documentation
323
324   if (file==NULL && line==0) {
325     // this is currently just to get rid of the warning "unused parameter"
326   }
327   int iResult=1; //CheckFilter(severity); // check moved to makro
328   if (iResult>0) {
329     const char* separator="";
330     TString origin;
331     if (originClass) {
332         origin+=originClass;
333         separator="::";
334     }
335     if (originFunc) {
336         origin+=separator;
337         origin+=originFunc;
338     }
339     va_list args;
340     va_start(args, line);
341
342     if (fgLoggingFunc) {
343       iResult=(*fgLoggingFunc)(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
344     } else {
345       if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
346         iResult=(*fgAliLoggingFunc)(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args ));
347       else
348         iResult=Message(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
349     }
350     va_end(args);
351   }
352   return iResult;
353 }
354
355 int AliHLTLogging::CheckFilter(AliHLTComponentLogSeverity severity) const
356 {
357   // see header file for class documentation
358
359   int iResult=severity==kHLTLogNone || (severity&fgGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
360   return iResult;
361 }
362
363 void AliHLTLogging::SetGlobalLoggingLevel(AliHLTComponentLogSeverity level)
364 {
365   // see header file for class documentation
366
367   fgGlobalLogFilter=level;
368 }
369
370 AliHLTComponentLogSeverity AliHLTLogging::GetGlobalLoggingLevel()
371 {
372   // see header file for class documentation
373
374   return fgGlobalLogFilter;
375 }
376
377 void AliHLTLogging::SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
378 {
379   // see header file for class documentation
380
381   fLocalLogFilter=level;
382 }
383
384
385 AliHLTComponentLogSeverity AliHLTLogging::GetLocalLoggingLevel()
386 {
387   // see header file for class documentation
388
389   return fLocalLogFilter;
390 }
391
392 int AliHLTLogging::CheckGroup(const char* /*originClass*/) const
393 {
394   // see header file for class documentation
395
396   return 1;
397 }
398
399 int AliHLTLogging::SetBlackList(const char* classnames)
400 {
401   // see header file for class documentation
402
403   if (classnames)
404     fgBlackList=classnames;
405   return 0;
406 }
407
408 int AliHLTLogging::SetWhiteList(const char* classnames)
409 {
410   // see header file for class documentation
411
412   if (classnames)
413     fgWhiteList=classnames;
414   return 0;
415 }