3 /* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * See cxx source for full Copyright notice */
9 /// class for logging debug, info and error messages
13 #include <TObjArray.h>
17 class AliLog: public TObject {
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);
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 SetPrintRepetitions(Bool_t on);
56 static void WriteToFile(const char* name, Int_t option = 0);
58 // the following public methods are used by the preprocessor macros
59 // and should not be called directly
60 static Bool_t IsDebugEnabled() {return fgDebugEnabled;}
61 static Int_t GetDebugLevel(const char* module, const char* className);
62 static void Message(UInt_t level, const char* message,
63 const char* module, const char* className,
64 const char* function, const char* file, Int_t line);
65 static void Debug(UInt_t level, const char* message,
66 const char* module, const char* className,
67 const char* function, const char* file, Int_t line);
69 static Int_t RedirectStdoutTo(EType type, UInt_t level, const char* module,
70 const char* className, const char* function,
71 const char* file, Int_t line, Bool_t print);
72 static Int_t RedirectStderrTo(EType type, UInt_t level, const char* module,
73 const char* className, const char* function,
74 const char* file, Int_t line, Bool_t print);
75 static void RestoreStdout(Int_t original);
76 static void RestoreStderr(Int_t original);
78 static ostream& Stream(EType type, UInt_t level,
79 const char* module, const char* className,
80 const char* function, const char* file, Int_t line);
83 AliLog(const AliLog& log);
84 AliLog& operator = (const AliLog& log);
86 void ReadEnvSettings();
88 static void RootErrorHandler(Int_t level, Bool_t abort,
89 const char* location, const char* message);
91 void CloseFile(Int_t type);
92 FILE* GetOutputStream(Int_t type);
94 UInt_t GetLogLevel(const char* module, const char* className) const;
95 void PrintMessage(UInt_t type, const char* message,
96 const char* module, const char* className,
98 const char* file, Int_t line);
99 void PrintRepetitions();
101 Int_t RedirectTo(FILE* stream, EType type, UInt_t level,
102 const char* module, const char* className,
103 const char* function,
104 const char* file, Int_t line, Bool_t print);
106 ostream& GetStream(EType type, UInt_t level,
107 const char* module, const char* className,
108 const char* function, const char* file, Int_t line);
110 enum {kDebugOffset = kDebug-1};
112 static AliLog* fgInstance; //! pointer to current instance
114 static Bool_t fgDebugEnabled; // flag for debug en-/disabling
116 UInt_t fGlobalLogLevel; // global logging level
117 TObjArray fModuleDebugLevels; // debug levels for modules
118 TObjArray fClassDebugLevels; // debug levels for classes
120 Int_t fOutputTypes[kMaxType]; // types of output streams
121 TString fFileNames[kMaxType]; // file names
122 FILE* fOutputFiles[kMaxType]; //! log output files
123 ofstream* fOutputStreams[kMaxType]; //! log output streams
125 Bool_t fPrintType[kMaxType]; // print type on/off
126 Bool_t fPrintModule[kMaxType]; // print module on/off
127 Bool_t fPrintScope[kMaxType]; // print scope/class name on/off
128 Bool_t fPrintLocation[kMaxType]; // print file and line on/off
130 Bool_t fPrintRepetitions; // print number of repetitions instead of repeated message on/off
132 Int_t fRepetitions; //! counter of repetitions
133 UInt_t fLastType; //! type of last message
134 TString fLastMessage; //! last message
135 TString fLastModule; //! module name of last message
136 TString fLastClassName; //! class name of last message
137 TString fLastFunction; //! function name of last message
138 TString fLastFile; //! file name of last message
139 Int_t fLastLine; //! line number of last message
141 ClassDef(AliLog, 1) // class for logging debug, info and error messages
147 #define MODULENAME() __MODULE__
149 #define MODULENAME() "NoModule"
153 #if defined(__GNUC__) || defined(__ICC) || defined(__ECC) || defined(__APPLE__)
154 #define FUNCTIONNAME() __FUNCTION__
155 // #elif defined(__HP_aCC) || defined(__alpha) || defined(__DECCXX)
156 // #define FUNCTIONNAME() __FUNC__
158 #define FUNCTIONNAME() "???"
162 #define REDIRECTSTDOUT(type, level, scope, whatever) {Int_t originalStdout = AliLog::RedirectStdoutTo(type, level, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__, kFALSE); whatever; AliLog::RestoreStdout(originalStdout);}
163 #define REDIRECTSTDERR(type, level, scope, whatever) {Int_t originalStderr = AliLog::RedirectStderrTo(type, level, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__, kFALSE); whatever; AliLog::RestoreStderr(originalStderr);}
164 #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);}
169 #define AliDebugLevel() -1
170 #define AliDebugLevelClass() -1
171 #define AliDebugLevelGeneral(scope) -1
173 #define AliDebugLevel() ((AliLog::IsDebugEnabled()) ? AliLog::GetDebugLevel(MODULENAME(), ClassName()) : -1)
174 #define AliDebugLevelClass() ((AliLog::IsDebugEnabled()) ? AliLog::GetDebugLevel(MODULENAME(), Class()->GetName()) : -1)
175 #define AliDebugLevelGeneral(scope) ((AliLog::IsDebugEnabled()) ? AliLog::GetDebugLevel(MODULENAME(), scope) : -1)
180 #define AliDebug(level, message)
181 #define AliDebugClass(level, message)
182 #define AliDebugGeneral(scope, level, message)
184 #define AliDebug(level, message) {if (AliLog::IsDebugEnabled()) AliLog::Debug(level, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
185 #define AliDebugClass(level, message) {if (AliLog::IsDebugEnabled()) AliLog::Debug(level, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
186 #define AliDebugGeneral(scope, level, message) {if (AliLog::IsDebugEnabled()) AliLog::Debug(level, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
189 // redirection to debug
190 #define StdoutToAliDebug(level, whatever) REDIRECTSTDOUT(AliLog::kDebug, level, ClassName(), whatever)
191 #define StderrToAliDebug(level, whatever) REDIRECTSTDERR(AliLog::kDebug, level, ClassName(), whatever)
192 #define ToAliDebug(level, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kDebug, level, ClassName(), whatever)
193 #define StdoutToAliDebugClass(level, whatever) REDIRECTSTDOUT(AliLog::kDebug, level, Class()->GetName(), whatever)
194 #define StderrToAliDebugClass(level, whatever) REDIRECTSTDERR(AliLog::kDebug, level, Class()->GetName(), whatever)
195 #define ToAliDebugClass(level, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kDebug, level, Class()->GetName(), whatever)
196 #define StdoutToAliDebugGeneral(scope, level, whatever) REDIRECTSTDOUT(AliLog::kDebug, level, scope, whatever)
197 #define StderrToAliDebugGeneral(scope, level, whatever) REDIRECTSTDERR(AliLog::kDebug, level, scope, whatever)
198 #define ToAliDebugGeneral(scope, level, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kDebug, level, scope, whatever)
200 // debug stream objects
201 #define AliDebugStream(level) AliLog::Stream(AliLog::kDebug, level, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__)
202 #define AliDebugClassStream(level) AliLog::Stream(AliLog::kDebug, level, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__)
203 #define AliDebugGeneralStream(scope, level) AliLog::Stream(AliLog::kDebug, level, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__)
208 #define AliInfo(message)
209 #define AliInfoClass(message)
210 #define AliInfoGeneral(scope, message)
212 #define AliInfo(message) {AliLog::Message(AliLog::kInfo, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
213 #define AliInfoClass(message) {AliLog::Message(AliLog::kInfo, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
214 #define AliInfoGeneral(scope, message) {AliLog::Message(AliLog::kInfo, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
217 // redirection to info
218 #define StdoutToAliInfo(whatever) REDIRECTSTDOUT(AliLog::kInfo, 0, ClassName(), whatever)
219 #define StderrToAliInfo(whatever) REDIRECTSTDERR(AliLog::kInfo, 0, ClassName(), whatever)
220 #define ToAliInfo(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kInfo, 0, ClassName(), whatever)
221 #define StdoutToAliInfoClass(whatever) REDIRECTSTDOUT(AliLog::kInfo, 0, Class()->GetName(), whatever)
222 #define StderrToAliInfoClass(whatever) REDIRECTSTDERR(AliLog::kInfo, 0, Class()->GetName(), whatever)
223 #define ToAliInfoClass(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kInfo, 0, Class()->GetName(), whatever)
224 #define StdoutToAliInfoGeneral(scope, whatever) REDIRECTSTDOUT(AliLog::kInfo, 0, scope, whatever)
225 #define StderrToAliInfoGeneral(scope, whatever) REDIRECTSTDERR(AliLog::kInfo, 0, scope, whatever)
226 #define ToAliInfoGeneral(scope, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kInfo, 0, scope, whatever)
228 // info stream objects
229 #define AliInfoStream() AliLog::Stream(AliLog::kInfo, 0, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__)
230 #define AliInfoClassStream() AliLog::Stream(AliLog::kInfo, 0, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__)
231 #define AliInfoGeneralStream(scope) AliLog::Stream(AliLog::kInfo, 0, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__)
235 #ifdef LOG_NO_WARNING
236 #define AliWarning(message)
237 #define AliWarningClass(message)
238 #define AliWarningGeneral(scope, message)
240 #define AliWarning(message) {AliLog::Message(AliLog::kWarning, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
241 #define AliWarningClass(message) {AliLog::Message(AliLog::kWarning, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
242 #define AliWarningGeneral(scope, message) {AliLog::Message(AliLog::kWarning, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
245 // redirection to warning
246 #define StdoutToAliWarning(whatever) REDIRECTSTDOUT(AliLog::kWarning, 0, ClassName(), whatever)
247 #define StderrToAliWarning(whatever) REDIRECTSTDERR(AliLog::kWarning, 0, ClassName(), whatever)
248 #define ToAliWarning(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kWarning, 0, ClassName(), whatever)
249 #define StdoutToAliWarningClass(whatever) REDIRECTSTDOUT(AliLog::kWarning, 0, Class()->GetName(), whatever)
250 #define StderrToAliWarningClass(whatever) REDIRECTSTDERR(AliLog::kWarning, 0, Class()->GetName(), whatever)
251 #define ToAliWarningClass(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kWarning, 0, Class()->GetName(), whatever)
252 #define StdoutToAliWarningGeneral(scope, whatever) REDIRECTSTDOUT(AliLog::kWarning, 0, scope, whatever)
253 #define StderrToAliWarningGeneral(scope, whatever) REDIRECTSTDERR(AliLog::kWarning, 0, scope, whatever)
254 #define ToAliWarningGeneral(scope, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kWarning, 0, scope, whatever)
256 // warning stream objects
257 #define AliWarningStream() AliLog::Stream(AliLog::kWarning, 0, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__)
258 #define AliWarningClassStream() AliLog::Stream(AliLog::kWarning, 0, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__)
259 #define AliWarningGeneralStream(scope) AliLog::Stream(AliLog::kWarning, 0, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__)
263 #define AliError(message) {AliLog::Message(AliLog::kError, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
264 #define AliErrorClass(message) {AliLog::Message(AliLog::kError, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
265 #define AliErrorGeneral(scope, message) {AliLog::Message(AliLog::kError, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}
267 // redirection to error
268 #define StdoutToAliError(whatever) REDIRECTSTDOUT(AliLog::kError, 0, ClassName(), whatever)
269 #define StderrToAliError(whatever) REDIRECTSTDERR(AliLog::kError, 0, ClassName(), whatever)
270 #define ToAliError(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kError, 0, ClassName(), whatever)
271 #define StdoutToAliErrorClass(whatever) REDIRECTSTDOUT(AliLog::kError, 0, Class()->GetName(), whatever)
272 #define StderrToAliErrorClass(whatever) REDIRECTSTDERR(AliLog::kError, 0, Class()->GetName(), whatever)
273 #define ToAliErrorClass(whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kError, 0, Class()->GetName(), whatever)
274 #define StdoutToAliErrorGeneral(scope, whatever) REDIRECTSTDOUT(AliLog::kError, 0, scope, whatever)
275 #define StderrToAliErrorGeneral(scope, whatever) REDIRECTSTDERR(AliLog::kError, 0, scope, whatever)
276 #define ToAliErrorGeneral(scope, whatever) REDIRECTSTDOUTANDSTDERR(AliLog::kError, 0, scope, whatever)
278 // error stream objects
279 #define AliErrorStream() AliLog::Stream(AliLog::kError, 0, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__)
280 #define AliErrorClassStream() AliLog::Stream(AliLog::kError, 0, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__)
281 #define AliErrorGeneralStream(scope) AliLog::Stream(AliLog::kError, 0, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__)
285 #define AliFatal(message) {AliLog::Message(AliLog::kFatal, message, MODULENAME(), ClassName(), FUNCTIONNAME(), __FILE__, __LINE__);}
286 #define AliFatalClass(message) {AliLog::Message(AliLog::kFatal, message, MODULENAME(), Class()->GetName(), FUNCTIONNAME(), __FILE__, __LINE__);}
287 #define AliFatalGeneral(scope, message) {AliLog::Message(AliLog::kFatal, message, MODULENAME(), scope, FUNCTIONNAME(), __FILE__, __LINE__);}