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