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 ///////////////////////////////////////////////////////////////////////////////
35 #include <Riostream.h>
46 AliLog* AliLog::fgInstance = NULL;
48 Bool_t AliLog::fgDebugEnabled = kTRUE;
51 //_____________________________________________________________________________
54 fGlobalLogLevel(kInfo),
57 fPrintRepetitions(kTRUE),
67 // default constructor: set default values
69 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
70 fOutputTypes[iType] = 0;
71 fFileNames[iType] = "";
72 fOutputFiles[iType] = NULL;
73 fOutputStreams[iType] = NULL;
75 fPrintType[iType] = kTRUE;
76 fPrintModule[iType] = kFALSE;
77 fPrintScope[iType] = kTRUE;
78 fPrintLocation[iType] = (iType == kDebug);
81 // replace the previous instance by this one
82 if (fgInstance) delete fgInstance;
85 SetHandleRootMessages(kTRUE);
87 // read the .rootrc settings
91 //_____________________________________________________________________________
94 // destructor: clean up and reset instance pointer
96 if (fRepetitions > 0) PrintRepetitions();
98 for (Int_t i = 0; i < fModuleDebugLevels.GetEntriesFast(); i++) {
99 if (fModuleDebugLevels[i]) fModuleDebugLevels[i]->Delete();
101 fClassDebugLevels.Delete();
102 for (Int_t i = 0; i < fClassDebugLevels.GetEntriesFast(); i++) {
103 if (fClassDebugLevels[i]) fClassDebugLevels[i]->Delete();
105 fClassDebugLevels.Delete();
107 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
116 //_____________________________________________________________________________
117 AliLog::AliLog(const AliLog& log) :
122 Fatal("AliLog", "copy constructor not implemented");
125 //_____________________________________________________________________________
126 AliLog& AliLog::operator = (const AliLog& /*log*/)
128 // assignment operator
130 Fatal("operator =", "assignment operator not implemented");
135 //_____________________________________________________________________________
136 void AliLog::ReadEnvSettings()
138 // load settings from the root configuration file (.rootrc)
139 // and from environment variables
141 static const char* typeNames[kMaxType] =
142 {"kFatal", "kError", "kWarning", "kInfo", "kDebug"};
144 // debug en- or disabling
145 if (gSystem->Getenv("LOG_NO_DEBUG")) {
146 fgDebugEnabled = kFALSE;
147 } else if (gEnv->Defined("AliRoot.AliLog.EnableDebug")) {
148 fgDebugEnabled = gEnv->GetValue("AliRoot.AliLog.EnableDebug",
150 AliInfo(Form("debug %sabled", ((fgDebugEnabled) ? "en" : "dis")));
154 if (gEnv->Defined("AliRoot.AliLog.GlobalLogLevel")) {
155 const char* type = gEnv->GetValue("AliRoot.AliLog.GlobalLogLevel", "");
156 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
157 if (strcmp(type, typeNames[iType]) == 0) fGlobalLogLevel = iType;
159 AliDebug(3, Form("global log level set to %d", fGlobalLogLevel));
162 // global debug level
163 if (gEnv->Defined("AliRoot.AliLog.GlobalDebugLevel")) {
164 Int_t level = gEnv->GetValue("AliRoot.AliLog.GlobalDebugLevel",
165 Int_t(fGlobalLogLevel - kDebugOffset));
166 if (level < -kDebugOffset) level = kDebugOffset;
167 fGlobalLogLevel = kDebugOffset + level;
168 AliDebug(3, Form("global debug level set to %d",
169 fGlobalLogLevel - kDebugOffset));
172 // module debug level
173 if (gEnv->Defined("AliRoot.AliLog.ModuleDebugLevel")) {
174 TString levels = gEnv->GetValue("AliRoot.AliLog.ModuleDebugLevel", "");
175 char* p = const_cast<char*>(levels.Data());
176 while (const char* module = strtok(p, " ")) {
178 char* pos = index(module, ':');
181 Int_t level = atoi(pos);
182 SetModuleDebugLevel(module, level);
183 AliDebug(3, Form("debug level for module %s set to %d", module, level));
188 if (gEnv->Defined("AliRoot.AliLog.ClassDebugLevel")) {
189 TString levels = gEnv->GetValue("AliRoot.AliLog.ClassDebugLevel", "");
190 char* p = const_cast<char*>(levels.Data());
191 while (const char* className = strtok(p, " ")) {
193 char* pos = index(className, ':');
196 Int_t level = atoi(pos);
197 SetClassDebugLevel(className, level);
198 AliDebug(3, Form("debug level for class %s set to %d",
203 // general output stream
204 if (gEnv->Defined("AliRoot.AliLog.Output")) {
205 TString stream = gEnv->GetValue("AliRoot.AliLog.Output", "Standard");
206 if (stream.CompareTo("standard", TString::kIgnoreCase) == 0) {
208 AliDebug(3, "output stream set to standard output for all types");
209 } else if (stream.CompareTo("error", TString::kIgnoreCase) == 0) {
211 AliDebug(3, "output stream set to error output for all types");
212 } else if (!stream.IsNull()) {
213 SetFileOutput(stream);
214 AliDebug(3, Form("output stream set to file %s for all types",
219 // individual output streams
220 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
221 TString name("AliRoot.AliLog.Output.");
222 name += &typeNames[iType][1];
223 if (gEnv->Defined(name)) {
224 TString stream = gEnv->GetValue(name, "Standard");
225 if (stream.CompareTo("standard", TString::kIgnoreCase) == 0) {
226 SetStandardOutput(EType(iType));
227 AliDebug(3, Form("output stream set to standard output for type %s",
229 } else if (stream.CompareTo("error", TString::kIgnoreCase) == 0) {
230 SetErrorOutput(EType(iType));
231 AliDebug(3, Form("output stream set to error output for type %s",
233 } else if (!stream.IsNull()) {
234 SetFileOutput(EType(iType), stream);
235 AliDebug(3, Form("output stream set to file %s for type %s",
236 stream.Data(), typeNames[iType]));
241 // handling of root error messages
242 if (gEnv->Defined("AliRoot.AliLog.HandleRootMessages")) {
243 Bool_t on = gEnv->GetValue("AliRoot.AliLog.HandleRootMessages", kTRUE);
244 SetHandleRootMessages(on);
245 AliDebug(3, Form("handling of root messages %sabled",
246 ((on) ? "en" : "dis")));
250 static const char* settingNames[4] =
251 {"Type", "Module", "Scope", "Location"};
253 {fPrintType, fPrintModule, fPrintScope, fPrintLocation};
254 for (Int_t iSetting = 0; iSetting < 4; iSetting++) {
255 TString name("AliRoot.AliLog.Print");
256 name += settingNames[iSetting];
257 if (gEnv->Defined(name)) {
258 Bool_t on = gEnv->GetValue(name, settings[iSetting][0]);
259 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
260 settings[iSetting][iType] = on;
262 AliDebug(3, Form("printing of %s %sabled for all types",
263 settingNames[iSetting], ((on) ? "en" : "dis")));
266 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
267 TString nameType = name + "." + &typeNames[iType][1];
268 if (gEnv->Defined(nameType)) {
269 Bool_t on = gEnv->GetValue(nameType, settings[iSetting][iType]);
270 settings[iSetting][iType] = on;
271 AliDebug(3, Form("printing of %s %sabled for type %s",
272 settingNames[iSetting], ((on) ? "en" : "dis"),
278 // repetition of messages
279 if (gEnv->Defined("AliRoot.AliLog.PrintRepetitions")) {
280 Bool_t on = gEnv->GetValue("AliRoot.AliLog.PrintRepetitions", kTRUE);
281 fPrintRepetitions = on;
282 AliDebug(3, Form("printing of message repetitions %sabled",
283 ((on) ? "en" : "dis")));
288 //_____________________________________________________________________________
289 void AliLog::RootErrorHandler(Int_t level, Bool_t abort,
290 const char* location, const char* message)
292 // new error handler for messages from root
295 case ::kFatal : level = kFatal; break;
297 DefaultErrorHandler(level, abort, location, message);
300 DefaultErrorHandler(level, abort, location, message);
302 case ::kError : level = kError; break;
303 case ::kWarning : level = kWarning; break;
304 case ::kInfo : level = kInfo; break;
305 default : level = kDebug; break;
307 AliLog::Message(level, message, "ROOT", NULL, location, NULL, 0);
311 //_____________________________________________________________________________
312 void AliLog::EnableDebug(Bool_t enabled)
314 // enable or disable debug output
316 fgDebugEnabled = enabled;
319 //_____________________________________________________________________________
320 void AliLog::SetGlobalLogLevel(EType type)
322 // set the global debug level
324 if (!fgInstance) new AliLog;
325 fgInstance->fGlobalLogLevel = type;
328 //_____________________________________________________________________________
329 Int_t AliLog::GetGlobalLogLevel()
331 // get the global debug level
333 if (!fgInstance) new AliLog;
334 return fgInstance->fGlobalLogLevel;
337 //_____________________________________________________________________________
338 void AliLog::SetGlobalDebugLevel(Int_t level)
340 // set the global debug level
342 if (!fgInstance) new AliLog;
343 if (level < -kDebugOffset) level = -kDebugOffset;
344 fgInstance->fGlobalLogLevel = kDebugOffset + level;
347 //_____________________________________________________________________________
348 Int_t AliLog::GetGlobalDebugLevel()
350 // get the global debug level
352 if (!fgInstance) new AliLog;
353 return fgInstance->fGlobalLogLevel - kDebugOffset;
356 //_____________________________________________________________________________
357 void AliLog::SetModuleDebugLevel(const char* module, Int_t level)
359 // set the debug level for the given module
362 if (!fgInstance) new AliLog;
363 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
365 obj = new TNamed(module, module);
366 fgInstance->fModuleDebugLevels.Add(obj);
368 level += kDebugOffset;
369 if (level < kFatal) level = kFatal;
370 obj->SetUniqueID(level);
373 //_____________________________________________________________________________
374 void AliLog::ClearModuleDebugLevel(const char* module)
376 // remove the setting of the debug level for the given module
379 if (!fgInstance) new AliLog;
380 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
381 if (obj) delete fgInstance->fModuleDebugLevels.Remove(obj);
384 //_____________________________________________________________________________
385 void AliLog::SetClassDebugLevel(const char* className, Int_t level)
387 // set the debug level for the given class
389 if (!className) return;
390 if (!fgInstance) new AliLog;
391 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
393 obj = new TNamed(className, className);
394 fgInstance->fClassDebugLevels.Add(obj);
396 level += kDebugOffset;
397 if (level < kFatal) level = kFatal;
398 obj->SetUniqueID(level);
401 //_____________________________________________________________________________
402 void AliLog::ClearClassDebugLevel(const char* className)
404 // remove the setting of the debug level for the given class
406 if (!className) return;
407 if (!fgInstance) new AliLog;
408 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
409 if (obj) delete fgInstance->fClassDebugLevels.Remove(obj);
413 //_____________________________________________________________________________
414 void AliLog::SetStandardOutput()
416 // write all log messages to the standard output (stdout)
418 if (!fgInstance) new AliLog;
419 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
420 fgInstance->CloseFile(iType);
421 fgInstance->fOutputTypes[iType] = 0;
425 //_____________________________________________________________________________
426 void AliLog::SetStandardOutput(EType type)
428 // write log messages of the given type to the standard output (stdout)
430 if ((type < kFatal) || (type >= kMaxType)) return;
431 if (!fgInstance) new AliLog;
432 fgInstance->CloseFile(type);
433 fgInstance->fOutputTypes[type] = 0;
436 //_____________________________________________________________________________
437 void AliLog::SetErrorOutput()
439 // write all log messages to the error output (stderr)
441 if (!fgInstance) new AliLog;
442 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
443 fgInstance->CloseFile(iType);
444 fgInstance->fOutputTypes[iType] = 1;
448 //_____________________________________________________________________________
449 void AliLog::SetErrorOutput(EType type)
451 // write log messages of the given type to the error output (stderr)
453 if ((type < kFatal) || (type >= kMaxType)) return;
454 if (!fgInstance) new AliLog;
455 fgInstance->CloseFile(type);
456 fgInstance->fOutputTypes[type] = 1;
459 //_____________________________________________________________________________
460 void AliLog::SetFileOutput(const char* fileName)
462 // write all log messages to the given file
464 if (!fgInstance) new AliLog;
465 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
466 if ((fgInstance->fOutputTypes[iType] == 2) &&
467 (fgInstance->fFileNames[iType].CompareTo(fileName) != 0)) {
468 fgInstance->CloseFile(iType);
470 fgInstance->fOutputTypes[iType] = 2;
471 fgInstance->fFileNames[iType] = fileName;
472 fgInstance->fOutputFiles[iType] = NULL;
473 fgInstance->fOutputStreams[iType] = NULL;
477 //_____________________________________________________________________________
478 void AliLog::SetFileOutput(EType type, const char* fileName)
480 // write log messages of the given type to the given file
482 if ((type < kFatal) || (type >= kMaxType)) return;
483 if (!fgInstance) new AliLog;
484 if ((fgInstance->fOutputTypes[type] == 2) &&
485 (fgInstance->fFileNames[type].CompareTo(fileName) != 0)) {
486 fgInstance->CloseFile(type);
488 fgInstance->fOutputTypes[type] = 2;
489 fgInstance->fFileNames[type] = fileName;
490 fgInstance->fOutputFiles[type] = NULL;
491 fgInstance->fOutputStreams[type] = NULL;
494 //_____________________________________________________________________________
495 void AliLog::CloseFile(Int_t type)
497 // close the file for the given type if needed
499 if ((fOutputTypes[type] == 2) && fOutputFiles[type]) {
500 Bool_t closeFile = kTRUE;
501 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
502 if ((iType != type) && (fOutputFiles[iType] == fOutputFiles[type])) {
507 fclose(fOutputFiles[type]);
508 fOutputStreams[type]->close();
509 delete fOutputStreams[type];
512 fOutputFiles[type] = NULL;
513 fOutputStreams[type] = NULL;
514 fFileNames[type] = "";
515 fOutputTypes[type] = 0;
518 //_____________________________________________________________________________
519 FILE* AliLog::GetOutputStream(Int_t type)
521 // get the output stream for the given type of messages
523 if (type > kDebug) type = kDebug;
524 if (fOutputTypes[type] == 0) return stdout;
525 else if (fOutputTypes[type] == 1) return stderr;
526 else if (fOutputTypes[type] == 2) {
527 if (!fOutputFiles[type]) {
529 ofstream* stream = NULL;
530 if (!fFileNames[type].IsNull()) {
531 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
532 if ((iType != type) &&
533 (fFileNames[iType].CompareTo(fFileNames[type]) == 0) &&
534 fOutputFiles[iType]) {
535 file = fOutputFiles[iType];
536 stream = fOutputStreams[iType];
541 file = fopen(fFileNames[type], "a");
542 stream = new ofstream(fFileNames[type], ios::app);
545 fOutputFiles[type] = file;
546 fOutputStreams[type] = stream;
547 if (!file) CloseFile(type);
549 if (fOutputFiles[type]) return fOutputFiles[type];
555 //_____________________________________________________________________________
558 // flush the output streams
560 if (!fgInstance) new AliLog;
561 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
562 if (fgInstance->fOutputFiles[iType]) {
563 fflush(fgInstance->fOutputFiles[iType]);
564 fgInstance->fOutputStreams[iType]->flush();
572 //_____________________________________________________________________________
573 void AliLog::SetHandleRootMessages(Bool_t on)
575 // enable or disable the handling of messages form root
577 if (!fgInstance) new AliLog;
579 SetErrorHandler(RootErrorHandler);
581 SetErrorHandler(DefaultErrorHandler);
586 //_____________________________________________________________________________
587 void AliLog::SetPrintType(Bool_t on)
589 // switch on or off the printing of the message type for all message types
591 if (!fgInstance) new AliLog;
592 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
593 fgInstance->fPrintType[iType] = on;
597 //_____________________________________________________________________________
598 void AliLog::SetPrintType(EType type, Bool_t on)
600 // switch on or off the printing of the message type for the given message type
602 if ((type < kFatal) || (type >= kMaxType)) return;
603 if (!fgInstance) new AliLog;
604 fgInstance->fPrintType[type] = on;
607 //_____________________________________________________________________________
608 void AliLog::SetPrintModule(Bool_t on)
610 // switch on or off the printing of the module for all message types
612 if (!fgInstance) new AliLog;
613 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
614 fgInstance->fPrintModule[iType] = on;
618 //_____________________________________________________________________________
619 void AliLog::SetPrintModule(EType type, Bool_t on)
621 // switch on or off the printing of the module for the given message type
623 if ((type < kFatal) || (type >= kMaxType)) return;
624 if (!fgInstance) new AliLog;
625 fgInstance->fPrintModule[type] = on;
628 //_____________________________________________________________________________
629 void AliLog::SetPrintScope(Bool_t on)
631 // switch on or off the printing of the scope/class name for all message types
633 if (!fgInstance) new AliLog;
634 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
635 fgInstance->fPrintScope[iType] = on;
639 //_____________________________________________________________________________
640 void AliLog::SetPrintScope(EType type, Bool_t on)
642 // switch on or off the printing of the scope/class name
643 // for the given message type
645 if ((type < kFatal) || (type >= kMaxType)) return;
646 if (!fgInstance) new AliLog;
647 fgInstance->fPrintScope[type] = on;
650 //_____________________________________________________________________________
651 void AliLog::SetPrintLocation(Bool_t on)
653 // switch on or off the printing of the file name and line number
654 // for all message types
656 if (!fgInstance) new AliLog;
657 for (Int_t iType = kFatal; iType < kMaxType; iType++) {
658 fgInstance->fPrintLocation[iType] = on;
662 //_____________________________________________________________________________
663 void AliLog::SetPrintLocation(EType type, Bool_t on)
665 // switch on or off the printing of the file name and line number
666 // for the given message type
668 if ((type < kFatal) || (type >= kMaxType)) return;
669 if (!fgInstance) new AliLog;
670 fgInstance->fPrintLocation[type] = on;
674 //_____________________________________________________________________________
675 void AliLog::SetPrintRepetitions(Bool_t on)
677 // switch on or off the printing of the number of repetitions of a message
678 // instead of repeating the same message
680 if (!fgInstance) new AliLog;
681 if (!on && (fgInstance->fRepetitions > 0)) fgInstance->PrintRepetitions();
682 fgInstance->fPrintRepetitions = on;
686 //_____________________________________________________________________________
687 void AliLog::WriteToFile(const char* name, Int_t option)
689 // write the log object with the given name and option to the current file
691 if (!fgInstance) new AliLog;
692 fgInstance->TObject::Write(name, option);
696 //_____________________________________________________________________________
697 UInt_t AliLog::GetLogLevel(const char* module, const char* className) const
699 // get the logging level for the given module and class
701 if (!fgInstance) new AliLog;
703 TObject* obj = fgInstance->fClassDebugLevels.FindObject(className);
704 if (obj) return obj->GetUniqueID();
707 TObject* obj = fgInstance->fModuleDebugLevels.FindObject(module);
708 if (obj) return obj->GetUniqueID();
710 return fgInstance->fGlobalLogLevel;
713 //_____________________________________________________________________________
714 Int_t AliLog::GetDebugLevel(const char* module, const char* className)
716 // get the debug level for the given module and class
718 if (!fgInstance) new AliLog;
719 return fgInstance->GetLogLevel(module, className) - kDebugOffset;
722 //_____________________________________________________________________________
723 void AliLog::PrintMessage(UInt_t type, const char* message,
724 const char* module, const char* className,
725 const char* function, const char* file, Int_t line)
727 // print the given message
729 // don't print the message if it is repeated
730 if (fPrintRepetitions &&
731 (fLastType == type) &&
732 (message && (fLastMessage.CompareTo(message) == 0)) &&
733 ((module && (fLastModule.CompareTo(module) == 0)) ||
734 (!module && fLastModule.IsNull())) &&
735 ((className && (fLastClassName.CompareTo(className) == 0)) ||
736 (!className && fLastClassName.IsNull())) &&
737 ((function && (fLastFunction.CompareTo(function) == 0)) ||
738 (!function && fLastFunction.IsNull()))&&
739 ((file && (fLastFile.CompareTo(file) == 0)) ||
740 (!file && fLastFile.IsNull())) &&
741 (fLastLine == line)) {
746 // print number of repetitions
747 if (fRepetitions > 0) PrintRepetitions();
749 // remember this message
752 fLastMessage = message;
753 fLastModule = module;
754 fLastClassName = className;
755 fLastFunction = function;
760 FILE* stream = GetOutputStream(type);
761 static const char* typeNames[kMaxType] =
762 {"Fatal", "Error", "Warning", "Info", "Debug"};
764 if (fPrintType[type]) {
765 fprintf(stream, "%s in ", typeNames[type]);
767 fprintf(stream, "<");
768 if (fPrintModule[type] && module) {
769 fprintf(stream, "%s/", module);
771 if (fPrintScope[type] && className) {
772 fprintf(stream, "%s::", className);
775 fprintf(stream, "%s>: %s", function, message);
777 fprintf(stream, "%s>", function);
779 if (fPrintLocation[type] && file) {
780 fprintf(stream, " (%s:%.0d)", file, line);
783 fprintf(stream, "\n");
785 fprintf(stream, ": ");
789 //_____________________________________________________________________________
790 void AliLog::PrintRepetitions()
792 // print number of repetitions
794 fprintf(GetOutputStream(fLastType), " <message repeated %d time%s>\n",
795 fRepetitions, (fRepetitions > 1) ? "s" : "");
798 //_____________________________________________________________________________
799 void AliLog::Message(UInt_t level, const char* message,
800 const char* module, const char* className,
801 const char* function, const char* file, Int_t line)
803 // print a log message
805 if (!fgInstance) new AliLog;
807 // get the message type
809 if (type >= kMaxType) type = kMaxType - 1;
811 // print the message if the debug level allows
812 if (level <= fgInstance->GetLogLevel(module, className)) {
813 fgInstance->PrintMessage(type, message,
814 module, className, function, file, line);
817 // abort in case of a fatal message
818 if (type == kFatal) {
821 gSystem->StackTrace();
829 //_____________________________________________________________________________
830 void AliLog::Debug(UInt_t level, const char* message,
831 const char* module, const char* className,
832 const char* function, const char* file, Int_t line)
834 // print a debug message
836 if (level == 0) level = 1;
837 level += kDebugOffset;
838 Message(level, message, module, className, function, file, line);
842 //_____________________________________________________________________________
843 Int_t AliLog::RedirectStdoutTo(EType type, UInt_t level, const char* module,
844 const char* className, const char* function,
845 const char* file, Int_t line, Bool_t print)
847 // redirect the standard output to the stream of the given type
849 if (!fgInstance) new AliLog;
850 return fgInstance->RedirectTo(stdout, type, level, module, className,
851 function, file, line, print);
854 //_____________________________________________________________________________
855 Int_t AliLog::RedirectStderrTo(EType type, UInt_t level, const char* module,
856 const char* className, const char* function,
857 const char* file, Int_t line, Bool_t print)
859 // redirect the standard error output to the stream of the given type
861 if (!fgInstance) new AliLog;
862 return fgInstance->RedirectTo(stderr, type, level, module, className,
863 function, file, line, print);
866 //_____________________________________________________________________________
867 Int_t AliLog::RedirectTo(FILE* stream, EType type, UInt_t level,
868 const char* module, const char* className,
869 const char* function, const char* file, Int_t line,
872 // redirect the standard (error) output stream to the stream of the given type
874 // get the original file descriptor to be able to restore it later
875 Int_t original = dup(fileno(stream));
878 // flush the stream of the selected type
879 FILE* newStream = GetOutputStream(type);
883 if ((type == kDebug) && (level > 0)) level--;
884 if (type + level > GetLogLevel(module, className)) { // /dev/null
885 freopen("/dev/null", "a", stream);
886 } else if (fOutputTypes[type] == 0) { // stdout
887 if (stream != stdout) dup2(fileno(stdout), fileno(stream));
888 } else if (fOutputTypes[type] == 1) { // stderr
889 if (stream != stderr) dup2(fileno(stderr), fileno(stream));
890 } else if (fOutputTypes[type] == 2) { // file
891 freopen(fFileNames[type], "a", stream);
896 PrintMessage(type, NULL, module, className, function, file, line);
903 //_____________________________________________________________________________
904 void AliLog::RestoreStdout(Int_t original)
906 // restore the standard output
909 dup2(original, fileno(stdout));
913 //_____________________________________________________________________________
914 void AliLog::RestoreStderr(Int_t original)
916 // restore the standard error output
919 dup2(original, fileno(stderr));
924 //_____________________________________________________________________________
925 ostream& AliLog::Stream(EType type, UInt_t level,
926 const char* module, const char* className,
927 const char* function, const char* file, Int_t line)
929 // get the stream object for the given output type
931 if (!fgInstance) new AliLog;
932 return fgInstance->GetStream(type, level, module, className,
933 function, file, line);
936 //_____________________________________________________________________________
937 ostream& AliLog::GetStream(EType type, UInt_t level,
938 const char* module, const char* className,
939 const char* function, const char* file, Int_t line)
941 // get the stream object for the given output type
943 if ((type == kDebug) && (level > 0)) level--;
944 Bool_t noOutput = (type + level > GetLogLevel(module, className));
947 PrintMessage(type, NULL, module, className, function, file, line);
949 fflush(GetOutputStream(type));
951 static ofstream nullStream("/dev/null");
954 } else if (fOutputTypes[type] == 0) {
956 } else if (fOutputTypes[type] == 1) {
958 } else if (fOutputTypes[type] == 2) {
959 return *fOutputStreams[type];