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
49 AliLog* AliLog::fgInstance = NULL;
51 Bool_t AliLog::fgDebugEnabled = kTRUE;
54 //_____________________________________________________________________________
57 fGlobalLogLevel(kInfo),
60 fPrintRepetitions(kTRUE),
70 // default constructor: set default values
72 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
73 fOutputTypes[iType] = 0;
74 fFileNames[iType] = "";
75 fOutputFiles[iType] = NULL;
76 fOutputStreams[iType] = NULL;
77 fCallBacks[iType]=NULL;
79 fPrintType[iType] = kTRUE;
80 fPrintModule[iType] = kFALSE;
81 fPrintScope[iType] = kTRUE;
82 fPrintLocation[iType] = (iType == kDebug);
85 // replace the previous instance by this one
86 if (fgInstance) delete fgInstance;
89 SetHandleRootMessages(kTRUE);
91 // read the .rootrc settings
95 //_____________________________________________________________________________
98 // destructor: clean up and reset instance pointer
100 if (fRepetitions > 0) PrintRepetitions();
102 for (Int_t i = 0; i < fModuleDebugLevels.GetEntriesFast(); i++) {
103 if (fModuleDebugLevels[i]) fModuleDebugLevels[i]->Delete();
105 fClassDebugLevels.Delete();
106 for (Int_t i = 0; i < fClassDebugLevels.GetEntriesFast(); i++) {
107 if (fClassDebugLevels[i]) fClassDebugLevels[i]->Delete();
109 fClassDebugLevels.Delete();
111 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
120 //_____________________________________________________________________________
121 AliLog::AliLog(const AliLog& log) :
123 fGlobalLogLevel(log.fGlobalLogLevel),
124 fModuleDebugLevels(log.fModuleDebugLevels),
125 fClassDebugLevels(log.fClassDebugLevels),
126 fPrintRepetitions(log.fPrintRepetitions),
127 fRepetitions(log.fRepetitions),
128 fLastType(log.fLastType),
129 fLastMessage(log.fLastMessage),
130 fLastModule(log.fLastModule),
131 fLastClassName(log.fLastClassName),
132 fLastFunction(log.fLastFunction),
133 fLastFile(log.fLastFile),
134 fLastLine(log.fLastLine)
138 Fatal("AliLog", "copy constructor not implemented");
141 //_____________________________________________________________________________
142 AliLog& AliLog::operator = (const AliLog& /*log*/)
144 // assignment operator
146 Fatal("operator =", "assignment operator not implemented");
151 //_____________________________________________________________________________
152 void AliLog::ReadEnvSettings()
154 // load settings from the root configuration file (.rootrc)
155 // and from environment variables
157 static const char* typeNames[kMaxType] =
158 {"kFatal", "kError", "kWarning", "kInfo", "kDebug"};
160 // debug en- or disabling
161 if (gSystem->Getenv("LOG_NO_DEBUG")) {
162 fgDebugEnabled = kFALSE;
163 } else if (gEnv->Defined("AliRoot.AliLog.EnableDebug")) {
164 fgDebugEnabled = gEnv->GetValue("AliRoot.AliLog.EnableDebug",
166 AliInfo(Form("debug %sabled", ((fgDebugEnabled) ? "en" : "dis")));
170 if (gEnv->Defined("AliRoot.AliLog.GlobalLogLevel")) {
171 const char* type = gEnv->GetValue("AliRoot.AliLog.GlobalLogLevel", "");
172 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
173 if (strcmp(type, typeNames[iType]) == 0) fGlobalLogLevel = iType;
175 AliDebug(3, Form("global log level set to %d", fGlobalLogLevel));
178 // global debug level
179 if (gEnv->Defined("AliRoot.AliLog.GlobalDebugLevel")) {
180 Int_t level = gEnv->GetValue("AliRoot.AliLog.GlobalDebugLevel",
181 Int_t(fGlobalLogLevel - kDebugOffset));
182 if (level < -kDebugOffset) level = kDebugOffset;
183 fGlobalLogLevel = kDebugOffset + level;
184 AliDebug(3, Form("global debug level set to %d",
185 fGlobalLogLevel - kDebugOffset));
188 // module debug level
189 if (gEnv->Defined("AliRoot.AliLog.ModuleDebugLevel")) {
190 TString levels = gEnv->GetValue("AliRoot.AliLog.ModuleDebugLevel", "");
191 char* p = const_cast<char*>(levels.Data());
192 while (const char* module = strtok(p, " ")) {
194 char* pos = index(module, ':');
197 Int_t level = atoi(pos);
198 SetModuleDebugLevel(module, level);
199 AliDebug(3, Form("debug level for module %s set to %d", module, level));
204 if (gEnv->Defined("AliRoot.AliLog.ClassDebugLevel")) {
205 TString levels = gEnv->GetValue("AliRoot.AliLog.ClassDebugLevel", "");
206 char* p = const_cast<char*>(levels.Data());
207 while (const char* className = strtok(p, " ")) {
209 char* pos = index(className, ':');
212 Int_t level = atoi(pos);
213 SetClassDebugLevel(className, level);
214 AliDebug(3, Form("debug level for class %s set to %d",
219 // general output stream
220 if (gEnv->Defined("AliRoot.AliLog.Output")) {
221 TString stream = gEnv->GetValue("AliRoot.AliLog.Output", "Standard");
222 if (stream.CompareTo("standard", TString::kIgnoreCase) == 0) {
224 AliDebug(3, "output stream set to standard output for all types");
225 } else if (stream.CompareTo("error", TString::kIgnoreCase) == 0) {
227 AliDebug(3, "output stream set to error output for all types");
228 } else if (!stream.IsNull()) {
229 SetFileOutput(stream);
230 AliDebug(3, Form("output stream set to file %s for all types",
235 // individual output streams
236 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
237 TString name("AliRoot.AliLog.Output.");
238 name += &typeNames[iType][1];
239 if (gEnv->Defined(name)) {
240 TString stream = gEnv->GetValue(name, "Standard");
241 if (stream.CompareTo("standard", TString::kIgnoreCase) == 0) {
242 SetStandardOutput(EType_t(iType));
243 AliDebug(3, Form("output stream set to standard output for type %s",
245 } else if (stream.CompareTo("error", TString::kIgnoreCase) == 0) {
246 SetErrorOutput(EType_t(iType));
247 AliDebug(3, Form("output stream set to error output for type %s",
249 } else if (!stream.IsNull()) {
250 SetFileOutput(EType_t(iType), stream);
251 AliDebug(3, Form("output stream set to file %s for type %s",
252 stream.Data(), typeNames[iType]));
257 // handling of root error messages
258 if (gEnv->Defined("AliRoot.AliLog.HandleRootMessages")) {
259 Bool_t on = gEnv->GetValue("AliRoot.AliLog.HandleRootMessages", kTRUE);
260 SetHandleRootMessages(on);
261 AliDebug(3, Form("handling of root messages %sabled",
262 ((on) ? "en" : "dis")));
266 static const char* settingNames[4] =
267 {"Type", "Module", "Scope", "Location"};
269 {fPrintType, fPrintModule, fPrintScope, fPrintLocation};
270 for (Int_t iSetting = 0; iSetting < 4; iSetting++) {
271 TString name("AliRoot.AliLog.Print");
272 name += settingNames[iSetting];
273 if (gEnv->Defined(name)) {
274 Bool_t on = gEnv->GetValue(name, settings[iSetting][0]);
275 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
276 settings[iSetting][iType] = on;
278 AliDebug(3, Form("printing of %s %sabled for all types",
279 settingNames[iSetting], ((on) ? "en" : "dis")));
282 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
283 TString nameType = name + "." + &typeNames[iType][1];
284 if (gEnv->Defined(nameType)) {
285 Bool_t on = gEnv->GetValue(nameType, settings[iSetting][iType]);
286 settings[iSetting][iType] = on;
287 AliDebug(3, Form("printing of %s %sabled for type %s",
288 settingNames[iSetting], ((on) ? "en" : "dis"),
294 // repetition of messages
295 if (gEnv->Defined("AliRoot.AliLog.PrintRepetitions")) {
296 Bool_t on = gEnv->GetValue("AliRoot.AliLog.PrintRepetitions", kTRUE);
297 fPrintRepetitions = on;
298 AliDebug(3, Form("printing of message repetitions %sabled",
299 ((on) ? "en" : "dis")));
304 //_____________________________________________________________________________
305 void AliLog::RootErrorHandler(Int_t level, Bool_t abort,
306 const char* location, const char* message)
308 // new error handler for messages from root
311 case ::kFatal : level = kFatal; break;
313 DefaultErrorHandler(level, abort, location, message);
316 DefaultErrorHandler(level, abort, location, message);
318 case ::kError : level = kError; break;
319 case ::kWarning : level = kWarning; break;
320 case ::kInfo : level = kInfo; break;
321 default : level = kDebug; break;
323 AliLog::Message(level, message, "ROOT", NULL, location, NULL, 0);
327 //_____________________________________________________________________________
328 void AliLog::EnableDebug(Bool_t enabled)
330 // enable or disable debug output
332 fgDebugEnabled = enabled;
335 //_____________________________________________________________________________
336 void AliLog::SetGlobalLogLevel(EType_t type)
338 // set the global debug level
340 if (!fgInstance) new AliLog;
341 fgInstance->fGlobalLogLevel = type;
344 //_____________________________________________________________________________
345 Int_t AliLog::GetGlobalLogLevel()
347 // get the global debug level
349 if (!fgInstance) new AliLog;
350 return fgInstance->fGlobalLogLevel;
353 //_____________________________________________________________________________
354 void AliLog::SetGlobalDebugLevel(Int_t level)
356 // set the global debug level
358 if (!fgInstance) new AliLog;
359 if (level < -kDebugOffset) level = -kDebugOffset;
360 fgInstance->fGlobalLogLevel = kDebugOffset + level;
363 //_____________________________________________________________________________
364 Int_t AliLog::GetGlobalDebugLevel()
366 // get the global debug level
368 if (!fgInstance) new AliLog;
369 return fgInstance->fGlobalLogLevel - kDebugOffset;
372 //_____________________________________________________________________________
373 void AliLog::SetModuleDebugLevel(const char* module, Int_t level)
375 // set the debug level for the given module
378 if (!fgInstance) new AliLog;
379 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
381 obj = new TNamed(module, module);
382 fgInstance->fModuleDebugLevels.Add(obj);
384 level += kDebugOffset;
385 if (level < kFatal) level = kFatal;
386 obj->SetUniqueID(level);
389 //_____________________________________________________________________________
390 void AliLog::ClearModuleDebugLevel(const char* module)
392 // remove the setting of the debug level for the given module
395 if (!fgInstance) new AliLog;
396 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
397 if (obj) delete fgInstance->fModuleDebugLevels.Remove(obj);
400 //_____________________________________________________________________________
401 void AliLog::SetClassDebugLevel(const char* className, Int_t level)
403 // set the debug level for the given class
405 if (!className) return;
406 if (!fgInstance) new AliLog;
407 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
409 obj = new TNamed(className, className);
410 fgInstance->fClassDebugLevels.Add(obj);
412 level += kDebugOffset;
413 if (level < kFatal) level = kFatal;
414 obj->SetUniqueID(level);
417 //_____________________________________________________________________________
418 void AliLog::ClearClassDebugLevel(const char* className)
420 // remove the setting of the debug level for the given class
422 if (!className) return;
423 if (!fgInstance) new AliLog;
424 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
425 if (obj) delete fgInstance->fClassDebugLevels.Remove(obj);
429 //_____________________________________________________________________________
430 void AliLog::SetStandardOutput()
432 // write all log messages to the standard output (stdout)
434 if (!fgInstance) new AliLog;
435 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
436 fgInstance->CloseFile(iType);
437 fgInstance->fOutputTypes[iType] = 0;
441 //_____________________________________________________________________________
442 void AliLog::SetStandardOutput(EType_t type)
444 // write log messages of the given type to the standard output (stdout)
446 if ((type < kFatal) || (type >= kMaxType)) return;
447 if (!fgInstance) new AliLog;
448 fgInstance->CloseFile(type);
449 fgInstance->fOutputTypes[type] = 0;
452 //_____________________________________________________________________________
453 void AliLog::SetErrorOutput()
455 // write all log messages to the error output (stderr)
457 if (!fgInstance) new AliLog;
458 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
459 fgInstance->CloseFile(iType);
460 fgInstance->fOutputTypes[iType] = 1;
464 //_____________________________________________________________________________
465 void AliLog::SetErrorOutput(EType_t type)
467 // write log messages of the given type to the error output (stderr)
469 if ((type < kFatal) || (type >= kMaxType)) return;
470 if (!fgInstance) new AliLog;
471 fgInstance->CloseFile(type);
472 fgInstance->fOutputTypes[type] = 1;
475 //_____________________________________________________________________________
476 void AliLog::SetFileOutput(const char* fileName)
478 // write all log messages to the given file
480 if (!fgInstance) new AliLog;
481 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
482 if ((fgInstance->fOutputTypes[iType] == 2) &&
483 (fgInstance->fFileNames[iType].CompareTo(fileName) != 0)) {
484 fgInstance->CloseFile(iType);
486 fgInstance->fOutputTypes[iType] = 2;
487 fgInstance->fFileNames[iType] = fileName;
488 fgInstance->fOutputFiles[iType] = NULL;
489 fgInstance->fOutputStreams[iType] = NULL;
493 //_____________________________________________________________________________
494 void AliLog::SetFileOutput(EType_t type, const char* fileName)
496 // write log messages of the given type to the given file
498 if ((type < kFatal) || (type >= kMaxType)) return;
499 if (!fgInstance) new AliLog;
500 if ((fgInstance->fOutputTypes[type] == 2) &&
501 (fgInstance->fFileNames[type].CompareTo(fileName) != 0)) {
502 fgInstance->CloseFile(type);
504 fgInstance->fOutputTypes[type] = 2;
505 fgInstance->fFileNames[type] = fileName;
506 fgInstance->fOutputFiles[type] = NULL;
507 fgInstance->fOutputStreams[type] = NULL;
510 //_____________________________________________________________________________
511 void AliLog::CloseFile(Int_t type)
513 // close the file for the given type if needed
515 if ((fOutputTypes[type] == 2) && fOutputFiles[type]) {
516 Bool_t closeFile = kTRUE;
517 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
518 if ((iType != type) && (fOutputFiles[iType] == fOutputFiles[type])) {
523 fclose(fOutputFiles[type]);
524 ofstream* stream=reinterpret_cast<ofstream*>(fOutputStreams[type]);
526 delete fOutputStreams[type];
529 fOutputFiles[type] = NULL;
530 fOutputStreams[type] = NULL;
531 fFileNames[type] = "";
532 fOutputTypes[type] = 0;
535 //_____________________________________________________________________________
536 FILE* AliLog::GetOutputStream(Int_t type)
538 // get the output stream for the given type of messages
540 if (type > kDebug) type = kDebug;
541 if (fOutputTypes[type] == 0) return stdout;
542 else if (fOutputTypes[type] == 1) return stderr;
543 else if (fOutputTypes[type] == 2) {
544 if (!fOutputFiles[type]) {
546 ostream* stream = NULL;
547 if (!fFileNames[type].IsNull()) {
548 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
549 if ((iType != type) &&
550 (fFileNames[iType].CompareTo(fFileNames[type]) == 0) &&
551 fOutputFiles[iType]) {
552 file = fOutputFiles[iType];
553 stream = fOutputStreams[iType];
558 file = fopen(fFileNames[type], "a");
559 stream = new ofstream(fFileNames[type], ios::app);
562 fOutputFiles[type] = file;
563 fOutputStreams[type] = stream;
564 if (!file) CloseFile(type);
566 if (fOutputFiles[type]) return fOutputFiles[type];
572 //_____________________________________________________________________________
575 // flush the output streams
577 if (!fgInstance) new AliLog;
578 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
579 if (fgInstance->fOutputFiles[iType]) {
580 fflush(fgInstance->fOutputFiles[iType]);
581 fgInstance->fOutputStreams[iType]->flush();
589 //_____________________________________________________________________________
590 void AliLog::SetHandleRootMessages(Bool_t on)
592 // enable or disable the handling of messages form root
594 if (!fgInstance) new AliLog;
596 SetErrorHandler(RootErrorHandler);
598 SetErrorHandler(DefaultErrorHandler);
603 //_____________________________________________________________________________
604 void AliLog::SetPrintType(Bool_t on)
606 // switch on or off the printing of the message type for all message types
608 if (!fgInstance) new AliLog;
609 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
610 fgInstance->fPrintType[iType] = on;
614 //_____________________________________________________________________________
615 void AliLog::SetPrintType(EType_t type, Bool_t on)
617 // switch on or off the printing of the message type for the given message type
619 if ((type < kFatal) || (type >= kMaxType)) return;
620 if (!fgInstance) new AliLog;
621 fgInstance->fPrintType[type] = on;
624 //_____________________________________________________________________________
625 void AliLog::SetPrintModule(Bool_t on)
627 // switch on or off the printing of the module for all message types
629 if (!fgInstance) new AliLog;
630 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
631 fgInstance->fPrintModule[iType] = on;
635 //_____________________________________________________________________________
636 void AliLog::SetPrintModule(EType_t type, Bool_t on)
638 // switch on or off the printing of the module for the given message type
640 if ((type < kFatal) || (type >= kMaxType)) return;
641 if (!fgInstance) new AliLog;
642 fgInstance->fPrintModule[type] = on;
645 //_____________________________________________________________________________
646 void AliLog::SetPrintScope(Bool_t on)
648 // switch on or off the printing of the scope/class name for all message types
650 if (!fgInstance) new AliLog;
651 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
652 fgInstance->fPrintScope[iType] = on;
656 //_____________________________________________________________________________
657 void AliLog::SetPrintScope(EType_t type, Bool_t on)
659 // switch on or off the printing of the scope/class name
660 // for the given message type
662 if ((type < kFatal) || (type >= kMaxType)) return;
663 if (!fgInstance) new AliLog;
664 fgInstance->fPrintScope[type] = on;
667 //_____________________________________________________________________________
668 void AliLog::SetPrintLocation(Bool_t on)
670 // switch on or off the printing of the file name and line number
671 // for all message types
673 if (!fgInstance) new AliLog;
674 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
675 fgInstance->fPrintLocation[iType] = on;
679 //_____________________________________________________________________________
680 void AliLog::SetPrintLocation(EType_t type, Bool_t on)
682 // switch on or off the printing of the file name and line number
683 // for the given message type
685 if ((type < kFatal) || (type >= kMaxType)) return;
686 if (!fgInstance) new AliLog;
687 fgInstance->fPrintLocation[type] = on;
691 //_____________________________________________________________________________
692 void AliLog::SetPrintRepetitions(Bool_t on)
694 // switch on or off the printing of the number of repetitions of a message
695 // instead of repeating the same message
697 if (!fgInstance) new AliLog;
698 if (!on && (fgInstance->fRepetitions > 0)) fgInstance->PrintRepetitions();
699 fgInstance->fPrintRepetitions = on;
703 //_____________________________________________________________________________
704 void AliLog::WriteToFile(const char* name, Int_t option)
706 // write the log object with the given name and option to the current file
708 if (!fgInstance) new AliLog;
709 fgInstance->TObject::Write(name, option);
713 //_____________________________________________________________________________
714 UInt_t AliLog::GetLogLevel(const char* module, const char* className) const
716 // get the logging level for the given module and class
718 if (!fgInstance) new AliLog;
720 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
721 if (obj) return obj->GetUniqueID();
724 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
725 if (obj) return obj->GetUniqueID();
727 return fgInstance->fGlobalLogLevel;
730 //_____________________________________________________________________________
731 Int_t AliLog::GetDebugLevel(const char* module, const char* className)
733 // get the debug level for the given module and class
735 if (!fgInstance) new AliLog;
736 return fgInstance->GetLogLevel(module, className) - kDebugOffset;
739 //_____________________________________________________________________________
740 void AliLog::PrintMessage(UInt_t type, const char* message,
741 const char* module, const char* className,
742 const char* function, const char* file, Int_t line)
744 // print the given message
746 // don't print the message if it is repeated
747 if (fPrintRepetitions &&
748 (fLastType == type) &&
749 (message && (fLastMessage.CompareTo(message) == 0)) &&
750 ((module && (fLastModule.CompareTo(module) == 0)) ||
751 (!module && fLastModule.IsNull())) &&
752 ((className && (fLastClassName.CompareTo(className) == 0)) ||
753 (!className && fLastClassName.IsNull())) &&
754 ((function && (fLastFunction.CompareTo(function) == 0)) ||
755 (!function && fLastFunction.IsNull()))&&
756 ((file && (fLastFile.CompareTo(file) == 0)) ||
757 (!file && fLastFile.IsNull())) &&
758 (fLastLine == line)) {
763 // print number of repetitions
764 if (fRepetitions > 0) PrintRepetitions();
766 // remember this message
769 fLastMessage = message;
770 fLastModule = module;
771 fLastClassName = className;
772 fLastFunction = function;
777 FILE* stream = GetOutputStream(type);
778 static const char* typeNames[kMaxType] =
779 {"Fatal", "Error", "Warning", "Info", "Debug"};
781 if (fPrintType[type]) {
782 PrintString(type, stream, "%c-", typeNames[type][0]);
784 if (fPrintModule[type] && module) {
785 PrintString(type, stream, "%s/", module);
787 if (fPrintScope[type] && className) {
788 PrintString(type, stream, "%s::", className);
791 PrintString(type, stream, "%s: %s", function, message);
793 PrintString(type, stream, "%s", function);
795 if (fPrintLocation[type] && file) {
796 PrintString(type, stream, " (%s:%.0d)", file, line);
799 PrintString(type, stream, "\n");
801 PrintString(type, stream, ": ");
803 if (fCallBacks[type]) (*(fCallBacks[type]))((EType_t)type, NULL);
806 //_____________________________________________________________________________
807 void AliLog::PrintRepetitions()
809 // print number of repetitions
811 PrintString(fLastType, GetOutputStream(fLastType), " <message repeated %d time%s>\n",
812 fRepetitions, (fRepetitions > 1) ? "s" : "");
813 if (fCallBacks[fLastType]) (*(fCallBacks[fLastType]))((EType_t)fLastType, NULL);
816 //_____________________________________________________________________________
817 void AliLog::Message(UInt_t level, const char* message,
818 const char* module, const char* className,
819 const char* function, const char* file, Int_t line)
821 // print a log message
823 if (!fgInstance) new AliLog;
825 // get the message type
827 if (type >= kMaxType) type = kMaxType - 1;
829 // print the message if the debug level allows
830 if (level <= fgInstance->GetLogLevel(module, className)) {
831 fgInstance->PrintMessage(type, message,
832 module, className, function, file, line);
835 // abort in case of a fatal message
836 if (type == kFatal) {
839 gSystem->StackTrace();
847 //_____________________________________________________________________________
848 void AliLog::Debug(UInt_t level, const char* message,
849 const char* module, const char* className,
850 const char* function, const char* file, Int_t line)
852 // print a debug message
854 if (level == 0) level = 1;
855 level += kDebugOffset;
856 Message(level, message, module, className, function, file, line);
860 //_____________________________________________________________________________
861 Int_t AliLog::RedirectStdoutTo(EType_t type, UInt_t level, const char* module,
862 const char* className, const char* function,
863 const char* file, Int_t line, Bool_t print)
865 // redirect the standard output to the stream of the given type
867 if (!fgInstance) new AliLog;
868 return fgInstance->RedirectTo(stdout, type, level, module, className,
869 function, file, line, print);
872 //_____________________________________________________________________________
873 Int_t AliLog::RedirectStderrTo(EType_t type, UInt_t level, const char* module,
874 const char* className, const char* function,
875 const char* file, Int_t line, Bool_t print)
877 // redirect the standard error output to the stream of the given type
879 if (!fgInstance) new AliLog;
880 return fgInstance->RedirectTo(stderr, type, level, module, className,
881 function, file, line, print);
884 //_____________________________________________________________________________
885 Int_t AliLog::RedirectTo(FILE* stream, EType_t type, UInt_t level,
886 const char* module, const char* className,
887 const char* function, const char* file, Int_t line,
890 // redirect the standard (error) output stream to the stream of the given type
892 // get the original file descriptor to be able to restore it later
893 Int_t original = dup(fileno(stream));
896 // flush the stream of the selected type
897 FILE* newStream = GetOutputStream(type);
901 if ((type == kDebug) && (level > 0)) level--;
902 if (type + level > GetLogLevel(module, className)) { // /dev/null
903 freopen("/dev/null", "a", stream);
904 } else if (fOutputTypes[type] == 0) { // stdout
905 if (stream != stdout) dup2(fileno(stdout), fileno(stream));
906 } else if (fOutputTypes[type] == 1) { // stderr
907 if (stream != stderr) dup2(fileno(stderr), fileno(stream));
908 } else if (fOutputTypes[type] == 2) { // file
909 freopen(fFileNames[type], "a", stream);
910 } else if (fOutputTypes[type] == 3) { // external C++ stream
911 // redirection is not possible for external C++ streams
916 PrintMessage(type, NULL, module, className, function, file, line);
923 //_____________________________________________________________________________
924 void AliLog::RestoreStdout(Int_t original)
926 // restore the standard output
929 dup2(original, fileno(stdout));
933 //_____________________________________________________________________________
934 void AliLog::RestoreStderr(Int_t original)
936 // restore the standard error output
939 dup2(original, fileno(stderr));
944 //_____________________________________________________________________________
945 ostream& AliLog::Stream(EType_t type, UInt_t level,
946 const char* module, const char* className,
947 const char* function, const char* file, Int_t line)
949 // get the stream object for the given output type
951 if (!fgInstance) new AliLog;
952 return fgInstance->GetStream(type, level, module, className,
953 function, file, line);
956 //_____________________________________________________________________________
957 ostream& AliLog::GetStream(EType_t type, UInt_t level,
958 const char* module, const char* className,
959 const char* function, const char* file, Int_t line)
961 // get the stream object for the given output type
963 if ((type == kDebug) && (level > 0)) level--;
964 Bool_t noOutput = (type + level > GetLogLevel(module, className));
967 PrintMessage(type, NULL, module, className, function, file, line);
969 fflush(GetOutputStream(type));
971 static ofstream nullStream("/dev/null");
974 } else if (fOutputTypes[type] == 0) {
976 } else if (fOutputTypes[type] == 1) {
978 } else if (fOutputTypes[type] == 2) {
979 return *fOutputStreams[type];
980 } else if (fOutputTypes[type] == 3) {
981 return *fOutputStreams[type];
987 void AliLog::SetStreamOutput(ostream* stream)
989 // set an external stream as target for log messages of all types
990 // the external stream is completely handled by the caller, the
991 // AliLog class just writes to it
993 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
994 SetStreamOutput((AliLog::EType_t)iType, stream);
998 void AliLog::SetStreamOutput(EType_t type, ostream* stream)
1000 // set an external stream as target for log messages of the given type
1001 // the external stream is completely handled by the caller, the
1002 // AliLog class just writes to it
1004 if ((type < kFatal) || (type >= kMaxType)) return;
1005 if (!fgInstance) new AliLog;
1006 if (fgInstance->fOutputTypes[type] == 2) {
1007 fgInstance->CloseFile(type);
1009 fgInstance->fOutputTypes[type] = 3;
1010 fgInstance->fFileNames[type] = "";
1011 fgInstance->fOutputFiles[type] = NULL;
1012 fgInstance->fOutputStreams[type] = stream;
1015 void AliLog::SetLogNotification(AliLogNotification pCallBack)
1017 // set a notification callback function for log messages of all types
1019 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
1020 SetLogNotification((AliLog::EType_t)iType, pCallBack);
1024 void AliLog::SetLogNotification(EType_t type, AliLogNotification pCallBack)
1026 // set a notifications call back function for log messages of all types
1027 // the callback fuction is invoced whenever an output was written
1028 // Note: does not work for c++ streamer classes, the external stream
1029 // has to handle this diectly (e.g. custom implementation of endl)
1031 if ((type < kFatal) || (type >= kMaxType)) return;
1032 if (!fgInstance) new AliLog;
1033 fgInstance->fCallBacks[type]=pCallBack;
1036 void AliLog::PrintString(Int_t type, FILE* stream, const char* format, ...)
1038 // this is the general method to print a log message using variadac args
1039 // to the FILE* like (C - like) streams, e.g. stdout, stderr, or files
1041 // Only in case of an external c++ ostream type output, the message is
1042 // written to that stream and the notifictaion callback is called.
1043 // The message is printed by a normal vfprintf function otherwise
1045 if (format==NULL) return;
1048 va_start(ap, format);
1049 if (fOutputTypes[type] != 3) {
1051 vfprintf(stream, format, ap);
1054 // build the string and write everthing to the corresponding ostream
1055 TString fmt(format);
1056 TArrayC tgt(fmt.Length()*10); // just take a number
1059 R__VA_COPY(bap, ap);
1061 #warning definition of R__VA_COPY has disappeared
1066 iResult=vsnprintf(tgt.GetArray(), tgt.GetSize(), format, ap);
1068 iResult=tgt.GetSize()*2;
1069 } else if (iResult<tgt.GetSize()) {
1073 if (iResult<10000) {
1076 R__VA_COPY(ap, bap);
1080 tgt[tgt.GetSize()-1]=0;
1088 if (fOutputStreams[type]) {
1089 *(fOutputStreams[type]) << tgt.GetArray();