]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTLogging.cxx
documentation
[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 AliHLTLogging::~AliHLTLogging()
79 {
80   // see header file for class documentation
81 }
82
83 // the array will be grown dynamically, this is just an initial size 
84 TArrayC AliHLTLogging::fgAliHLTLoggingTarget(200);
85 // the maximum size of the array
86 const int AliHLTLogging::fgkALIHLTLOGGINGMAXBUFFERSIZE=10000;
87
88 int AliHLTLogging::Init(AliHLTfctLogging pFun) 
89 {
90   // see header file for class documentation
91   if (fgLoggingFunc!=NULL && fgLoggingFunc!=pFun) {
92     (*fgLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");    
93   }
94   fgLoggingFunc=pFun;
95   
96   return 0;
97 }
98
99 int AliHLTLogging::InitAliLogTrap(AliHLTComponentHandler* pHandler)
100 {
101   // see header file for class documentation
102   int iResult=0;
103   if (pHandler) {
104     AliHLTComponentLogSeverity loglevel=pHandler->GetLocalLoggingLevel();
105     pHandler->SetLocalLoggingLevel(kHLTLogError);
106     pHandler->LoadLibrary("libAliHLTUtil.so");
107     pHandler->SetLocalLoggingLevel(loglevel);
108     InitAliDynamicMessageCallback pFunc=(InitAliDynamicMessageCallback)pHandler->FindSymbol("libAliHLTUtil.so", "InitAliDynamicMessageCallback"); 
109     if (pFunc) {
110       iResult=(*pFunc)();
111     } else {
112       Message(NULL, kHLTLogError, "AliHLTLogging::InitAliLogTrap", "init logging",
113               "can not initialize AliLog callback");
114       iResult=-ENOSYS;
115     }
116   } else {
117     iResult=-EINVAL;
118   }
119   
120   return iResult;
121 }
122
123 int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity,
124                            const char* origin, const char* keyword,
125                            const char* message) 
126 {
127   // see header file for class documentation
128   int iResult=0;
129   if (param==NULL) {
130     // this is currently just to get rid of the warning "unused parameter"
131   }
132
133   const char* strSeverity="";
134   switch (severity) {
135   case kHLTLogBenchmark: 
136     strSeverity="benchmark";
137     break;
138   case kHLTLogDebug:
139     strSeverity="debug";
140     break;
141   case kHLTLogInfo:
142     strSeverity="info";
143     break;
144   case kHLTLogWarning:
145     strSeverity="warning";
146     break;
147   case kHLTLogError:
148     strSeverity="error";
149     break;
150   case kHLTLogFatal:
151     strSeverity="fatal";
152     break;
153   default:
154     break;
155   }
156   TString out="HLT Log ";
157   out+=strSeverity;
158   if (origin && origin[0]!=0) {out+=": <"; out+=origin; out+="> ";}
159   out+=" "; out+=message;
160   if (keyword!=NULL && strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0) {
161     out+=" ("; out+=keyword; out +=")";
162   }
163   cout << out.Data() << endl;
164   return iResult;
165 }
166
167 #if 0
168 int AliHLTLogging::AliMessage(AliHLTComponentLogSeverity severity, 
169                               const char* originClass, const char* originFunc,
170                               const char* file, int line, const char* message) 
171 {
172   // see header file for class documentation
173
174   switch (severity) {
175   case kHLTLogBenchmark: 
176     AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
177     break;
178   case kHLTLogDebug:
179     AliLog::Message(AliLog::kDebug, message, "HLT", originClass, originFunc, file, line);
180     break;
181   case kHLTLogInfo:
182     AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
183     break;
184   case kHLTLogWarning:
185     AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
186     break;
187   case kHLTLogError:
188     AliLog::Message(AliLog::kError, message, "HLT", originClass, originFunc, file, line);
189     break;
190   case kHLTLogFatal:
191     AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
192     break;
193   default:
194     break;
195   }
196   return 0;
197 }
198 #endif
199
200 const char* AliHLTLogging::BuildLogString(const char *format, va_list ap) 
201 {
202   // see header file for class documentation
203
204   int iResult=0;
205 #ifdef R__VA_COPY
206   va_list bap;
207   R__VA_COPY(bap, ap);
208 #endif //R__VA_COPY
209
210   // take the first argument from the list as format string if no
211   // format was given
212   const char* fmt = format;
213   if (fmt==NULL) fmt=va_arg(ap, const char*);
214
215   fgAliHLTLoggingTarget[0]=0;
216   while (fmt!=NULL) {
217     iResult=vsnprintf(fgAliHLTLoggingTarget.GetArray(), fgAliHLTLoggingTarget.GetSize(), fmt, ap);
218     if (iResult==-1)
219       // for compatibility with older version of vsnprintf
220       iResult=fgAliHLTLoggingTarget.GetSize()*2;
221     else if (iResult<fgAliHLTLoggingTarget.GetSize())
222       break;
223
224     // terminate if buffer is already at the limit
225     if (fgAliHLTLoggingTarget.GetSize()>=fgkALIHLTLOGGINGMAXBUFFERSIZE) 
226     {
227       fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
228       break;
229     }
230
231     // check limitation and grow the buffer
232     if (iResult>fgkALIHLTLOGGINGMAXBUFFERSIZE) iResult=fgkALIHLTLOGGINGMAXBUFFERSIZE;
233     fgAliHLTLoggingTarget.Set(iResult+1);
234
235     // copy the original list and skip the first argument if this was the format string
236 #ifdef R__VA_COPY
237     va_end(ap);
238     R__VA_COPY(ap, bap);
239 #else
240     fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
241     break;
242 #endif //R__VA_COPY
243     if (format==NULL) va_arg(ap, const char*);
244   }     
245 #ifdef R__VA_COPY
246   va_end(bap);
247 #endif //R__VA_COPY
248
249   return fgAliHLTLoggingTarget.GetArray();
250 }
251
252 int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
253                            const char* origin, const char* keyword,
254                            const char* format, ... ) 
255 {
256   // see header file for class documentation
257   int iResult=CheckFilter(severity);
258   if (iResult>0) {
259     va_list args;
260     va_start(args, format);
261     if (fgLoggingFunc) {
262       iResult = (*fgLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
263     } else {
264       if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
265         iResult=(*fgAliLoggingFunc)(severity, NULL, origin, NULL, 0, AliHLTLogging::BuildLogString(format, args ));
266       else
267         iResult=Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
268     }
269     va_end(args);
270   }
271   return iResult;
272 }
273
274 int AliHLTLogging::LoggingVarargs(AliHLTComponentLogSeverity severity, 
275                                   const char* originClass, const char* originFunc,
276                                   const char* file, int line,  ... ) const
277 {
278   // see header file for class documentation
279
280   if (file==NULL && line==0) {
281     // this is currently just to get rid of the warning "unused parameter"
282   }
283   int iResult=CheckFilter(severity);
284   if (iResult>0) {
285     const char* separator="";
286     TString origin;
287     if (originClass) {
288         origin+=originClass;
289         separator="::";
290     }
291     if (originFunc) {
292         origin+=separator;
293         origin+=originFunc;
294     }
295     va_list args;
296     va_start(args, line);
297
298     if (fgLoggingFunc) {
299       iResult=(*fgLoggingFunc)(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
300     } else {
301       if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
302         iResult=(*fgAliLoggingFunc)(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args ));
303       else
304         iResult=Message(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
305     }
306     va_end(args);
307   }
308   return iResult;
309 }
310
311 int AliHLTLogging::CheckFilter(AliHLTComponentLogSeverity severity) const
312 {
313   // see header file for class documentation
314
315   int iResult=severity==kHLTLogNone || (severity&fgGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
316   return iResult;
317 }
318
319 void AliHLTLogging::SetGlobalLoggingLevel(AliHLTComponentLogSeverity level)
320 {
321   // see header file for class documentation
322
323   fgGlobalLogFilter=level;
324 }
325
326 AliHLTComponentLogSeverity AliHLTLogging::GetGlobalLoggingLevel()
327 {
328   // see header file for class documentation
329
330   return fgGlobalLogFilter;
331 }
332
333 void AliHLTLogging::SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
334 {
335   // see header file for class documentation
336
337   fLocalLogFilter=level;
338 }
339
340
341 AliHLTComponentLogSeverity AliHLTLogging::GetLocalLoggingLevel()
342 {
343   // see header file for class documentation
344
345   return fLocalLogFilter;
346 }