1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // class for logging debug, info and error messages //
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 //
26 // It also handles the messages produces by the preprocessor macros defined //
27 // in the header file: AliDebug, AliInfo, AliWarning, AliError, AliFatal. //
29 // More details about the message logging can be found on the ALICE Offline //
32 ///////////////////////////////////////////////////////////////////////////////
36 #include <Riostream.h>
42 #include <Varargs.h> // platform independent definition of va_copy
54 // implementation of a singleton here
55 AliLog* AliLog::fgInstance = NULL;
57 Bool_t AliLog::fgDebugEnabled = kTRUE;
60 * get root logger singleton instance
62 AliLog *AliLog::GetRootLogger()
64 if (fgInstance == NULL)
67 fgInstance = new AliLog;
74 * delete the root logger singleton instance
76 void AliLog::DeleteRootLogger()
78 if (fgInstance != NULL)
86 * default private constructor
90 fGlobalLogLevel(kInfo),
93 fPrintRepetitions(kTRUE),
103 // default constructor: set default values
105 for (Int_t iType = kFatal; iType < kMaxType; iType++)
107 fOutputTypes[iType] = 0;
108 fFileNames[iType] = "";
109 fOutputFiles[iType] = NULL;
110 fOutputStreams[iType] = NULL;
111 fCallBacks[iType]=NULL;
113 fPrintType[iType] = kTRUE;
114 fPrintModule[iType] = kFALSE;
115 fPrintScope[iType] = kTRUE;
116 fPrintLocation[iType] = (iType == kDebug);
120 // replace the previous instance by this one
121 if (fgInstance) delete fgInstance;
124 SetHandleRootMessages(kTRUE);
126 // read the .rootrc settings
135 // destructor: clean up and reset instance pointer
137 if (fRepetitions > 0) PrintRepetitions();
139 for (Int_t i = 0; i < fModuleDebugLevels.GetEntriesFast(); i++)
141 if (fModuleDebugLevels[i]) fModuleDebugLevels[i]->Delete();
144 fClassDebugLevels.Delete();
146 for (Int_t i = 0; i < fClassDebugLevels.GetEntriesFast(); i++)
148 if (fClassDebugLevels[i]) fClassDebugLevels[i]->Delete();
151 fClassDebugLevels.Delete();
153 for (Int_t iType = kFatal; iType < kMaxType; iType++)
165 //_____________________________________________________________________________
166 AliLog::AliLog(const AliLog& log) :
168 fGlobalLogLevel(log.fGlobalLogLevel),
169 fModuleDebugLevels(log.fModuleDebugLevels),
170 fClassDebugLevels(log.fClassDebugLevels),
171 fPrintRepetitions(log.fPrintRepetitions),
172 fRepetitions(log.fRepetitions),
173 fLastType(log.fLastType),
174 fLastMessage(log.fLastMessage),
175 fLastModule(log.fLastModule),
176 fLastClassName(log.fLastClassName),
177 fLastFunction(log.fLastFunction),
178 fLastFile(log.fLastFile),
179 fLastLine(log.fLastLine)
183 Fatal("AliLog", "copy constructor not implemented");
187 //_____________________________________________________________________________
188 AliLog& AliLog::operator = (const AliLog& /*log*/)
190 // assignment operator
192 Fatal("operator =", "assignment operator not implemented");
198 * gSystem see TSystem.h
201 * LOG_NO_DEBUG: fgDebugEnabled <- false
202 * AliRoot.AliLog.EnableDebug
203 * AliRoot.AliLog.GlobalLogLevel
205 //_____________________________________________________________________________
206 void AliLog::ReadEnvSettings()
208 // load settings from the root configuration file (.rootrc)
209 // and from environment variables
211 static const char* typeNames[kMaxType] = {"kFatal", "kError", "kWarning", "kInfo", "kDebug"};
213 // debug en- or disabling
214 if (gSystem->Getenv("LOG_NO_DEBUG"))
216 fgDebugEnabled = kFALSE;
218 else if (gEnv->Defined("AliRoot.AliLog.EnableDebug"))
220 fgDebugEnabled = gEnv->GetValue("AliRoot.AliLog.EnableDebug", fgDebugEnabled);
221 AliInfo(Form("debug %sabled", ((fgDebugEnabled) ? "en" : "dis")));
225 if (gEnv->Defined("AliRoot.AliLog.GlobalLogLevel"))
227 const char* type = gEnv->GetValue("AliRoot.AliLog.GlobalLogLevel", "");
229 for (Int_t iType = kFatal; iType < kMaxType; iType++)
231 if (strcmp(type, typeNames[iType]) == 0) fGlobalLogLevel = iType;
234 AliDebug(3, Form("global log level set to %d", fGlobalLogLevel));
237 // global debug level
238 if (gEnv->Defined("AliRoot.AliLog.GlobalDebugLevel"))
240 Int_t level = gEnv->GetValue("AliRoot.AliLog.GlobalDebugLevel", Int_t(fGlobalLogLevel - kDebugOffset));
241 if (level < -kDebugOffset) level = kDebugOffset;
242 fGlobalLogLevel = kDebugOffset + level;
243 AliDebug(3, Form("global debug level set to %d", fGlobalLogLevel - kDebugOffset));
246 // module debug level
247 if (gEnv->Defined("AliRoot.AliLog.ModuleDebugLevel"))
249 TString levels = gEnv->GetValue("AliRoot.AliLog.ModuleDebugLevel", "");
250 char* p = const_cast<char*>(levels.Data());
252 while (const char* module = strtok(p, " "))
255 char* pos = const_cast<char*>(index(module, ':'));
258 Int_t level = atoi(pos);
259 SetModuleDebugLevel(module, level);
260 AliDebug(3, Form("debug level for module %s set to %d", module, level));
265 if (gEnv->Defined("AliRoot.AliLog.ClassDebugLevel"))
267 TString levels = gEnv->GetValue("AliRoot.AliLog.ClassDebugLevel", "");
268 char* p = const_cast<char*>(levels.Data());
270 while (const char* className = strtok(p, " "))
273 char* pos = const_cast<char*>(index(className, ':'));
276 Int_t level = atoi(pos);
277 SetClassDebugLevel(className, level);
278 AliDebug(3, Form("debug level for class %s set to %d", className, level));
282 // general output stream
283 if (gEnv->Defined("AliRoot.AliLog.Output"))
285 TString stream = gEnv->GetValue("AliRoot.AliLog.Output", "Standard");
287 if (stream.CompareTo("standard", TString::kIgnoreCase) == 0)
290 AliDebug(3, "output stream set to standard output for all types");
292 else if (stream.CompareTo("error", TString::kIgnoreCase) == 0)
295 AliDebug(3, "output stream set to error output for all types");
297 else if (!stream.IsNull())
299 SetFileOutput(stream);
300 AliDebug(3, Form("output stream set to file %s for all types", stream.Data()));
304 // individual output streams
305 for (Int_t iType = kFatal; iType < kMaxType; iType++)
307 TString name("AliRoot.AliLog.Output.");
308 name += &typeNames[iType][1];
310 if (gEnv->Defined(name))
312 TString stream = gEnv->GetValue(name, "Standard");
314 if (stream.CompareTo("standard", TString::kIgnoreCase) == 0)
316 SetStandardOutput(EType_t(iType));
317 AliDebug(3, Form("output stream set to standard output for type %s", typeNames[iType]));
319 else if (stream.CompareTo("error", TString::kIgnoreCase) == 0)
321 SetErrorOutput(EType_t(iType));
322 AliDebug(3, Form("output stream set to error output for type %s", typeNames[iType]));
324 else if (!stream.IsNull())
326 SetFileOutput(EType_t(iType), stream);
327 AliDebug(3, Form("output stream set to file %s for type %s", stream.Data(), typeNames[iType]));
332 // handling of root error messages
333 if (gEnv->Defined("AliRoot.AliLog.HandleRootMessages"))
335 Bool_t on = gEnv->GetValue("AliRoot.AliLog.HandleRootMessages", kTRUE);
336 SetHandleRootMessages(on);
337 AliDebug(3, Form("handling of root messages %sabled", ((on) ? "en" : "dis")));
341 static const char* settingNames[4] = {"Type", "Module", "Scope", "Location"};
342 Bool_t* settings[] = {fPrintType, fPrintModule, fPrintScope, fPrintLocation};
344 for (Int_t iSetting = 0; iSetting < 4; iSetting++)
346 TString name("AliRoot.AliLog.Print");
347 name += settingNames[iSetting];
349 if (gEnv->Defined(name))
351 Bool_t on = gEnv->GetValue(name, settings[iSetting][0]);
353 for (Int_t iType = kFatal; iType < kMaxType; iType++)
355 settings[iSetting][iType] = on;
357 AliDebug(3, Form("printing of %s %sabled for all types", settingNames[iSetting], ((on) ? "en" : "dis")));
360 for (Int_t iType = kFatal; iType < kMaxType; iType++)
362 TString nameType = name + "." + &typeNames[iType][1];
364 if (gEnv->Defined(nameType))
366 Bool_t on = gEnv->GetValue(nameType, settings[iSetting][iType]);
367 settings[iSetting][iType] = on;
368 AliDebug(3, Form("printing of %s %sabled for type %s", settingNames[iSetting], ((on) ? "en" : "dis"), typeNames[iType]));
373 // repetition of messages
374 if (gEnv->Defined("AliRoot.AliLog.PrintRepetitions"))
376 Bool_t on = gEnv->GetValue("AliRoot.AliLog.PrintRepetitions", kTRUE);
377 fPrintRepetitions = on;
378 AliDebug(3, Form("printing of message repetitions %sabled", ((on) ? "en" : "dis")));
383 //_____________________________________________________________________________
384 void AliLog::RootErrorHandler(Int_t level, Bool_t abort,
385 const char* location, const char* message)
387 // new error handler for messages from root
391 case ::kFatal : level = kFatal; break;
393 DefaultErrorHandler(level, abort, location, message);
396 DefaultErrorHandler(level, abort, location, message);
398 case ::kError : level = kError; break;
399 case ::kWarning : level = kWarning; break;
400 case ::kInfo : level = kInfo; break;
401 default : level = kDebug; break;
403 AliLog::Message(level, message, "ROOT", NULL, location, NULL, 0);
407 // DEPRECATED: USE A CONFIGURATION FILE INSTEAD
408 //_____________________________________________________________________________
409 void AliLog::EnableDebug(Bool_t enabled)
411 // enable or disable debug output
413 fgDebugEnabled = enabled;
416 //_____________________________________________________________________________
417 void AliLog::SetGlobalLogLevel(EType_t type)
419 // set the global debug level
422 if (!fgInstance) new AliLog;
423 fgInstance->fGlobalLogLevel = type;
426 //_____________________________________________________________________________
427 Int_t AliLog::GetGlobalLogLevel()
429 // get the global debug level
431 if (!fgInstance) new AliLog;
432 return fgInstance->fGlobalLogLevel;
435 //_____________________________________________________________________________
436 void AliLog::SetGlobalDebugLevel(Int_t level)
438 // set the global debug level
440 if (!fgInstance) new AliLog;
441 if (level < -kDebugOffset) level = -kDebugOffset;
442 fgInstance->fGlobalLogLevel = kDebugOffset + level;
445 //_____________________________________________________________________________
446 Int_t AliLog::GetGlobalDebugLevel()
448 // get the global debug level
450 if (!fgInstance) new AliLog;
451 return fgInstance->fGlobalLogLevel - kDebugOffset;
454 //_____________________________________________________________________________
455 void AliLog::SetModuleDebugLevel(const char* module, Int_t level)
457 // set the debug level for the given module
460 if (!fgInstance) new AliLog;
461 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
463 obj = new TNamed(module, module);
464 fgInstance->fModuleDebugLevels.Add(obj);
466 level += kDebugOffset;
467 if (level < kFatal) level = kFatal;
468 obj->SetUniqueID(level);
471 //_____________________________________________________________________________
472 void AliLog::ClearModuleDebugLevel(const char* module)
474 // remove the setting of the debug level for the given module
477 if (!fgInstance) new AliLog;
478 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
479 if (obj) delete fgInstance->fModuleDebugLevels.Remove(obj);
482 //_____________________________________________________________________________
483 void AliLog::SetClassDebugLevel(const char* className, Int_t level)
485 // set the debug level for the given class
487 if (!className) return;
488 if (!fgInstance) new AliLog;
489 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
491 obj = new TNamed(className, className);
492 fgInstance->fClassDebugLevels.Add(obj);
494 level += kDebugOffset;
495 if (level < kFatal) level = kFatal;
496 obj->SetUniqueID(level);
499 //_____________________________________________________________________________
500 void AliLog::ClearClassDebugLevel(const char* className)
502 // remove the setting of the debug level for the given class
504 if (!className) return;
505 if (!fgInstance) new AliLog;
506 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
507 if (obj) delete fgInstance->fClassDebugLevels.Remove(obj);
511 //_____________________________________________________________________________
512 void AliLog::SetStandardOutput()
514 // write all log messages to the standard output (stdout)
516 if (!fgInstance) new AliLog;
517 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
518 fgInstance->CloseFile(iType);
519 fgInstance->fOutputTypes[iType] = 0;
523 //_____________________________________________________________________________
524 void AliLog::SetStandardOutput(EType_t type)
526 // write log messages of the given type to the standard output (stdout)
528 if ((type < kFatal) || (type >= kMaxType)) return;
529 if (!fgInstance) new AliLog;
530 fgInstance->CloseFile(type);
531 fgInstance->fOutputTypes[type] = 0;
534 //_____________________________________________________________________________
535 void AliLog::SetErrorOutput()
537 // write all log messages to the error output (stderr)
539 if (!fgInstance) new AliLog;
540 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
541 fgInstance->CloseFile(iType);
542 fgInstance->fOutputTypes[iType] = 1;
546 //_____________________________________________________________________________
547 void AliLog::SetErrorOutput(EType_t type)
549 // write log messages of the given type to the error output (stderr)
551 if ((type < kFatal) || (type >= kMaxType)) return;
552 if (!fgInstance) new AliLog;
553 fgInstance->CloseFile(type);
554 fgInstance->fOutputTypes[type] = 1;
557 //_____________________________________________________________________________
558 void AliLog::SetFileOutput(const char* fileName)
560 // write all log messages to the given file
562 if (!fgInstance) new AliLog;
563 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
564 if ((fgInstance->fOutputTypes[iType] == 2) &&
565 (fgInstance->fFileNames[iType].CompareTo(fileName) != 0)) {
566 fgInstance->CloseFile(iType);
568 fgInstance->fOutputTypes[iType] = 2;
569 fgInstance->fFileNames[iType] = fileName;
570 fgInstance->fOutputFiles[iType] = NULL;
571 fgInstance->fOutputStreams[iType] = NULL;
575 //_____________________________________________________________________________
576 void AliLog::SetFileOutput(EType_t type, const char* fileName)
578 // write log messages of the given type to the given file
580 if ((type < kFatal) || (type >= kMaxType)) return;
581 if (!fgInstance) new AliLog;
582 if ((fgInstance->fOutputTypes[type] == 2) &&
583 (fgInstance->fFileNames[type].CompareTo(fileName) != 0)) {
584 fgInstance->CloseFile(type);
586 fgInstance->fOutputTypes[type] = 2;
587 fgInstance->fFileNames[type] = fileName;
588 fgInstance->fOutputFiles[type] = NULL;
589 fgInstance->fOutputStreams[type] = NULL;
592 //_____________________________________________________________________________
593 void AliLog::CloseFile(Int_t type)
595 // close the file for the given type if needed
597 if ((fOutputTypes[type] == 2) && fOutputFiles[type]) {
598 Bool_t closeFile = kTRUE;
599 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
600 if ((iType != type) && (fOutputFiles[iType] == fOutputFiles[type])) {
605 fclose(fOutputFiles[type]);
606 ofstream* stream=reinterpret_cast<ofstream*>(fOutputStreams[type]);
608 delete fOutputStreams[type];
611 fOutputFiles[type] = NULL;
612 fOutputStreams[type] = NULL;
613 fFileNames[type] = "";
614 fOutputTypes[type] = 0;
617 //_____________________________________________________________________________
618 FILE* AliLog::GetOutputStream(Int_t type)
620 // get the output stream for the given type of messages
622 if (type > kDebug) type = kDebug;
623 if (fOutputTypes[type] == 0) return stdout;
624 else if (fOutputTypes[type] == 1) return stderr;
625 else if (fOutputTypes[type] == 2) {
626 if (!fOutputFiles[type]) {
628 ostream* stream = NULL;
629 if (!fFileNames[type].IsNull()) {
630 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
631 if ((iType != type) &&
632 (fFileNames[iType].CompareTo(fFileNames[type]) == 0) &&
633 fOutputFiles[iType]) {
634 file = fOutputFiles[iType];
635 stream = fOutputStreams[iType];
640 file = fopen(fFileNames[type], "a");
641 stream = new ofstream(fFileNames[type], ios::app);
644 fOutputFiles[type] = file;
645 fOutputStreams[type] = stream;
646 if (!file) CloseFile(type);
648 if (fOutputFiles[type]) return fOutputFiles[type];
654 //_____________________________________________________________________________
657 // flush the output streams
659 if (!fgInstance) new AliLog;
660 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
661 if (fgInstance->fOutputFiles[iType]) {
662 fflush(fgInstance->fOutputFiles[iType]);
663 fgInstance->fOutputStreams[iType]->flush();
671 //_____________________________________________________________________________
672 void AliLog::SetHandleRootMessages(Bool_t on)
674 // enable or disable the handling of messages form root
676 if (!fgInstance) new AliLog;
678 SetErrorHandler(RootErrorHandler);
680 SetErrorHandler(DefaultErrorHandler);
685 //_____________________________________________________________________________
686 void AliLog::SetPrintType(Bool_t on)
688 // switch on or off the printing of the message type for all message types
690 if (!fgInstance) new AliLog;
691 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
692 fgInstance->fPrintType[iType] = on;
696 //_____________________________________________________________________________
697 void AliLog::SetPrintType(EType_t type, Bool_t on)
699 // switch on or off the printing of the message type for the given message type
701 if ((type < kFatal) || (type >= kMaxType)) return;
702 if (!fgInstance) new AliLog;
703 fgInstance->fPrintType[type] = on;
706 //_____________________________________________________________________________
707 void AliLog::SetPrintModule(Bool_t on)
709 // switch on or off the printing of the module for all message types
711 if (!fgInstance) new AliLog;
712 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
713 fgInstance->fPrintModule[iType] = on;
717 //_____________________________________________________________________________
718 void AliLog::SetPrintModule(EType_t type, Bool_t on)
720 // switch on or off the printing of the module for the given message type
722 if ((type < kFatal) || (type >= kMaxType)) return;
723 if (!fgInstance) new AliLog;
724 fgInstance->fPrintModule[type] = on;
727 //_____________________________________________________________________________
728 void AliLog::SetPrintScope(Bool_t on)
730 // switch on or off the printing of the scope/class name for all message types
732 if (!fgInstance) new AliLog;
733 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
734 fgInstance->fPrintScope[iType] = on;
738 //_____________________________________________________________________________
739 void AliLog::SetPrintScope(EType_t type, Bool_t on)
741 // switch on or off the printing of the scope/class name
742 // for the given message type
744 if ((type < kFatal) || (type >= kMaxType)) return;
745 if (!fgInstance) new AliLog;
746 fgInstance->fPrintScope[type] = on;
749 //_____________________________________________________________________________
750 void AliLog::SetPrintLocation(Bool_t on)
752 // switch on or off the printing of the file name and line number
753 // for all message types
755 if (!fgInstance) new AliLog;
756 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
757 fgInstance->fPrintLocation[iType] = on;
761 //_____________________________________________________________________________
762 void AliLog::SetPrintLocation(EType_t type, Bool_t on)
764 // switch on or off the printing of the file name and line number
765 // for the given message type
767 if ((type < kFatal) || (type >= kMaxType)) return;
768 if (!fgInstance) new AliLog;
769 fgInstance->fPrintLocation[type] = on;
773 //_____________________________________________________________________________
774 void AliLog::SetPrintRepetitions(Bool_t on)
776 // switch on or off the printing of the number of repetitions of a message
777 // instead of repeating the same message
779 if (!fgInstance) new AliLog;
780 if (!on && (fgInstance->fRepetitions > 0)) fgInstance->PrintRepetitions();
781 fgInstance->fPrintRepetitions = on;
785 //_____________________________________________________________________________
786 void AliLog::WriteToFile(const char* name, Int_t option)
788 // write the log object with the given name and option to the current file
790 if (!fgInstance) new AliLog;
791 fgInstance->TObject::Write(name, option);
795 //_____________________________________________________________________________
796 UInt_t AliLog::GetLogLevel(const char* module, const char* className) const
798 // get the logging level for the given module and class
800 if (!fgInstance) new AliLog;
802 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
803 if (obj) return obj->GetUniqueID();
806 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
807 if (obj) return obj->GetUniqueID();
809 return fgInstance->fGlobalLogLevel;
812 //_____________________________________________________________________________
813 Int_t AliLog::GetDebugLevel(const char* module, const char* className)
815 // get the debug level for the given module and class
817 if (!fgInstance) new AliLog;
818 return fgInstance->GetLogLevel(module, className) - kDebugOffset;
821 //_____________________________________________________________________________
822 void AliLog::PrintMessage(UInt_t type, const char* message,
823 const char* module, const char* className,
824 const char* function, const char* file, Int_t line)
826 // print the given message
828 // don't print the message if it is repeated
829 if (fPrintRepetitions &&
830 (fLastType == type) &&
831 (message && (fLastMessage.CompareTo(message) == 0)) &&
832 ((module && (fLastModule.CompareTo(module) == 0)) ||
833 (!module && fLastModule.IsNull())) &&
834 ((className && (fLastClassName.CompareTo(className) == 0)) ||
835 (!className && fLastClassName.IsNull())) &&
836 ((function && (fLastFunction.CompareTo(function) == 0)) ||
837 (!function && fLastFunction.IsNull()))&&
838 ((file && (fLastFile.CompareTo(file) == 0)) ||
839 (!file && fLastFile.IsNull())) &&
840 (fLastLine == line)) {
845 // print number of repetitions
846 if (fRepetitions > 0) PrintRepetitions();
848 // remember this message
851 fLastMessage = message;
852 fLastModule = module;
853 fLastClassName = className;
854 fLastFunction = function;
859 FILE* stream = GetOutputStream(type);
860 static const char* typeNames[kMaxType] =
861 {"Fatal", "Error", "Warning", "Info", "Debug"};
863 if (fPrintType[type]) {
864 PrintString(type, stream, "%c-", typeNames[type][0]);
866 if (fPrintModule[type] && module) {
867 PrintString(type, stream, "%s/", module);
869 if (fPrintScope[type] && className) {
870 PrintString(type, stream, "%s::", className);
873 PrintString(type, stream, "%s: %s", function, message);
875 PrintString(type, stream, "%s", function);
877 if (fPrintLocation[type] && file) {
878 PrintString(type, stream, " (%s:%.0d)", file, line);
881 PrintString(type, stream, "\n");
883 PrintString(type, stream, ": ");
885 if (fCallBacks[type]) (*(fCallBacks[type]))((EType_t)type, NULL);
888 //_____________________________________________________________________________
889 void AliLog::PrintRepetitions()
891 // print number of repetitions
893 PrintString(fLastType, GetOutputStream(fLastType), " <message repeated %d time%s>\n",
894 fRepetitions, (fRepetitions > 1) ? "s" : "");
895 if (fCallBacks[fLastType]) (*(fCallBacks[fLastType]))((EType_t)fLastType, NULL);
898 //_____________________________________________________________________________
899 void AliLog::Message(UInt_t level, const char* message,
900 const char* module, const char* className,
901 const char* function, const char* file, Int_t line)
903 // print a log message
905 if (!fgInstance) new AliLog;
907 // get the message type
909 if (type >= kMaxType) type = kMaxType - 1;
911 // print the message if the debug level allows
912 if (level <= fgInstance->GetLogLevel(module, className)) {
913 fgInstance->PrintMessage(type, message,
914 module, className, function, file, line);
917 // abort in case of a fatal message
918 if (type == kFatal) {
921 gSystem->StackTrace();
929 //_____________________________________________________________________________
930 void AliLog::Debug(UInt_t level, const char* message,
931 const char* module, const char* className,
932 const char* function, const char* file, Int_t line)
934 // print a debug message
936 if (level == 0) level = 1;
937 level += kDebugOffset;
938 Message(level, message, module, className, function, file, line);
942 //_____________________________________________________________________________
943 Int_t AliLog::RedirectStdoutTo(EType_t type, UInt_t level, const char* module,
944 const char* className, const char* function,
945 const char* file, Int_t line, Bool_t print)
947 // redirect the standard output to the stream of the given type
949 if (!fgInstance) new AliLog;
950 return fgInstance->RedirectTo(stdout, type, level, module, className,
951 function, file, line, print);
954 //_____________________________________________________________________________
955 Int_t AliLog::RedirectStderrTo(EType_t type, UInt_t level, const char* module,
956 const char* className, const char* function,
957 const char* file, Int_t line, Bool_t print)
959 // redirect the standard error output to the stream of the given type
961 if (!fgInstance) new AliLog;
962 return fgInstance->RedirectTo(stderr, type, level, module, className,
963 function, file, line, print);
966 //_____________________________________________________________________________
967 Int_t AliLog::RedirectTo(FILE* stream, EType_t type, UInt_t level,
968 const char* module, const char* className,
969 const char* function, const char* file, Int_t line,
972 // redirect the standard (error) output stream to the stream of the given type
974 // get the original file descriptor to be able to restore it later
975 Int_t original = dup(fileno(stream));
978 // flush the stream of the selected type
979 FILE* newStream = GetOutputStream(type);
983 if ((type == kDebug) && (level > 0)) level--;
984 if (type + level > GetLogLevel(module, className)) { // /dev/null
985 if(!freopen("/dev/null", "a", stream)) AliWarning("Cannot reopen /dev/null");
986 } else if (fOutputTypes[type] == 0) { // stdout
987 if (stream != stdout) dup2(fileno(stdout), fileno(stream));
988 } else if (fOutputTypes[type] == 1) { // stderr
989 if (stream != stderr) dup2(fileno(stderr), fileno(stream));
990 } else if (fOutputTypes[type] == 2) { // file
991 if(!freopen(fFileNames[type], "a", stream)) AliWarning(Form("Cannot reopen %s",fFileNames[type].Data()));
992 } else if (fOutputTypes[type] == 3) { // external C++ stream
993 // redirection is not possible for external C++ streams
998 PrintMessage(type, NULL, module, className, function, file, line);
1005 //_____________________________________________________________________________
1006 void AliLog::RestoreStdout(Int_t original)
1008 // restore the standard output
1011 dup2(original, fileno(stdout));
1015 //_____________________________________________________________________________
1016 void AliLog::RestoreStderr(Int_t original)
1018 // restore the standard error output
1021 dup2(original, fileno(stderr));
1026 //_____________________________________________________________________________
1027 ostream& AliLog::Stream(EType_t type, UInt_t level,
1028 const char* module, const char* className,
1029 const char* function, const char* file, Int_t line)
1031 // get the stream object for the given output type
1033 if (!fgInstance) new AliLog;
1034 return fgInstance->GetStream(type, level, module, className,
1035 function, file, line);
1038 //_____________________________________________________________________________
1039 ostream& AliLog::GetStream(EType_t type, UInt_t level,
1040 const char* module, const char* className,
1041 const char* function, const char* file, Int_t line)
1043 // get the stream object for the given output type
1045 if ((type == kDebug) && (level > 0)) level--;
1046 Bool_t noOutput = (type + level > GetLogLevel(module, className));
1049 PrintMessage(type, NULL, module, className, function, file, line);
1051 fflush(GetOutputStream(type));
1053 static ofstream nullStream("/dev/null");
1056 } else if (fOutputTypes[type] == 0) {
1058 } else if (fOutputTypes[type] == 1) {
1060 } else if (fOutputTypes[type] == 2) {
1061 return *fOutputStreams[type];
1062 } else if (fOutputTypes[type] == 3) {
1063 return *fOutputStreams[type];
1069 void AliLog::SetStreamOutput(ostream* stream)
1071 // set an external stream as target for log messages of all types
1072 // the external stream is completely handled by the caller, the
1073 // AliLog class just writes to it
1075 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
1076 SetStreamOutput((AliLog::EType_t)iType, stream);
1080 void AliLog::SetStreamOutput(EType_t type, ostream* stream)
1082 // set an external stream as target for log messages of the given type
1083 // the external stream is completely handled by the caller, the
1084 // AliLog class just writes to it
1086 if ((type < kFatal) || (type >= kMaxType)) return;
1087 if (!fgInstance) new AliLog;
1088 if (fgInstance->fOutputTypes[type] == 2) {
1089 fgInstance->CloseFile(type);
1091 fgInstance->fOutputTypes[type] = 3;
1092 fgInstance->fFileNames[type] = "";
1093 fgInstance->fOutputFiles[type] = NULL;
1094 fgInstance->fOutputStreams[type] = stream;
1097 void AliLog::SetLogNotification(AliLogNotification pCallBack)
1099 // set a notification callback function for log messages of all types
1101 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
1102 SetLogNotification((AliLog::EType_t)iType, pCallBack);
1106 void AliLog::SetLogNotification(EType_t type, AliLogNotification pCallBack)
1108 // set a notifications call back function for log messages of all types
1109 // the callback fuction is invoced whenever an output was written
1110 // Note: does not work for c++ streamer classes, the external stream
1111 // has to handle this diectly (e.g. custom implementation of endl)
1113 if ((type < kFatal) || (type >= kMaxType)) return;
1114 if (!fgInstance) new AliLog;
1115 fgInstance->fCallBacks[type]=pCallBack;
1118 void AliLog::PrintString(Int_t type, FILE* stream, const char* format, ...)
1120 // this is the general method to print a log message using variadac args
1121 // to the FILE* like (C - like) streams, e.g. stdout, stderr, or files
1123 // Only in case of an external c++ ostream type output, the message is
1124 // written to that stream and the notifictaion callback is called.
1125 // The message is printed by a normal vfprintf function otherwise
1127 if (format==NULL) return;
1130 va_start(ap, format);
1131 if (fOutputTypes[type] != 3) {
1133 vfprintf(stream, format, ap);
1136 // build the string and write everthing to the corresponding ostream
1137 TString fmt(format);
1138 TArrayC tgt(fmt.Length()*10); // just take a number
1141 R__VA_COPY(bap, ap);
1143 #warning definition of R__VA_COPY has disappeared
1148 iResult=vsnprintf(tgt.GetArray(), tgt.GetSize(), format, ap);
1150 iResult=tgt.GetSize()*2;
1151 } else if (iResult<tgt.GetSize()) {
1155 if (iResult<10000) {
1158 R__VA_COPY(ap, bap);
1162 tgt[tgt.GetSize()-1]=0;
1170 if (fOutputStreams[type]) {
1171 *(fOutputStreams[type]) << tgt.GetArray();