]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTLogging.cxx
HLT base
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTLogging.cxx
CommitLineData
fc455fba 1// @(#) $Id$
3495cce2 2
3/**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5 * *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
3495cce2 8 * for The ALICE Off-line 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
b22e91eb 19/** @file AliHLTLogging.cxx
20 @author Matthias Richter, Timm Steinbeck
21 @date
22 @brief Implementation of HLT logging primitives.
23*/
3495cce2 24
0c0c9d99 25#if __GNUC__>= 3
3495cce2 26using namespace std;
27#endif
28
85869391 29#include "AliHLTStdIncludes.h"
3495cce2 30#include "AliHLTLogging.h"
a742f6f8 31#include "AliHLTComponentHandler.h"
3cde846d 32#include "TString.h"
fc455fba 33#include "Varargs.h"
34#include <string>
35#include <sstream>
36#include <iostream>
37
b22e91eb 38/** ROOT macro for the implementation of ROOT specific class methods */
a742f6f8 39ClassImp(AliHLTLogging);
3495cce2 40
3495cce2 41AliHLTLogging::AliHLTLogging()
85869391 42 :
85869391 43 //fLocalLogFilter(kHLTLogDefault),
53feaef5 44 fLocalLogFilter(kHLTLogAll),
45 fpDefaultKeyword(NULL),
46 fpCurrentKeyword(NULL)
3495cce2 47{
5f5b708b 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
85869391 53}
54
55AliHLTLogging::AliHLTLogging(const AliHLTLogging&)
56 :
53feaef5 57 fLocalLogFilter(kHLTLogAll),
85869391 58 fpDefaultKeyword(NULL),
53feaef5 59 fpCurrentKeyword(NULL)
85869391 60{
5f5b708b 61 // see header file for class documentation
85869391 62 HLTFatal("copy constructor untested");
63}
64
65AliHLTLogging& AliHLTLogging::operator=(const AliHLTLogging&)
66{
5f5b708b 67 // see header file for class documentation
85869391 68 HLTFatal("assignment operator untested");
69 return *this;
3495cce2 70}
71
66043029 72ostringstream AliHLTLogging::fgLogstr;
73AliHLTComponentLogSeverity AliHLTLogging::fgGlobalLogFilter=kHLTLogAll;
74AliHLTfctLogging AliHLTLogging::fgLoggingFunc=NULL;
a742f6f8 75AliHLTLogging::AliHLTDynamicMessage AliHLTLogging::fgAliLoggingFunc=NULL;
fc455fba 76int AliHLTLogging::fgUseAliLog=1;
3495cce2 77
78AliHLTLogging::~AliHLTLogging()
79{
5f5b708b 80 // see header file for class documentation
3495cce2 81}
82
a742f6f8 83// the array will be grown dynamically, this is just an initial size
66043029 84TArrayC AliHLTLogging::fgAliHLTLoggingTarget(200);
a742f6f8 85// the maximum size of the array
66043029 86const int AliHLTLogging::fgkALIHLTLOGGINGMAXBUFFERSIZE=10000;
87
bb16cc41 88int AliHLTLogging::Init(AliHLTfctLogging pFun)
fa274626 89{
5f5b708b 90 // see header file for class documentation
66043029 91 if (fgLoggingFunc!=NULL && fgLoggingFunc!=pFun) {
92 (*fgLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");
bb16cc41 93 }
66043029 94 fgLoggingFunc=pFun;
fc455fba 95
bb16cc41 96 return 0;
97}
98
a742f6f8 99int 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
fc455fba 123int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity,
124 const char* origin, const char* keyword,
125 const char* message)
5f5b708b 126{
127 // see header file for class documentation
3495cce2 128 int iResult=0;
53feaef5 129 if (param==NULL) {
130 // this is currently just to get rid of the warning "unused parameter"
131 }
fc455fba 132
3495cce2 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 }
3cde846d 156 TString out="HLT Log ";
157 out+=strSeverity;
d54f579e 158 if (origin && origin[0]!=0) {out+=": <"; out+=origin; out+="> ";}
3cde846d 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;
3495cce2 164 return iResult;
165}
166
a742f6f8 167#if 0
fc455fba 168int AliHLTLogging::AliMessage(AliHLTComponentLogSeverity severity,
66043029 169 const char* originClass, const char* originFunc,
fc455fba 170 const char* file, int line, const char* message)
5f5b708b 171{
172 // see header file for class documentation
3495cce2 173
fc455fba 174 switch (severity) {
175 case kHLTLogBenchmark:
66043029 176 AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
fc455fba 177 break;
178 case kHLTLogDebug:
66043029 179 AliLog::Message(AliLog::kDebug, message, "HLT", originClass, originFunc, file, line);
fc455fba 180 break;
181 case kHLTLogInfo:
66043029 182 AliLog::Message(AliLog::kInfo, message, "HLT", originClass, originFunc, file, line);
fc455fba 183 break;
184 case kHLTLogWarning:
66043029 185 AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
fc455fba 186 break;
187 case kHLTLogError:
66043029 188 AliLog::Message(AliLog::kError, message, "HLT", originClass, originFunc, file, line);
fc455fba 189 break;
190 case kHLTLogFatal:
66043029 191 AliLog::Message(AliLog::kWarning, message, "HLT", originClass, originFunc, file, line);
fc455fba 192 break;
193 default:
194 break;
195 }
196 return 0;
197}
85465857 198#endif
fc455fba 199
200const char* AliHLTLogging::BuildLogString(const char *format, va_list ap)
201{
202 // see header file for class documentation
203
204 int iResult=0;
a59b461e 205#ifdef R__VA_COPY
fc455fba 206 va_list bap;
207 R__VA_COPY(bap, ap);
a59b461e 208#endif //R__VA_COPY
fc455fba 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
66043029 215 fgAliHLTLoggingTarget[0]=0;
fc455fba 216 while (fmt!=NULL) {
66043029 217 iResult=vsnprintf(fgAliHLTLoggingTarget.GetArray(), fgAliHLTLoggingTarget.GetSize(), fmt, ap);
fc455fba 218 if (iResult==-1)
219 // for compatibility with older version of vsnprintf
66043029 220 iResult=fgAliHLTLoggingTarget.GetSize()*2;
221 else if (iResult<fgAliHLTLoggingTarget.GetSize())
fc455fba 222 break;
223
224 // terminate if buffer is already at the limit
66043029 225 if (fgAliHLTLoggingTarget.GetSize()>=fgkALIHLTLOGGINGMAXBUFFERSIZE)
a59b461e 226 {
66043029 227 fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
fc455fba 228 break;
3495cce2 229 }
fc455fba 230
231 // check limitation and grow the buffer
66043029 232 if (iResult>fgkALIHLTLOGGINGMAXBUFFERSIZE) iResult=fgkALIHLTLOGGINGMAXBUFFERSIZE;
233 fgAliHLTLoggingTarget.Set(iResult+1);
fc455fba 234
235 // copy the original list and skip the first argument if this was the format string
a59b461e 236#ifdef R__VA_COPY
fc455fba 237 va_end(ap);
238 R__VA_COPY(ap, bap);
a59b461e 239#else
66043029 240 fgAliHLTLoggingTarget[fgAliHLTLoggingTarget.GetSize()-1]=0;
a59b461e 241 break;
242#endif //R__VA_COPY
fc455fba 243 if (format==NULL) va_arg(ap, const char*);
244 }
a59b461e 245#ifdef R__VA_COPY
fc455fba 246 va_end(bap);
a59b461e 247#endif //R__VA_COPY
fc455fba 248
66043029 249 return fgAliHLTLoggingTarget.GetArray();
3495cce2 250}
251
fc455fba 252int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
253 const char* origin, const char* keyword,
254 const char* format, ... )
5f5b708b 255{
256 // see header file for class documentation
85465857 257 int iResult=CheckFilter(severity);
258 if (iResult>0) {
259 va_list args;
260 va_start(args, format);
66043029 261 if (fgLoggingFunc) {
262 iResult = (*fgLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
85465857 263 } else {
a742f6f8 264 if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
265 iResult=(*fgAliLoggingFunc)(severity, NULL, origin, NULL, 0, AliHLTLogging::BuildLogString(format, args ));
fc455fba 266 else
fc455fba 267 iResult=Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
85465857 268 }
fc455fba 269 va_end(args);
3495cce2 270 }
85465857 271 return iResult;
3495cce2 272}
273
fc455fba 274int AliHLTLogging::LoggingVarargs(AliHLTComponentLogSeverity severity,
66043029 275 const char* originClass, const char* originFunc,
fc455fba 276 const char* file, int line, ... ) const
3495cce2 277{
5f5b708b 278 // see header file for class documentation
279
fc455fba 280 if (file==NULL && line==0) {
281 // this is currently just to get rid of the warning "unused parameter"
282 }
85465857 283 int iResult=CheckFilter(severity);
284 if (iResult>0) {
85465857 285 const char* separator="";
fc455fba 286 TString origin;
66043029 287 if (originClass) {
288 origin+=originClass;
85465857 289 separator="::";
3495cce2 290 }
66043029 291 if (originFunc) {
fc455fba 292 origin+=separator;
66043029 293 origin+=originFunc;
3495cce2 294 }
85465857 295 va_list args;
fc455fba 296 va_start(args, line);
85465857 297
66043029 298 if (fgLoggingFunc) {
299 iResult=(*fgLoggingFunc)(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
85465857 300 } else {
a742f6f8 301 if (fgUseAliLog!=0 && fgAliLoggingFunc!=NULL)
302 iResult=(*fgAliLoggingFunc)(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args ));
fc455fba 303 else
fc455fba 304 iResult=Message(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
85465857 305 }
306 va_end(args);
3495cce2 307 }
3495cce2 308 return iResult;
309}
310
8ede8717 311int AliHLTLogging::CheckFilter(AliHLTComponentLogSeverity severity) const
3495cce2 312{
5f5b708b 313 // see header file for class documentation
314
66043029 315 int iResult=severity==kHLTLogNone || (severity&fgGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
3495cce2 316 return iResult;
317}
5f5b708b 318
319void AliHLTLogging::SetGlobalLoggingLevel(AliHLTComponentLogSeverity level)
320{
321 // see header file for class documentation
322
66043029 323 fgGlobalLogFilter=level;
5f5b708b 324}
325
a742f6f8 326AliHLTComponentLogSeverity AliHLTLogging::GetGlobalLoggingLevel()
327{
328 // see header file for class documentation
329
330 return fgGlobalLogFilter;
331}
332
5f5b708b 333void AliHLTLogging::SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
334{
335 // see header file for class documentation
336
337 fLocalLogFilter=level;
338}
a742f6f8 339
340
341AliHLTComponentLogSeverity AliHLTLogging::GetLocalLoggingLevel()
342{
343 // see header file for class documentation
344
345 return fLocalLogFilter;
346}