PWGPP-4 ATO-35 - optional enablig of core dumps in case envirnment variables ALIROOT_...
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliLog.cxx
CommitLineData
6ab674bd 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16/* $Id$ */
17
18///////////////////////////////////////////////////////////////////////////////
19// //
20// class for logging debug, info and error messages //
21// //
22// The AliLog class is a singleton class. It allows to steer the output //
23// level and output streams for different types of messages via static //
24// methods. //
25// //
26// It also handles the messages produces by the preprocessor macros defined //
27// in the header file: AliDebug, AliInfo, AliWarning, AliError, AliFatal. //
28// //
29// More details about the message logging can be found on the ALICE Offline //
30// web page. //
31// //
32///////////////////////////////////////////////////////////////////////////////
33
b09247a2 34#include <cstdlib>
30ae1e2b 35#include <strings.h>
eeb769e2 36#include <Riostream.h>
6ab674bd 37#include <TError.h>
38#include <TNamed.h>
39#include <TSystem.h>
eeb769e2 40#include <TEnv.h>
1948744e 41#include <TArrayC.h>
a7a801dc 42#include <Varargs.h> // platform independent definition of va_copy
6ab674bd 43
44#include "AliLog.h"
45
66b0310c 46using std::endl;
47using std::cout;
48using std::ostream;
49using std::cerr;
50using std::ofstream;
51using std::ios;
6ab674bd 52ClassImp(AliLog)
53
6d7793cf 54// implementation of a singleton here
6ab674bd 55AliLog* AliLog::fgInstance = NULL;
56
57Bool_t AliLog::fgDebugEnabled = kTRUE;
55247085 58Bool_t AliLog::fgCoreEnabled = kFALSE;
6ab674bd 59
6d7793cf 60/**
61 * get root logger singleton instance
62 */
63AliLog *AliLog::GetRootLogger()
64{
65 if (fgInstance == NULL)
66 {
67 // creating singleton
68 fgInstance = new AliLog;
69 }
6ab674bd 70
6d7793cf 71 return fgInstance;
72}
73
74/**
75 * delete the root logger singleton instance
76 */
77void AliLog::DeleteRootLogger()
78{
79 if (fgInstance != NULL)
80 {
81 delete fgInstance;
82 fgInstance = NULL;
83 }
84}
85
86/**
87 * default private constructor
88 */
6ab674bd 89AliLog::AliLog() :
90 TObject(),
91 fGlobalLogLevel(kInfo),
92 fModuleDebugLevels(),
45fa0297 93 fClassDebugLevels(),
94 fPrintRepetitions(kTRUE),
95 fRepetitions(0),
96 fLastType(0),
97 fLastMessage(),
98 fLastModule(),
99 fLastClassName(),
100 fLastFunction(),
101 fLastFile(),
102 fLastLine(0)
6ab674bd 103{
104// default constructor: set default values
105
6d7793cf 106 for (Int_t iType = kFatal; iType < kMaxType; iType++)
107 {
6ab674bd 108 fOutputTypes[iType] = 0;
109 fFileNames[iType] = "";
110 fOutputFiles[iType] = NULL;
eeb769e2 111 fOutputStreams[iType] = NULL;
1948744e 112 fCallBacks[iType]=NULL;
6ab674bd 113
114 fPrintType[iType] = kTRUE;
115 fPrintModule[iType] = kFALSE;
116 fPrintScope[iType] = kTRUE;
117 fPrintLocation[iType] = (iType == kDebug);
118 }
119
6d7793cf 120 // TO BE REVIEWED
6ab674bd 121 // replace the previous instance by this one
122 if (fgInstance) delete fgInstance;
123 fgInstance = this;
eeb769e2 124
45fa0297 125 SetHandleRootMessages(kTRUE);
126
eeb769e2 127 // read the .rootrc settings
128 ReadEnvSettings();
6ab674bd 129}
130
6d7793cf 131/**
132 * private destructor
133 */
6ab674bd 134AliLog::~AliLog()
135{
136// destructor: clean up and reset instance pointer
137
45fa0297 138 if (fRepetitions > 0) PrintRepetitions();
139
6d7793cf 140 for (Int_t i = 0; i < fModuleDebugLevels.GetEntriesFast(); i++)
141 {
6ab674bd 142 if (fModuleDebugLevels[i]) fModuleDebugLevels[i]->Delete();
143 }
6d7793cf 144
6ab674bd 145 fClassDebugLevels.Delete();
6d7793cf 146
147 for (Int_t i = 0; i < fClassDebugLevels.GetEntriesFast(); i++)
148 {
6ab674bd 149 if (fClassDebugLevels[i]) fClassDebugLevels[i]->Delete();
150 }
6d7793cf 151
6ab674bd 152 fClassDebugLevels.Delete();
153
6d7793cf 154 for (Int_t iType = kFatal; iType < kMaxType; iType++)
155 {
6ab674bd 156 CloseFile(iType);
157 }
6d7793cf 158
6ab674bd 159 fflush(stderr);
160 fflush(stdout);
161
162 fgInstance = NULL;
163}
164
6d7793cf 165// NOT IMPLEMENTED!?
6ab674bd 166//_____________________________________________________________________________
167AliLog::AliLog(const AliLog& log) :
90e48c0c 168 TObject(log),
169 fGlobalLogLevel(log.fGlobalLogLevel),
170 fModuleDebugLevels(log.fModuleDebugLevels),
171 fClassDebugLevels(log.fClassDebugLevels),
172 fPrintRepetitions(log.fPrintRepetitions),
173 fRepetitions(log.fRepetitions),
174 fLastType(log.fLastType),
175 fLastMessage(log.fLastMessage),
176 fLastModule(log.fLastModule),
177 fLastClassName(log.fLastClassName),
178 fLastFunction(log.fLastFunction),
179 fLastFile(log.fLastFile),
180 fLastLine(log.fLastLine)
6ab674bd 181{
182// copy constructor
183
184 Fatal("AliLog", "copy constructor not implemented");
185}
186
6d7793cf 187// NOT IMPLEMENTED!?
6ab674bd 188//_____________________________________________________________________________
189AliLog& AliLog::operator = (const AliLog& /*log*/)
190{
191// assignment operator
192
193 Fatal("operator =", "assignment operator not implemented");
194 return *this;
195}
196
197
6d7793cf 198/**
199 * gSystem see TSystem.h
200 * gEnv see TEnv.h
201 *
202 * LOG_NO_DEBUG: fgDebugEnabled <- false
203 * AliRoot.AliLog.EnableDebug
204 * AliRoot.AliLog.GlobalLogLevel
205 */
6ab674bd 206//_____________________________________________________________________________
eeb769e2 207void AliLog::ReadEnvSettings()
208{
209// load settings from the root configuration file (.rootrc)
210// and from environment variables
211
6d7793cf 212 static const char* typeNames[kMaxType] = {"kFatal", "kError", "kWarning", "kInfo", "kDebug"};
eeb769e2 213
214 // debug en- or disabling
6d7793cf 215 if (gSystem->Getenv("LOG_NO_DEBUG"))
216 {
eeb769e2 217 fgDebugEnabled = kFALSE;
6d7793cf 218 }
219 else if (gEnv->Defined("AliRoot.AliLog.EnableDebug"))
220 {
221 fgDebugEnabled = gEnv->GetValue("AliRoot.AliLog.EnableDebug", fgDebugEnabled);
eeb769e2 222 AliInfo(Form("debug %sabled", ((fgDebugEnabled) ? "en" : "dis")));
223 }
224
225 // global log level
6d7793cf 226 if (gEnv->Defined("AliRoot.AliLog.GlobalLogLevel"))
227 {
eeb769e2 228 const char* type = gEnv->GetValue("AliRoot.AliLog.GlobalLogLevel", "");
6d7793cf 229
230 for (Int_t iType = kFatal; iType < kMaxType; iType++)
231 {
eeb769e2 232 if (strcmp(type, typeNames[iType]) == 0) fGlobalLogLevel = iType;
233 }
6d7793cf 234
eeb769e2 235 AliDebug(3, Form("global log level set to %d", fGlobalLogLevel));
236 }
237
238 // global debug level
6d7793cf 239 if (gEnv->Defined("AliRoot.AliLog.GlobalDebugLevel"))
240 {
241 Int_t level = gEnv->GetValue("AliRoot.AliLog.GlobalDebugLevel", Int_t(fGlobalLogLevel - kDebugOffset));
eeb769e2 242 if (level < -kDebugOffset) level = kDebugOffset;
243 fGlobalLogLevel = kDebugOffset + level;
6d7793cf 244 AliDebug(3, Form("global debug level set to %d", fGlobalLogLevel - kDebugOffset));
eeb769e2 245 }
246
247 // module debug level
6d7793cf 248 if (gEnv->Defined("AliRoot.AliLog.ModuleDebugLevel"))
249 {
eeb769e2 250 TString levels = gEnv->GetValue("AliRoot.AliLog.ModuleDebugLevel", "");
251 char* p = const_cast<char*>(levels.Data());
6d7793cf 252
253 while (const char* module = strtok(p, " "))
254 {
eeb769e2 255 p = NULL;
12f04812 256 char* pos = const_cast<char*>(index(module, ':'));
eeb769e2 257 if (!pos) continue;
258 *(pos++) = '\0';
259 Int_t level = atoi(pos);
260 SetModuleDebugLevel(module, level);
261 AliDebug(3, Form("debug level for module %s set to %d", module, level));
262 }
263 }
264
265 // class debug level
6d7793cf 266 if (gEnv->Defined("AliRoot.AliLog.ClassDebugLevel"))
267 {
eeb769e2 268 TString levels = gEnv->GetValue("AliRoot.AliLog.ClassDebugLevel", "");
269 char* p = const_cast<char*>(levels.Data());
6d7793cf 270
271 while (const char* className = strtok(p, " "))
272 {
eeb769e2 273 p = NULL;
12f04812 274 char* pos = const_cast<char*>(index(className, ':'));
eeb769e2 275 if (!pos) continue;
276 *(pos++) = '\0';
277 Int_t level = atoi(pos);
278 SetClassDebugLevel(className, level);
6d7793cf 279 AliDebug(3, Form("debug level for class %s set to %d", className, level));
eeb769e2 280 }
281 }
282
283 // general output stream
6d7793cf 284 if (gEnv->Defined("AliRoot.AliLog.Output"))
285 {
eeb769e2 286 TString stream = gEnv->GetValue("AliRoot.AliLog.Output", "Standard");
6d7793cf 287
288 if (stream.CompareTo("standard", TString::kIgnoreCase) == 0)
289 {
eeb769e2 290 SetStandardOutput();
291 AliDebug(3, "output stream set to standard output for all types");
6d7793cf 292 }
293 else if (stream.CompareTo("error", TString::kIgnoreCase) == 0)
294 {
eeb769e2 295 SetErrorOutput();
296 AliDebug(3, "output stream set to error output for all types");
6d7793cf 297 }
298 else if (!stream.IsNull())
299 {
eeb769e2 300 SetFileOutput(stream);
6d7793cf 301 AliDebug(3, Form("output stream set to file %s for all types", stream.Data()));
eeb769e2 302 }
303 }
304
305 // individual output streams
6d7793cf 306 for (Int_t iType = kFatal; iType < kMaxType; iType++)
307 {
eeb769e2 308 TString name("AliRoot.AliLog.Output.");
309 name += &typeNames[iType][1];
6d7793cf 310
311 if (gEnv->Defined(name))
312 {
eeb769e2 313 TString stream = gEnv->GetValue(name, "Standard");
6d7793cf 314
315 if (stream.CompareTo("standard", TString::kIgnoreCase) == 0)
316 {
44ce6bbe 317 SetStandardOutput(EType_t(iType));
6d7793cf 318 AliDebug(3, Form("output stream set to standard output for type %s", typeNames[iType]));
319 }
320 else if (stream.CompareTo("error", TString::kIgnoreCase) == 0)
321 {
44ce6bbe 322 SetErrorOutput(EType_t(iType));
6d7793cf 323 AliDebug(3, Form("output stream set to error output for type %s", typeNames[iType]));
324 }
325 else if (!stream.IsNull())
326 {
44ce6bbe 327 SetFileOutput(EType_t(iType), stream);
6d7793cf 328 AliDebug(3, Form("output stream set to file %s for type %s", stream.Data(), typeNames[iType]));
eeb769e2 329 }
330 }
331 }
332
333 // handling of root error messages
6d7793cf 334 if (gEnv->Defined("AliRoot.AliLog.HandleRootMessages"))
335 {
eeb769e2 336 Bool_t on = gEnv->GetValue("AliRoot.AliLog.HandleRootMessages", kTRUE);
337 SetHandleRootMessages(on);
6d7793cf 338 AliDebug(3, Form("handling of root messages %sabled", ((on) ? "en" : "dis")));
eeb769e2 339 }
340
341 // printout settings
6d7793cf 342 static const char* settingNames[4] = {"Type", "Module", "Scope", "Location"};
343 Bool_t* settings[] = {fPrintType, fPrintModule, fPrintScope, fPrintLocation};
344
345 for (Int_t iSetting = 0; iSetting < 4; iSetting++)
346 {
eeb769e2 347 TString name("AliRoot.AliLog.Print");
348 name += settingNames[iSetting];
6d7793cf 349
350 if (gEnv->Defined(name))
351 {
eeb769e2 352 Bool_t on = gEnv->GetValue(name, settings[iSetting][0]);
6d7793cf 353
354 for (Int_t iType = kFatal; iType < kMaxType; iType++)
355 {
eeb769e2 356 settings[iSetting][iType] = on;
357 }
6d7793cf 358 AliDebug(3, Form("printing of %s %sabled for all types", settingNames[iSetting], ((on) ? "en" : "dis")));
eeb769e2 359 }
360
6d7793cf 361 for (Int_t iType = kFatal; iType < kMaxType; iType++)
362 {
eeb769e2 363 TString nameType = name + "." + &typeNames[iType][1];
6d7793cf 364
365 if (gEnv->Defined(nameType))
366 {
eeb769e2 367 Bool_t on = gEnv->GetValue(nameType, settings[iSetting][iType]);
368 settings[iSetting][iType] = on;
6d7793cf 369 AliDebug(3, Form("printing of %s %sabled for type %s", settingNames[iSetting], ((on) ? "en" : "dis"), typeNames[iType]));
eeb769e2 370 }
371 }
372 }
45fa0297 373
374 // repetition of messages
6d7793cf 375 if (gEnv->Defined("AliRoot.AliLog.PrintRepetitions"))
376 {
45fa0297 377 Bool_t on = gEnv->GetValue("AliRoot.AliLog.PrintRepetitions", kTRUE);
378 fPrintRepetitions = on;
6d7793cf 379 AliDebug(3, Form("printing of message repetitions %sabled", ((on) ? "en" : "dis")));
45fa0297 380 }
cd4f3d22 381 if (gSystem->Getenv("ALIROOT_FORCE_COREDUMP")){
382 EnableCoreDump(kTRUE);
383 }
384
eeb769e2 385}
386
387
388//_____________________________________________________________________________
6ab674bd 389void AliLog::RootErrorHandler(Int_t level, Bool_t abort,
390 const char* location, const char* message)
391{
392// new error handler for messages from root
393
6d7793cf 394 switch (level)
395 {
6ab674bd 396 case ::kFatal : level = kFatal; break;
397 case ::kSysError :
398 DefaultErrorHandler(level, abort, location, message);
399 return;
400 case ::kBreak :
401 DefaultErrorHandler(level, abort, location, message);
402 return;
403 case ::kError : level = kError; break;
404 case ::kWarning : level = kWarning; break;
405 case ::kInfo : level = kInfo; break;
406 default : level = kDebug; break;
407 }
408 AliLog::Message(level, message, "ROOT", NULL, location, NULL, 0);
409}
410
411
6d7793cf 412// DEPRECATED: USE A CONFIGURATION FILE INSTEAD
6ab674bd 413//_____________________________________________________________________________
414void AliLog::EnableDebug(Bool_t enabled)
415{
416// enable or disable debug output
417
418 fgDebugEnabled = enabled;
419}
420
55247085 421void AliLog::EnableCoreDump(Bool_t enabled)
422{
423// enable or disable debug output
424
425 fgCoreEnabled = enabled;
426 gSystem->ResetSignal(kSigFloatingException,enabled);
427 gSystem->ResetSignal(kSigSegmentationViolation,enabled);
428
429
430}
431
432
433
6ab674bd 434//_____________________________________________________________________________
44ce6bbe 435void AliLog::SetGlobalLogLevel(EType_t type)
6ab674bd 436{
437// set the global debug level
438
6d7793cf 439 // TO BE DELETED
6ab674bd 440 if (!fgInstance) new AliLog;
441 fgInstance->fGlobalLogLevel = type;
442}
443
444//_____________________________________________________________________________
445Int_t AliLog::GetGlobalLogLevel()
446{
447// get the global debug level
448
449 if (!fgInstance) new AliLog;
450 return fgInstance->fGlobalLogLevel;
451}
452
453//_____________________________________________________________________________
454void AliLog::SetGlobalDebugLevel(Int_t level)
455{
456// set the global debug level
457
458 if (!fgInstance) new AliLog;
459 if (level < -kDebugOffset) level = -kDebugOffset;
460 fgInstance->fGlobalLogLevel = kDebugOffset + level;
461}
462
463//_____________________________________________________________________________
464Int_t AliLog::GetGlobalDebugLevel()
465{
466// get the global debug level
467
468 if (!fgInstance) new AliLog;
469 return fgInstance->fGlobalLogLevel - kDebugOffset;
470}
471
472//_____________________________________________________________________________
473void AliLog::SetModuleDebugLevel(const char* module, Int_t level)
474{
475// set the debug level for the given module
476
477 if (!module) return;
478 if (!fgInstance) new AliLog;
479 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
480 if (!obj) {
481 obj = new TNamed(module, module);
482 fgInstance->fModuleDebugLevels.Add(obj);
483 }
484 level += kDebugOffset;
485 if (level < kFatal) level = kFatal;
486 obj->SetUniqueID(level);
487}
488
489//_____________________________________________________________________________
490void AliLog::ClearModuleDebugLevel(const char* module)
491{
492// remove the setting of the debug level for the given module
493
494 if (!module) return;
495 if (!fgInstance) new AliLog;
496 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
497 if (obj) delete fgInstance->fModuleDebugLevels.Remove(obj);
498}
499
500//_____________________________________________________________________________
501void AliLog::SetClassDebugLevel(const char* className, Int_t level)
502{
503// set the debug level for the given class
504
505 if (!className) return;
506 if (!fgInstance) new AliLog;
507 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
508 if (!obj) {
509 obj = new TNamed(className, className);
510 fgInstance->fClassDebugLevels.Add(obj);
511 }
512 level += kDebugOffset;
513 if (level < kFatal) level = kFatal;
514 obj->SetUniqueID(level);
515}
516
517//_____________________________________________________________________________
518void AliLog::ClearClassDebugLevel(const char* className)
519{
520// remove the setting of the debug level for the given class
521
522 if (!className) return;
523 if (!fgInstance) new AliLog;
524 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
525 if (obj) delete fgInstance->fClassDebugLevels.Remove(obj);
526}
527
528
529//_____________________________________________________________________________
530void AliLog::SetStandardOutput()
531{
532// write all log messages to the standard output (stdout)
533
534 if (!fgInstance) new AliLog;
535 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
536 fgInstance->CloseFile(iType);
537 fgInstance->fOutputTypes[iType] = 0;
538 }
539}
540
541//_____________________________________________________________________________
44ce6bbe 542void AliLog::SetStandardOutput(EType_t type)
6ab674bd 543{
544// write log messages of the given type to the standard output (stdout)
545
546 if ((type < kFatal) || (type >= kMaxType)) return;
547 if (!fgInstance) new AliLog;
548 fgInstance->CloseFile(type);
549 fgInstance->fOutputTypes[type] = 0;
550}
551
552//_____________________________________________________________________________
553void AliLog::SetErrorOutput()
554{
555// write all log messages to the error output (stderr)
556
557 if (!fgInstance) new AliLog;
558 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
559 fgInstance->CloseFile(iType);
560 fgInstance->fOutputTypes[iType] = 1;
561 }
562}
563
564//_____________________________________________________________________________
44ce6bbe 565void AliLog::SetErrorOutput(EType_t type)
6ab674bd 566{
567// write log messages of the given type to the error output (stderr)
568
569 if ((type < kFatal) || (type >= kMaxType)) return;
570 if (!fgInstance) new AliLog;
571 fgInstance->CloseFile(type);
572 fgInstance->fOutputTypes[type] = 1;
573}
574
575//_____________________________________________________________________________
576void AliLog::SetFileOutput(const char* fileName)
577{
578// write all log messages to the given file
579
580 if (!fgInstance) new AliLog;
581 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
051599bf 582 if ((fgInstance->fOutputTypes[iType] == 2) &&
6ab674bd 583 (fgInstance->fFileNames[iType].CompareTo(fileName) != 0)) {
584 fgInstance->CloseFile(iType);
585 }
586 fgInstance->fOutputTypes[iType] = 2;
587 fgInstance->fFileNames[iType] = fileName;
588 fgInstance->fOutputFiles[iType] = NULL;
eeb769e2 589 fgInstance->fOutputStreams[iType] = NULL;
6ab674bd 590 }
591}
592
593//_____________________________________________________________________________
44ce6bbe 594void AliLog::SetFileOutput(EType_t type, const char* fileName)
6ab674bd 595{
596// write log messages of the given type to the given file
597
598 if ((type < kFatal) || (type >= kMaxType)) return;
599 if (!fgInstance) new AliLog;
051599bf 600 if ((fgInstance->fOutputTypes[type] == 2) &&
6ab674bd 601 (fgInstance->fFileNames[type].CompareTo(fileName) != 0)) {
602 fgInstance->CloseFile(type);
603 }
604 fgInstance->fOutputTypes[type] = 2;
605 fgInstance->fFileNames[type] = fileName;
606 fgInstance->fOutputFiles[type] = NULL;
eeb769e2 607 fgInstance->fOutputStreams[type] = NULL;
6ab674bd 608}
609
610//_____________________________________________________________________________
611void AliLog::CloseFile(Int_t type)
612{
613// close the file for the given type if needed
614
615 if ((fOutputTypes[type] == 2) && fOutputFiles[type]) {
616 Bool_t closeFile = kTRUE;
617 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
618 if ((iType != type) && (fOutputFiles[iType] == fOutputFiles[type])) {
619 closeFile = kFALSE;
620 }
621 }
eeb769e2 622 if (closeFile) {
623 fclose(fOutputFiles[type]);
1948744e 624 ofstream* stream=reinterpret_cast<ofstream*>(fOutputStreams[type]);
625 stream->close();
eeb769e2 626 delete fOutputStreams[type];
627 }
6ab674bd 628 }
629 fOutputFiles[type] = NULL;
eeb769e2 630 fOutputStreams[type] = NULL;
6ab674bd 631 fFileNames[type] = "";
632 fOutputTypes[type] = 0;
633}
634
635//_____________________________________________________________________________
636FILE* AliLog::GetOutputStream(Int_t type)
637{
638// get the output stream for the given type of messages
639
eeb769e2 640 if (type > kDebug) type = kDebug;
6ab674bd 641 if (fOutputTypes[type] == 0) return stdout;
642 else if (fOutputTypes[type] == 1) return stderr;
643 else if (fOutputTypes[type] == 2) {
644 if (!fOutputFiles[type]) {
645 FILE* file = NULL;
1948744e 646 ostream* stream = NULL;
6ab674bd 647 if (!fFileNames[type].IsNull()) {
648 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
649 if ((iType != type) &&
650 (fFileNames[iType].CompareTo(fFileNames[type]) == 0) &&
651 fOutputFiles[iType]) {
652 file = fOutputFiles[iType];
eeb769e2 653 stream = fOutputStreams[iType];
6ab674bd 654 break;
655 }
656 }
eeb769e2 657 if (!file) {
658 file = fopen(fFileNames[type], "a");
659 stream = new ofstream(fFileNames[type], ios::app);
660 }
6ab674bd 661 }
662 fOutputFiles[type] = file;
eeb769e2 663 fOutputStreams[type] = stream;
6ab674bd 664 if (!file) CloseFile(type);
665 }
666 if (fOutputFiles[type]) return fOutputFiles[type];
667 }
668
669 return stdout;
670}
671
672//_____________________________________________________________________________
673void AliLog::Flush()
674{
675// flush the output streams
676
677 if (!fgInstance) new AliLog;
678 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
679 if (fgInstance->fOutputFiles[iType]) {
680 fflush(fgInstance->fOutputFiles[iType]);
eeb769e2 681 fgInstance->fOutputStreams[iType]->flush();
6ab674bd 682 }
683 }
684 fflush(stderr);
685 fflush(stdout);
686}
687
688
689//_____________________________________________________________________________
690void AliLog::SetHandleRootMessages(Bool_t on)
691{
692// enable or disable the handling of messages form root
693
45fa0297 694 if (!fgInstance) new AliLog;
6ab674bd 695 if (on) {
696 SetErrorHandler(RootErrorHandler);
697 } else {
698 SetErrorHandler(DefaultErrorHandler);
699 }
700}
701
702
703//_____________________________________________________________________________
704void AliLog::SetPrintType(Bool_t on)
705{
706// switch on or off the printing of the message type for all message types
707
708 if (!fgInstance) new AliLog;
709 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
710 fgInstance->fPrintType[iType] = on;
711 }
712}
713
714//_____________________________________________________________________________
44ce6bbe 715void AliLog::SetPrintType(EType_t type, Bool_t on)
6ab674bd 716{
717// switch on or off the printing of the message type for the given message type
718
719 if ((type < kFatal) || (type >= kMaxType)) return;
720 if (!fgInstance) new AliLog;
721 fgInstance->fPrintType[type] = on;
722}
723
724//_____________________________________________________________________________
725void AliLog::SetPrintModule(Bool_t on)
726{
727// switch on or off the printing of the module for all message types
728
729 if (!fgInstance) new AliLog;
730 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
731 fgInstance->fPrintModule[iType] = on;
732 }
733}
734
735//_____________________________________________________________________________
44ce6bbe 736void AliLog::SetPrintModule(EType_t type, Bool_t on)
6ab674bd 737{
738// switch on or off the printing of the module for the given message type
739
740 if ((type < kFatal) || (type >= kMaxType)) return;
741 if (!fgInstance) new AliLog;
742 fgInstance->fPrintModule[type] = on;
743}
744
745//_____________________________________________________________________________
746void AliLog::SetPrintScope(Bool_t on)
747{
748// switch on or off the printing of the scope/class name for all message types
749
750 if (!fgInstance) new AliLog;
751 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
752 fgInstance->fPrintScope[iType] = on;
753 }
754}
755
756//_____________________________________________________________________________
44ce6bbe 757void AliLog::SetPrintScope(EType_t type, Bool_t on)
6ab674bd 758{
759// switch on or off the printing of the scope/class name
760// for the given message type
761
762 if ((type < kFatal) || (type >= kMaxType)) return;
763 if (!fgInstance) new AliLog;
764 fgInstance->fPrintScope[type] = on;
765}
766
767//_____________________________________________________________________________
768void AliLog::SetPrintLocation(Bool_t on)
769{
770// switch on or off the printing of the file name and line number
771// for all message types
772
773 if (!fgInstance) new AliLog;
774 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
775 fgInstance->fPrintLocation[iType] = on;
776 }
777}
778
779//_____________________________________________________________________________
44ce6bbe 780void AliLog::SetPrintLocation(EType_t type, Bool_t on)
6ab674bd 781{
782// switch on or off the printing of the file name and line number
783// for the given message type
784
785 if ((type < kFatal) || (type >= kMaxType)) return;
786 if (!fgInstance) new AliLog;
787 fgInstance->fPrintLocation[type] = on;
788}
789
790
791//_____________________________________________________________________________
45fa0297 792void AliLog::SetPrintRepetitions(Bool_t on)
793{
794// switch on or off the printing of the number of repetitions of a message
795// instead of repeating the same message
796
797 if (!fgInstance) new AliLog;
798 if (!on && (fgInstance->fRepetitions > 0)) fgInstance->PrintRepetitions();
799 fgInstance->fPrintRepetitions = on;
800}
801
802
803//_____________________________________________________________________________
c4cb6153 804void AliLog::WriteToFile(const char* name, Int_t option)
6ab674bd 805{
806// write the log object with the given name and option to the current file
807
808 if (!fgInstance) new AliLog;
809 fgInstance->TObject::Write(name, option);
810}
811
812
813//_____________________________________________________________________________
814UInt_t AliLog::GetLogLevel(const char* module, const char* className) const
815{
816// get the logging level for the given module and class
817
818 if (!fgInstance) new AliLog;
21bf76e1 819 if (className) {
820 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
821 if (obj) return obj->GetUniqueID();
822 }
823 if (module) {
824 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
825 if (obj) return obj->GetUniqueID();
826 }
6ab674bd 827 return fgInstance->fGlobalLogLevel;
828}
829
830//_____________________________________________________________________________
831Int_t AliLog::GetDebugLevel(const char* module, const char* className)
832{
833// get the debug level for the given module and class
834
835 if (!fgInstance) new AliLog;
836 return fgInstance->GetLogLevel(module, className) - kDebugOffset;
837}
838
839//_____________________________________________________________________________
eeb769e2 840void AliLog::PrintMessage(UInt_t type, const char* message,
841 const char* module, const char* className,
842 const char* function, const char* file, Int_t line)
843{
844// print the given message
845
45fa0297 846 // don't print the message if it is repeated
847 if (fPrintRepetitions &&
848 (fLastType == type) &&
849 (message && (fLastMessage.CompareTo(message) == 0)) &&
850 ((module && (fLastModule.CompareTo(module) == 0)) ||
851 (!module && fLastModule.IsNull())) &&
852 ((className && (fLastClassName.CompareTo(className) == 0)) ||
853 (!className && fLastClassName.IsNull())) &&
854 ((function && (fLastFunction.CompareTo(function) == 0)) ||
855 (!function && fLastFunction.IsNull()))&&
856 ((file && (fLastFile.CompareTo(file) == 0)) ||
857 (!file && fLastFile.IsNull())) &&
858 (fLastLine == line)) {
859 fRepetitions++;
860 return;
861 }
862
863 // print number of repetitions
864 if (fRepetitions > 0) PrintRepetitions();
865
866 // remember this message
867 fRepetitions = 0;
868 fLastType = type;
869 fLastMessage = message;
870 fLastModule = module;
871 fLastClassName = className;
872 fLastFunction = function;
873 fLastFile = file;
874 fLastLine = line;
875
876 // print the message
eeb769e2 877 FILE* stream = GetOutputStream(type);
878 static const char* typeNames[kMaxType] =
879 {"Fatal", "Error", "Warning", "Info", "Debug"};
880
881 if (fPrintType[type]) {
1948744e 882 PrintString(type, stream, "%c-", typeNames[type][0]);
eeb769e2 883 }
eeb769e2 884 if (fPrintModule[type] && module) {
1948744e 885 PrintString(type, stream, "%s/", module);
eeb769e2 886 }
887 if (fPrintScope[type] && className) {
1948744e 888 PrintString(type, stream, "%s::", className);
eeb769e2 889 }
890 if (message) {
1948744e 891 PrintString(type, stream, "%s: %s", function, message);
eeb769e2 892 } else {
1948744e 893 PrintString(type, stream, "%s", function);
eeb769e2 894 }
895 if (fPrintLocation[type] && file) {
1948744e 896 PrintString(type, stream, " (%s:%.0d)", file, line);
eeb769e2 897 }
898 if (message) {
1948744e 899 PrintString(type, stream, "\n");
eeb769e2 900 } else {
1948744e 901 PrintString(type, stream, ": ");
eeb769e2 902 }
1948744e 903 if (fCallBacks[type]) (*(fCallBacks[type]))((EType_t)type, NULL);
eeb769e2 904}
905
906//_____________________________________________________________________________
45fa0297 907void AliLog::PrintRepetitions()
908{
909// print number of repetitions
910
1948744e 911 PrintString(fLastType, GetOutputStream(fLastType), " <message repeated %d time%s>\n",
45fa0297 912 fRepetitions, (fRepetitions > 1) ? "s" : "");
1948744e 913 if (fCallBacks[fLastType]) (*(fCallBacks[fLastType]))((EType_t)fLastType, NULL);
45fa0297 914}
915
916//_____________________________________________________________________________
6ab674bd 917void AliLog::Message(UInt_t level, const char* message,
918 const char* module, const char* className,
919 const char* function, const char* file, Int_t line)
920{
921// print a log message
922
923 if (!fgInstance) new AliLog;
924
925 // get the message type
6ab674bd 926 UInt_t type = level;
927 if (type >= kMaxType) type = kMaxType - 1;
928
929 // print the message if the debug level allows
930 if (level <= fgInstance->GetLogLevel(module, className)) {
eeb769e2 931 fgInstance->PrintMessage(type, message,
932 module, className, function, file, line);
6ab674bd 933 }
934
935 // abort in case of a fatal message
936 if (type == kFatal) {
937 delete fgInstance;
938 if (gSystem) {
939 gSystem->StackTrace();
55247085 940 if (fgCoreEnabled) MakeCoreDump("core.AliRoot");
6ab674bd 941 gSystem->Abort();
942 } else {
55247085 943 if (fgCoreEnabled) MakeCoreDump("core.AliRoot");
6ab674bd 944 ::abort();
945 }
946 }
947}
948
55247085 949
950
6ab674bd 951//_____________________________________________________________________________
952void AliLog::Debug(UInt_t level, const char* message,
953 const char* module, const char* className,
954 const char* function, const char* file, Int_t line)
955{
956// print a debug message
957
958 if (level == 0) level = 1;
959 level += kDebugOffset;
960 Message(level, message, module, className, function, file, line);
961}
eeb769e2 962
963
964//_____________________________________________________________________________
44ce6bbe 965Int_t AliLog::RedirectStdoutTo(EType_t type, UInt_t level, const char* module,
eeb769e2 966 const char* className, const char* function,
967 const char* file, Int_t line, Bool_t print)
968{
969// redirect the standard output to the stream of the given type
970
971 if (!fgInstance) new AliLog;
972 return fgInstance->RedirectTo(stdout, type, level, module, className,
973 function, file, line, print);
974}
975
976//_____________________________________________________________________________
44ce6bbe 977Int_t AliLog::RedirectStderrTo(EType_t type, UInt_t level, const char* module,
eeb769e2 978 const char* className, const char* function,
979 const char* file, Int_t line, Bool_t print)
980{
981// redirect the standard error output to the stream of the given type
982
983 if (!fgInstance) new AliLog;
984 return fgInstance->RedirectTo(stderr, type, level, module, className,
985 function, file, line, print);
986}
987
988//_____________________________________________________________________________
44ce6bbe 989Int_t AliLog::RedirectTo(FILE* stream, EType_t type, UInt_t level,
eeb769e2 990 const char* module, const char* className,
991 const char* function, const char* file, Int_t line,
992 Bool_t print)
993{
994// redirect the standard (error) output stream to the stream of the given type
995
996 // get the original file descriptor to be able to restore it later
997 Int_t original = dup(fileno(stream));
998 fflush(stream);
999
1000 // flush the stream of the selected type
1001 FILE* newStream = GetOutputStream(type);
1002 fflush(newStream);
1003
1004 // redirect stream
1005 if ((type == kDebug) && (level > 0)) level--;
1006 if (type + level > GetLogLevel(module, className)) { // /dev/null
17ef8526 1007 if(!freopen("/dev/null", "a", stream)) AliWarning("Cannot reopen /dev/null");
eeb769e2 1008 } else if (fOutputTypes[type] == 0) { // stdout
1009 if (stream != stdout) dup2(fileno(stdout), fileno(stream));
1010 } else if (fOutputTypes[type] == 1) { // stderr
1011 if (stream != stderr) dup2(fileno(stderr), fileno(stream));
1012 } else if (fOutputTypes[type] == 2) { // file
17ef8526 1013 if(!freopen(fFileNames[type], "a", stream)) AliWarning(Form("Cannot reopen %s",fFileNames[type].Data()));
1948744e 1014 } else if (fOutputTypes[type] == 3) { // external C++ stream
1015 // redirection is not possible for external C++ streams
eeb769e2 1016 }
1017
1018 // print information
1019 if (print) {
1020 PrintMessage(type, NULL, module, className, function, file, line);
1021 fflush(newStream);
1022 }
1023
1024 return original;
1025}
1026
1027//_____________________________________________________________________________
1028void AliLog::RestoreStdout(Int_t original)
1029{
1030// restore the standard output
1031
1032 fflush(stdout);
1033 dup2(original, fileno(stdout));
b6f30bf9 1034 close(original);
eeb769e2 1035}
1036
1037//_____________________________________________________________________________
1038void AliLog::RestoreStderr(Int_t original)
1039{
1040// restore the standard error output
1041
1042 fflush(stderr);
1043 dup2(original, fileno(stderr));
b6f30bf9 1044 close(original);
eeb769e2 1045}
1046
1047
1048//_____________________________________________________________________________
44ce6bbe 1049ostream& AliLog::Stream(EType_t type, UInt_t level,
eeb769e2 1050 const char* module, const char* className,
1051 const char* function, const char* file, Int_t line)
1052{
1053// get the stream object for the given output type
1054
1055 if (!fgInstance) new AliLog;
1056 return fgInstance->GetStream(type, level, module, className,
1057 function, file, line);
1058}
1059
1060//_____________________________________________________________________________
44ce6bbe 1061ostream& AliLog::GetStream(EType_t type, UInt_t level,
eeb769e2 1062 const char* module, const char* className,
1063 const char* function, const char* file, Int_t line)
1064{
1065// get the stream object for the given output type
1066
1067 if ((type == kDebug) && (level > 0)) level--;
1068 Bool_t noOutput = (type + level > GetLogLevel(module, className));
1069
1070 if (!noOutput) {
1071 PrintMessage(type, NULL, module, className, function, file, line);
1072 }
1073 fflush(GetOutputStream(type));
1074
1075 static ofstream nullStream("/dev/null");
1076 if (noOutput) {
1077 return nullStream;
1078 } else if (fOutputTypes[type] == 0) {
1079 return cout;
1080 } else if (fOutputTypes[type] == 1) {
1081 return cerr;
1082 } else if (fOutputTypes[type] == 2) {
1083 return *fOutputStreams[type];
1948744e 1084 } else if (fOutputTypes[type] == 3) {
1085 return *fOutputStreams[type];
eeb769e2 1086 }
1087
1088 return nullStream;
1089}
1090
1948744e 1091void AliLog::SetStreamOutput(ostream* stream)
1092{
1093 // set an external stream as target for log messages of all types
1094 // the external stream is completely handled by the caller, the
1095 // AliLog class just writes to it
1096
1097 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
1098 SetStreamOutput((AliLog::EType_t)iType, stream);
1099 }
1100}
1101
1102void AliLog::SetStreamOutput(EType_t type, ostream* stream)
1103{
1104 // set an external stream as target for log messages of the given type
1105 // the external stream is completely handled by the caller, the
1106 // AliLog class just writes to it
1107
1108 if ((type < kFatal) || (type >= kMaxType)) return;
1109 if (!fgInstance) new AliLog;
1110 if (fgInstance->fOutputTypes[type] == 2) {
1111 fgInstance->CloseFile(type);
1112 }
1113 fgInstance->fOutputTypes[type] = 3;
1114 fgInstance->fFileNames[type] = "";
1115 fgInstance->fOutputFiles[type] = NULL;
1116 fgInstance->fOutputStreams[type] = stream;
1117}
1118
1119void AliLog::SetLogNotification(AliLogNotification pCallBack)
1120{
1121 // set a notification callback function for log messages of all types
1122
1123 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
1124 SetLogNotification((AliLog::EType_t)iType, pCallBack);
1125 }
1126}
1127
1128void AliLog::SetLogNotification(EType_t type, AliLogNotification pCallBack)
1129{
1130 // set a notifications call back function for log messages of all types
1131 // the callback fuction is invoced whenever an output was written
1132 // Note: does not work for c++ streamer classes, the external stream
1133 // has to handle this diectly (e.g. custom implementation of endl)
1134
1135 if ((type < kFatal) || (type >= kMaxType)) return;
1136 if (!fgInstance) new AliLog;
1137 fgInstance->fCallBacks[type]=pCallBack;
1138}
1139
1140void AliLog::PrintString(Int_t type, FILE* stream, const char* format, ...)
1141{
1142 // this is the general method to print a log message using variadac args
1143 // to the FILE* like (C - like) streams, e.g. stdout, stderr, or files
1144 // opened by fopen.
1145 // Only in case of an external c++ ostream type output, the message is
1146 // written to that stream and the notifictaion callback is called.
1147 // The message is printed by a normal vfprintf function otherwise
1148
1149 if (format==NULL) return;
1150
1151 va_list ap;
1152 va_start(ap, format);
1153 if (fOutputTypes[type] != 3) {
1154 if (stream!=NULL) {
1155 vfprintf(stream, format, ap);
1156 }
1157 } else {
1158 // build the string and write everthing to the corresponding ostream
1159 TString fmt(format);
1160 TArrayC tgt(fmt.Length()*10); // just take a number
1161#ifdef R__VA_COPY
1162 va_list bap;
1163 R__VA_COPY(bap, ap);
a7a801dc 1164#else
1165#warning definition of R__VA_COPY has disappeared
1948744e 1166#endif //R__VA_COPY
1167
1168 Int_t iResult=0;
1169 while (1) {
1170 iResult=vsnprintf(tgt.GetArray(), tgt.GetSize(), format, ap);
1171 if (iResult==-1) {
1172 iResult=tgt.GetSize()*2;
1173 } else if (iResult<tgt.GetSize()) {
1174 break;
1175 }
1176#ifdef R__VA_COPY
1177 if (iResult<10000) {
1178 tgt.Set(iResult+1);
1179 va_end(ap);
1180 R__VA_COPY(ap, bap);
1181 } else
1182#endif //R__VA_COPY
1183 {
1184 tgt[tgt.GetSize()-1]=0;
1185 break;
1186 }
1187 }
1188#ifdef R__VA_COPY
1189 va_end(bap);
1190#endif //R__VA_COPY
1191
1192 if (fOutputStreams[type]) {
1193 *(fOutputStreams[type]) << tgt.GetArray();
1194 }
1195 }
1196 va_end(ap);
1197}
55247085 1198
1199
1200void AliLog::MakeCoreDump(const char *fout){
1201 //
1202 // Functionality to make a program snapshot
1203 // gcore - Generate a core file for a running process
1204 // gcore dmake a current snapshot, program can continue further
1205 // We assum that gcore is installed
1206 // for details see: man gcore
1207 //
1208 // Example use - make default core file for current process: AliLog::MakeCoreDump(0)
1209 //
1210 //
1211 // Automatic core dump creation in case of the AliFatal can be specified using
1212 // static void EnableCoreDump(Bool_t enabled);
1213 // Core dump is created in addition to the stack trace ()
1214 // marian.ivanov@cern.ch
1215 //
1216 if (!gSystem) return;
1217 printf("AliLog::MakeCoreDump\n");
1218 if (fout){
1219 gSystem->Exec(Form("gcore -o %s %d",fout, gSystem->GetPid()));
1220 }else{
1221 gSystem->Exec(Form("gcore %d", gSystem->GetPid()));
1222 }
1223}