]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTLogging.cxx
Modifications needed by TRD (Christoph)
[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
fc455fba 29#ifndef NOALIROOT_LOGGING
30#include "AliLog.h"
31#endif
85869391 32#include "AliHLTStdIncludes.h"
3495cce2 33#include "AliHLTLogging.h"
3cde846d 34#include "TString.h"
fc455fba 35#include "TArrayC.h"
36#include "Varargs.h"
37#include <string>
38#include <sstream>
39#include <iostream>
40
41/** target stream for AliRoot logging methods */
42ostringstream gLogstr;
43
44#ifndef NOALIROOT_LOGGING
45/**
46 * Notification callback for AliRoot logging methods
47 */
48void LogNotification(AliLog::EType_t level, const char* message)
49{
50 cout << "Notification handler: " << gLogstr.str() << endl;
51 AliHLTLogging hltlog;
52 hltlog.SwitchAliLog(0);
53 hltlog.Logging(kHLTLogInfo, "NotificationHandler", "AliLog", gLogstr.str().c_str());
54 gLogstr.clear();
55 string empty("");
56 gLogstr.str(empty);
57}
58#endif
3495cce2 59
fc455fba 60/**
61 * The global logging buffer.
62 * The buffer is created with an initial size and grown dynamically on
63 * demand.
64 */
65TArrayC gAliHLTLoggingTarget(200);
66
67/** the maximum size of the buffer */
68const int gALIHLTLOGGING_MAXBUFFERSIZE=10000;
5f1685a0 69
b22e91eb 70/** ROOT macro for the implementation of ROOT specific class methods */
3495cce2 71ClassImp(AliHLTLogging)
72
3495cce2 73AliHLTLogging::AliHLTLogging()
85869391 74 :
85869391 75 //fLocalLogFilter(kHLTLogDefault),
53feaef5 76 fLocalLogFilter(kHLTLogAll),
77 fpDefaultKeyword(NULL),
78 fpCurrentKeyword(NULL)
3495cce2 79{
5f5b708b 80 // see header file for class documentation
81 // or
82 // refer to README to build package
83 // or
84 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
85869391 85}
86
87AliHLTLogging::AliHLTLogging(const AliHLTLogging&)
88 :
53feaef5 89 fLocalLogFilter(kHLTLogAll),
85869391 90 fpDefaultKeyword(NULL),
53feaef5 91 fpCurrentKeyword(NULL)
85869391 92{
5f5b708b 93 // see header file for class documentation
85869391 94 HLTFatal("copy constructor untested");
95}
96
97AliHLTLogging& AliHLTLogging::operator=(const AliHLTLogging&)
98{
5f5b708b 99 // see header file for class documentation
85869391 100 HLTFatal("assignment operator untested");
101 return *this;
3495cce2 102}
103
8ede8717 104AliHLTComponentLogSeverity AliHLTLogging::fGlobalLogFilter=kHLTLogAll;
b22e91eb 105AliHLTfctLogging AliHLTLogging::fLoggingFunc=NULL;
fc455fba 106int AliHLTLogging::fgUseAliLog=1;
3495cce2 107
108AliHLTLogging::~AliHLTLogging()
109{
5f5b708b 110 // see header file for class documentation
3495cce2 111}
112
bb16cc41 113int AliHLTLogging::Init(AliHLTfctLogging pFun)
114{
5f5b708b 115 // see header file for class documentation
bb16cc41 116 if (fLoggingFunc!=NULL && fLoggingFunc!=pFun) {
117 (*fLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");
118 }
fc455fba 119 fLoggingFunc=pFun;
120#ifndef NOALIROOT_LOGGING
121 // to be activated when changes to AliLog have been committed
122// AliLog* log=new AliLog;
123// log->SetLogNotification(LogNotification);
124// log->SetStreamOutput(&gLogstr);
125#endif
126
bb16cc41 127 return 0;
128}
129
fc455fba 130int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity,
131 const char* origin, const char* keyword,
132 const char* message)
5f5b708b 133{
134 // see header file for class documentation
3495cce2 135 int iResult=0;
53feaef5 136 if (param==NULL) {
137 // this is currently just to get rid of the warning "unused parameter"
138 }
fc455fba 139
3495cce2 140 const char* strSeverity="";
141 switch (severity) {
142 case kHLTLogBenchmark:
143 strSeverity="benchmark";
144 break;
145 case kHLTLogDebug:
146 strSeverity="debug";
147 break;
148 case kHLTLogInfo:
149 strSeverity="info";
150 break;
151 case kHLTLogWarning:
152 strSeverity="warning";
153 break;
154 case kHLTLogError:
155 strSeverity="error";
156 break;
157 case kHLTLogFatal:
158 strSeverity="fatal";
159 break;
160 default:
161 break;
162 }
3cde846d 163 TString out="HLT Log ";
164 out+=strSeverity;
165 if (origin) {out+=": "; out+=origin;}
166 out+=" "; out+=message;
167 if (keyword!=NULL && strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0) {
168 out+=" ("; out+=keyword; out +=")";
169 }
170 cout << out.Data() << endl;
3495cce2 171 return iResult;
172}
173
fc455fba 174#ifndef NOALIROOT_LOGGING
175int AliHLTLogging::AliMessage(AliHLTComponentLogSeverity severity,
176 const char* origin_class, const char* origin_func,
177 const char* file, int line, const char* message)
5f5b708b 178{
179 // see header file for class documentation
3495cce2 180
fc455fba 181 switch (severity) {
182 case kHLTLogBenchmark:
183 AliLog::Message(AliLog::kInfo, message, "HLT", origin_class, origin_func, file, line);
184 break;
185 case kHLTLogDebug:
186 AliLog::Message(AliLog::kDebug, message, "HLT", origin_class, origin_func, file, line);
187 break;
188 case kHLTLogInfo:
189 AliLog::Message(AliLog::kInfo, message, "HLT", origin_class, origin_func, file, line);
190 break;
191 case kHLTLogWarning:
192 AliLog::Message(AliLog::kWarning, message, "HLT", origin_class, origin_func, file, line);
193 break;
194 case kHLTLogError:
195 AliLog::Message(AliLog::kError, message, "HLT", origin_class, origin_func, file, line);
196 break;
197 case kHLTLogFatal:
198 AliLog::Message(AliLog::kWarning, message, "HLT", origin_class, origin_func, file, line);
199 break;
200 default:
201 break;
202 }
203 return 0;
204}
85465857 205#endif
fc455fba 206
207const char* AliHLTLogging::BuildLogString(const char *format, va_list ap)
208{
209 // see header file for class documentation
210
211 int iResult=0;
212 va_list bap;
213 R__VA_COPY(bap, ap);
214
215 // take the first argument from the list as format string if no
216 // format was given
217 const char* fmt = format;
218 if (fmt==NULL) fmt=va_arg(ap, const char*);
219
220 gAliHLTLoggingTarget[0]=0;
221 while (fmt!=NULL) {
222 iResult=vsnprintf(gAliHLTLoggingTarget.GetArray(), gAliHLTLoggingTarget.GetSize(), fmt, ap);
223 if (iResult==-1)
224 // for compatibility with older version of vsnprintf
225 iResult=gAliHLTLoggingTarget.GetSize()*2;
226 else if (iResult<gAliHLTLoggingTarget.GetSize())
227 break;
228
229 // terminate if buffer is already at the limit
230 if (gAliHLTLoggingTarget.GetSize()>=gALIHLTLOGGING_MAXBUFFERSIZE) {
231 gAliHLTLoggingTarget[gAliHLTLoggingTarget.GetSize()-1]=0;
232 break;
3495cce2 233 }
fc455fba 234
235 // check limitation and grow the buffer
236 if (iResult>gALIHLTLOGGING_MAXBUFFERSIZE) iResult=gALIHLTLOGGING_MAXBUFFERSIZE;
237 gAliHLTLoggingTarget.Set(iResult+1);
238
239 // copy the original list and skip the first argument if this was the format string
240 va_end(ap);
241 R__VA_COPY(ap, bap);
242 if (format==NULL) va_arg(ap, const char*);
243 }
244 va_end(bap);
245
246 return gAliHLTLoggingTarget.GetArray();
3495cce2 247}
248
fc455fba 249int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity,
250 const char* origin, const char* keyword,
251 const char* format, ... )
5f5b708b 252{
253 // see header file for class documentation
85465857 254 int iResult=CheckFilter(severity);
255 if (iResult>0) {
256 va_list args;
257 va_start(args, format);
258 if (fLoggingFunc) {
259 iResult = (*fLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
260 } else {
fc455fba 261#ifndef NOALIROOT_LOGGING
262 if (fgUseAliLog)
263 iResult=AliMessage(severity, NULL, origin, NULL, 0, AliHLTLogging::BuildLogString(format, args ));
264 else
265#endif
266 iResult=Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
85465857 267 }
fc455fba 268 va_end(args);
3495cce2 269 }
85465857 270 return iResult;
3495cce2 271}
272
fc455fba 273int AliHLTLogging::LoggingVarargs(AliHLTComponentLogSeverity severity,
274 const char* origin_class, const char* origin_func,
275 const char* file, int line, ... ) const
3495cce2 276{
5f5b708b 277 // see header file for class documentation
278
fc455fba 279 if (file==NULL && line==0) {
280 // this is currently just to get rid of the warning "unused parameter"
281 }
85465857 282 int iResult=CheckFilter(severity);
283 if (iResult>0) {
85465857 284 const char* separator="";
fc455fba 285 TString origin;
85465857 286 if (origin_class) {
fc455fba 287 origin+=origin_class;
85465857 288 separator="::";
3495cce2 289 }
85465857 290 if (origin_func) {
fc455fba 291 origin+=separator;
292 origin+=origin_func;
3495cce2 293 }
85465857 294 va_list args;
fc455fba 295 va_start(args, line);
85465857 296
85465857 297 if (fLoggingFunc) {
fc455fba 298 iResult=(*fLoggingFunc)(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
85465857 299 } else {
fc455fba 300#ifndef NOALIROOT_LOGGING
301 if (fgUseAliLog)
302 iResult=AliMessage(severity, origin_class, origin_func, file, line, AliHLTLogging::BuildLogString(NULL, args ));
303 else
304#endif
305 iResult=Message(NULL/*fParam*/, severity, origin.Data(), GetKeyword(), AliHLTLogging::BuildLogString(NULL, args ));
85465857 306 }
307 va_end(args);
3495cce2 308 }
3495cce2 309 return iResult;
310}
311
8ede8717 312int AliHLTLogging::CheckFilter(AliHLTComponentLogSeverity severity) const
3495cce2 313{
5f5b708b 314 // see header file for class documentation
315
85465857 316 int iResult=severity==kHLTLogNone || (severity&fGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
3495cce2 317 return iResult;
318}
5f5b708b 319
320void AliHLTLogging::SetGlobalLoggingLevel(AliHLTComponentLogSeverity level)
321{
322 // see header file for class documentation
323
324 fGlobalLogFilter=level;
325}
326
327void AliHLTLogging::SetLocalLoggingLevel(AliHLTComponentLogSeverity level)
328{
329 // see header file for class documentation
330
331 fLocalLogFilter=level;
332}