redirection and C++ streams added
[u/mrichter/AliRoot.git] / STEER / AliLog.h
6ab674bd 1#ifndef ALILOG_H
2#define ALILOG_H
3/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * See cxx source for full Copyright notice */
6/* $Id$ */
9/// class for logging debug, info and error messages
12#include <TObject.h>
13#include <TObjArray.h>
14#include <TString.h>
17class AliLog: public TObject {
18 public:
19 AliLog();
20 virtual ~AliLog();
21 static AliLog* Instance() {return fgInstance;}
23 enum EType {kFatal = 0, kError, kWarning, kInfo, kDebug, kMaxType};
25 static void EnableDebug(Bool_t enabled);
26 static void SetGlobalLogLevel(EType type);
27 static Int_t GetGlobalLogLevel();
28 static void SetGlobalDebugLevel(Int_t level);
29 static Int_t GetGlobalDebugLevel();
30 static void SetModuleDebugLevel(const char* module, Int_t level);
31 static void ClearModuleDebugLevel(const char* module);
32 static void SetClassDebugLevel(const char* className, Int_t level);
33 static void ClearClassDebugLevel(const char* className);
35 static void SetStandardOutput();
36 static void SetStandardOutput(EType type);
37 static void SetErrorOutput();
38 static void SetErrorOutput(EType type);
39 static void SetFileOutput(const char* fileName);
40 static void SetFileOutput(EType type, const char* fileName);
41 static void Flush();
43 static void SetHandleRootMessages(Bool_t on);
45 static void SetPrintType(Bool_t on);
46 static void SetPrintType(EType type, Bool_t on);
47 static void SetPrintModule(Bool_t on);
48 static void SetPrintModule(EType type, Bool_t on);
49 static void SetPrintScope(Bool_t on);
50 static void SetPrintScope(EType type, Bool_t on);
51 static void SetPrintLocation(Bool_t on);
52 static void SetPrintLocation(EType type, Bool_t on);
54 static void Write(const char* name, Int_t option = 0);
56 // the following public methods are used by the preprocessor macros
57 // and should not be called directly
20848f77 58 static Bool_t IsDebugEnabled() {return fgDebugEnabled;}
6ab674bd 59 static Int_t GetDebugLevel(const char* module, const char* className);
60 static void Message(UInt_t level, const char* message,
61 const char* module, const char* className,
62 const char* function, const char* file, Int_t line);
63 static void Debug(UInt_t level, const char* message,
64 const char* module, const char* className,
65 const char* function, const char* file, Int_t line);
eeb769e2 67 static Int_t RedirectStdoutTo(EType type, UInt_t level, const char* module,
68 const char* className, const char* function,
69 const char* file, Int_t line, Bool_t print);
70 static Int_t RedirectStderrTo(EType type, UInt_t level, const char* module,
71 const char* className, const char* function,
72 const char* file, Int_t line, Bool_t print);
73 static void RestoreStdout(Int_t original);
74 static void RestoreStderr(Int_t original);
76 static ostream& Stream(EType type, UInt_t level,
77 const char* module, const char* className,
78 const char* function, const char* file, Int_t line);
6ab674bd 80 private:
81 AliLog(const AliLog& log);
82 AliLog& operator = (const AliLog& log);
eeb769e2 84 void ReadEnvSettings();
6ab674bd 86 static void RootErrorHandler(Int_t level, Bool_t abort,
87 const char* location, const char* message);
89 void CloseFile(Int_t type);
90 FILE* GetOutputStream(Int_t type);
92 UInt_t GetLogLevel(const char* module, const char* className) const;
eeb769e2 93 void PrintMessage(UInt_t type, const char* message,
94 const char* module, const char* className,
95 const char* function,
96 const char* file, Int_t line);
98 Int_t RedirectTo(FILE* stream, EType type, UInt_t level,
99 const char* module, const char* className,
100 const char* function,
101 const char* file, Int_t line, Bool_t print);
103 ostream& GetStream(EType type, UInt_t level,
104 const char* module, const char* className,
105 const char* function, const char* file, Int_t line);
6ab674bd 106
107 enum {kDebugOffset = kDebug-1};
109 static AliLog* fgInstance; //! pointer to current instance
111 static Bool_t fgDebugEnabled; // flag for debug en-/disabling
113 UInt_t fGlobalLogLevel; // global logging level
114 TObjArray fModuleDebugLevels; // debug levels for modules
115 TObjArray fClassDebugLevels; // debug levels for classes
117 Int_t fOutputTypes[kMaxType]; // types of output streams
118 TString fFileNames[kMaxType]; // file names
119 FILE* fOutputFiles[kMaxType]; //! log output files
eeb769e2 120 ofstream* fOutputStreams[kMaxType]; //! log output streams
6ab674bd 121
122 Bool_t fPrintType[kMaxType]; // print type on/off
123 Bool_t fPrintModule[kMaxType]; // print module on/off
124 Bool_t fPrintScope[kMaxType]; // print scope/class name on/off
125 Bool_t fPrintLocation[kMaxType]; // print file and line on/off
127 ClassDef(AliLog, 1) // class for logging debug, info and error messages
eeb769e2 130
131// module name
132#ifdef __MODULE__
133#define MODULENAME() __MODULE__
135#define MODULENAME() "NoModule"
cd676389 136#endif
eeb769e2 137
138// function name
139#if defined(__GNUC__) || defined(__ICC) || defined(__ECC) || defined(__APPLE__)
141#elif defined(__HP_aCC) || defined(__alpha) || defined(__DECCXX)
142#define FUNCTIONNAME() __FUNC__
144#define FUNCTIONNAME() "???"
cd676389 145#endif
6ab674bd 146
eeb769e2 147// redirection
148#define REDIRECTSTDOUT(type, level, scope, whatever) {Int_t originalStdout = AliLog::RedirectStdoutTo(type, level, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__, kFALSE); whatever; AliLog::RestoreStdout(originalStdout);}
149#define REDIRECTSTDERR(type, level, scope, whatever) {Int_t originalStderr = AliLog::RedirectStderrTo(type, level, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__, kFALSE); whatever; AliLog::RestoreStderr(originalStderr);}
150#define REDIRECTSTDOUTANDSTDERR(type, level, scope, whatever) {Int_t originalStdout = AliLog::RedirectStdoutTo(type, level, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__, kFALSE); Int_t originalStderr = AliLog::RedirectStderrTo(type, level, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__, kFALSE); whatever; AliLog::RestoreStderr(originalStderr); AliLog::RestoreStdout(originalStdout);}
153// debug level
6ab674bd 154#ifdef LOG_NO_DEBUG
155#define AliDebugLevel() -1
156#define AliDebugLevelClass() -1
157#define AliDebugLevelGeneral(scope) -1
eeb769e2 159#define AliDebugLevel() ((AliLog::IsDebugEnabled()) ? AliLog::GetDebugLevel(MODULENAME(), ClassName()) : -1)
160#define AliDebugLevelClass() ((AliLog::IsDebugEnabled()) ? AliLog::GetDebugLevel(MODULENAME(), Class()->GetName()) : -1)
161#define AliDebugLevelGeneral(scope) ((AliLog::IsDebugEnabled()) ? AliLog::GetDebugLevel(MODULENAME(), scope) : -1)
6ab674bd 162#endif
eeb769e2 164// debug messages
6ab674bd 165#ifdef LOG_NO_DEBUG
166#define AliDebug(level, message)
167#define AliDebugClass(level, message)
168#define AliDebugGeneral(scope, level, message)
eeb769e2 170#define AliDebug(level, message) {if (AliLog::IsDebugEnabled()) AliLog::Debug(level, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
171#define AliDebugClass(level, message) {if (AliLog::IsDebugEnabled()) AliLog::Debug(level, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
172#define AliDebugGeneral(scope, level, message) {if (AliLog::IsDebugEnabled()) AliLog::Debug(level, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
6ab674bd 173#endif
eeb769e2 175// redirection to debug
176#define StdoutToAliDebug(level, whatever) REDIRECTSTDOUT(AliLog::kDebug, level, ClassName(), whatever)
177#define StderrToAliDebug(level, whatever) REDIRECTSTDERR(AliLog::kDebug, level, ClassName(), whatever)
178#define ToAliDebug(level, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kDebug, level, ClassName(), whatever)
179#define StdoutToAliDebugClass(level, whatever) REDIRECTSTDOUT(AliLog::kDebug, level, Class()->GetName(), whatever)
180#define StderrToAliDebugClass(level, whatever) REDIRECTSTDERR(AliLog::kDebug, level, Class()->GetName(), whatever)
181#define ToAliDebugClass(level, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kDebug, level, Class()->GetName(), whatever)
182#define StdoutToAliDebugGeneral(scope, level, whatever) REDIRECTSTDOUT(AliLog::kDebug, level, scope, whatever)
183#define StderrToAliDebugGeneral(scope, level, whatever) REDIRECTSTDERR(AliLog::kDebug, level, scope, whatever)
184#define ToAliDebugGeneral(scope, level, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kDebug, level, scope, whatever)
186// debug stream objects
187#define AliDebugStream(level) AliLog::Stream(AliLog::kDebug, level, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__)
188#define AliDebugClassStream(level) AliLog::Stream(AliLog::kDebug, level, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__)
189#define AliDebugGeneralStream(scope, level) AliLog::Stream(AliLog::kDebug, level, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__)
192// info messages
6ab674bd 193#ifdef LOG_NO_INFO
194#define AliInfo(message)
195#define AliInfoClass(message)
196#define AliInfoGeneral(scope, message)
eeb769e2 198#define AliInfo(message) {AliLog::Message(AliLog::kInfo, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
199#define AliInfoClass(message) {AliLog::Message(AliLog::kInfo, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
200#define AliInfoGeneral(scope, message) {AliLog::Message(AliLog::kInfo, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
6ab674bd 201#endif
eeb769e2 203// redirection to info
204#define StdoutToAliInfo(whatever) REDIRECTSTDOUT(AliLog::kInfo, 0, ClassName(), whatever)
205#define StderrToAliInfo(whatever) REDIRECTSTDERR(AliLog::kInfo, 0, ClassName(), whatever)
206#define ToAliInfo(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kInfo, 0, ClassName(), whatever)
207#define StdoutToAliInfoClass(whatever) REDIRECTSTDOUT(AliLog::kInfo, 0, Class()->GetName(), whatever)
208#define StderrToAliInfoClass(whatever) REDIRECTSTDERR(AliLog::kInfo, 0, Class()->GetName(), whatever)
209#define ToAliInfoClass(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kInfo, 0, Class()->GetName(), whatever)
210#define StdoutToAliInfoGeneral(scope, whatever) REDIRECTSTDOUT(AliLog::kInfo, 0, scope, whatever)
211#define StderrToAliInfoGeneral(scope, whatever) REDIRECTSTDERR(AliLog::kInfo, 0, scope, whatever)
212#define ToAliInfoGeneral(scope, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kInfo, 0, scope, whatever)
214// info stream objects
215#define AliInfoStream() AliLog::Stream(AliLog::kInfo, 0, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__)
216#define AliInfoClassStream() AliLog::Stream(AliLog::kInfo, 0, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__)
217#define AliInfoGeneralStream(scope) AliLog::Stream(AliLog::kInfo, 0, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__)
220// warning messages
6ab674bd 221#ifdef LOG_NO_WARNING
222#define AliWarning(message)
223#define AliWarningClass(message)
224#define AliWarningGeneral(scope, message)
eeb769e2 226#define AliWarning(message) {AliLog::Message(AliLog::kWarning, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
227#define AliWarningClass(message) {AliLog::Message(AliLog::kWarning, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
228#define AliWarningGeneral(scope, message) {AliLog::Message(AliLog::kWarning, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
6ab674bd 229#endif
eeb769e2 231// redirection to warning
232#define StdoutToAliWarning(whatever) REDIRECTSTDOUT(AliLog::kWarning, 0, ClassName(), whatever)
233#define StderrToAliWarning(whatever) REDIRECTSTDERR(AliLog::kWarning, 0, ClassName(), whatever)
234#define ToAliWarning(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kWarning, 0, ClassName(), whatever)
235#define StdoutToAliWarningClass(whatever) REDIRECTSTDOUT(AliLog::kWarning, 0, Class()->GetName(), whatever)
236#define StderrToAliWarningClass(whatever) REDIRECTSTDERR(AliLog::kWarning, 0, Class()->GetName(), whatever)
237#define ToAliWarningClass(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kWarning, 0, Class()->GetName(), whatever)
238#define StdoutToAliWarningGeneral(scope, whatever) REDIRECTSTDOUT(AliLog::kWarning, 0, scope, whatever)
239#define StderrToAliWarningGeneral(scope, whatever) REDIRECTSTDERR(AliLog::kWarning, 0, scope, whatever)
240#define ToAliWarningGeneral(scope, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kWarning, 0, scope, whatever)
242// warning stream objects
243#define AliWarningStream() AliLog::Stream(AliLog::kWarning, 0, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__)
244#define AliWarningClassStream() AliLog::Stream(AliLog::kWarning, 0, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__)
245#define AliWarningGeneralStream(scope) AliLog::Stream(AliLog::kWarning, 0, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__)
248// error messages
249#define AliError(message) {AliLog::Message(AliLog::kError, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
250#define AliErrorClass(message) {AliLog::Message(AliLog::kError, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
251#define AliErrorGeneral(scope, message) {AliLog::Message(AliLog::kError, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
253// redirection to error
254#define StdoutToAliError(whatever) REDIRECTSTDOUT(AliLog::kError, 0, ClassName(), whatever)
255#define StderrToAliError(whatever) REDIRECTSTDERR(AliLog::kError, 0, ClassName(), whatever)
256#define ToAliError(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kError, 0, ClassName(), whatever)
257#define StdoutToAliErrorClass(whatever) REDIRECTSTDOUT(AliLog::kError, 0, Class()->GetName(), whatever)
258#define StderrToAliErrorClass(whatever) REDIRECTSTDERR(AliLog::kError, 0, Class()->GetName(), whatever)
259#define ToAliErrorClass(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kError, 0, Class()->GetName(), whatever)
260#define StdoutToAliErrorGeneral(scope, whatever) REDIRECTSTDOUT(AliLog::kError, 0, scope, whatever)
261#define StderrToAliErrorGeneral(scope, whatever) REDIRECTSTDERR(AliLog::kError, 0, scope, whatever)
262#define ToAliErrorGeneral(scope, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kError, 0, scope, whatever)
264// error stream objects
265#define AliErrorStream() AliLog::Stream(AliLog::kError, 0, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__)
266#define AliErrorClassStream() AliLog::Stream(AliLog::kError, 0, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__)
267#define AliErrorGeneralStream(scope) AliLog::Stream(AliLog::kError, 0, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__)
6ab674bd 269
eeb769e2 270// fatal messages
271#define AliFatal(message) {AliLog::Message(AliLog::kFatal, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
272#define AliFatalClass(message) {AliLog::Message(AliLog::kFatal, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
273#define AliFatalGeneral(scope, message) {AliLog::Message(AliLog::kFatal, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
6ab674bd 274