]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTLogging.h
AliLog handler moved to libHLTrec, minor cleanup in HLT logging
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTLogging.h
1 // @(#) $Id$
2
3 #ifndef ALIHLTLOGGING_H
4 #define ALIHLTLOGGING_H
5 /* This file is property of and copyright by the ALICE HLT Project        * 
6  * ALICE Experiment at CERN, All rights reserved.                         *
7  * See cxx source for full Copyright notice                               */
8
9 /** @file   AliHLTLogging.h
10     @author Matthias Richter, Timm Steinbeck
11     @date   
12     @brief  HLT module logging primitives.
13 */
14
15 #include "AliHLTDataTypes.h"
16 #include "AliHLTStdIncludes.h"
17 #include "TObject.h"
18 #include "TArrayC.h"
19
20 class AliHLTComponentHandler;
21 //#define LOG_PREFIX ""       // logging prefix, for later extensions
22
23 #define ALILOG_WRAPPER_LIBRARY "libHLTrec.so"
24
25 /* the logging macros can be used inside methods of classes which inherit from 
26  * AliHLTLogging
27  */
28 // HLTMessage is not filtered
29 #define HLTMessage( ... )   LoggingVarargs(kHLTLogNone,      NULL , NULL , __FILE__ , __LINE__ , __VA_ARGS__ )
30
31 // function name
32 #if defined(__GNUC__) || defined(__ICC) || defined(__ECC) || defined(__APPLE__)
33 #define FUNCTIONNAME() __FUNCTION__
34 #else
35 #define FUNCTIONNAME() "???"
36 #endif
37
38 // the following macros are filtered by the Global and Local Log Filter
39 #define HLTBenchmark( ... ) LoggingVarargs(kHLTLogBenchmark, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
40 #ifdef __DEBUG
41 #define HLTDebug( ... )     if (CheckFilter(kHLTLogDebug) && CheckGroup(Class_Name())) LoggingVarargs(kHLTLogDebug,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
42 #else
43 #define HLTDebug( ... )
44 #endif
45 #define HLTInfo( ... )      if (CheckFilter(kHLTLogInfo))    LoggingVarargs(kHLTLogInfo,      Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
46 #define HLTWarning( ... )   if (CheckFilter(kHLTLogWarning)) LoggingVarargs(kHLTLogWarning,   Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
47 #define HLTError( ... )     if (CheckFilter(kHLTLogError))   LoggingVarargs(kHLTLogError,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
48 #define HLTFatal( ... )     if (CheckFilter(kHLTLogFatal))   LoggingVarargs(kHLTLogFatal,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
49
50 // helper macro to set the keyword
51 #define HLTLogKeyword(a)    AliHLTKeyword hltlogTmpkey(this, a)
52
53 #define HLT_DEFAULT_LOG_KEYWORD "no key"
54
55 class AliHLTLogging {
56 public:
57   AliHLTLogging();
58   AliHLTLogging(const AliHLTLogging&);
59   AliHLTLogging& operator=(const AliHLTLogging&);
60   virtual ~AliHLTLogging();
61
62   /** set the default key word
63    * the keyword is intended to simplify the use of logging macros
64    */ 
65   void SetDefaultKeyword(const char* keyword) { fpDefaultKeyword=keyword; }
66
67   /**
68    * Set a temporary keyword
69    * returns the old key value
70    */
71   const char* SetKeyword(const char* keyword) 
72     { 
73       const char* currentKeyword=fpCurrentKeyword;
74       fpCurrentKeyword=keyword;
75       return currentKeyword; 
76     }
77
78   /**
79    * Get the current keyword
80    */
81   const char* GetKeyword() const
82     {
83       if (fpCurrentKeyword) return fpCurrentKeyword;
84       else if (fpDefaultKeyword) return fpDefaultKeyword;
85       return HLT_DEFAULT_LOG_KEYWORD;
86     }
87   
88   /**
89    * Init the AliLogging class for use from external package.
90    * This initializes the logging callback. <br>
91    * Only deployed by external users of the C wrapper interface, not used
92    * when running in AliRoot
93    */
94   static int Init(AliHLTfctLogging pFun);
95
96   /**
97    * Init the message trap in AliLog.
98    * This initializes the AliLog trap, the AliLog class is the logging
99    * mechanism of AliRoot. The trap can fetch log messages written to
100    * AliLog, components and detector algorithms can use the AliLog
101    * mechanism to be as close as possible to Offline habits. <br>
102    * Only used with external users of the C wrapper interface, not used
103    * when running in AliRoot
104    */
105   static int InitAliLogTrap(AliHLTComponentHandler* pHandler);
106
107   /**
108    * Init the AliRoot logging function.
109    * All log messages are redirected to AliLog when running in AliRoot.
110    * Note: when running in PubSub, AliLog messages are redirected to PubSub,
111    * see AliHLTLogging::InitAliLogTrap
112    */
113   static int InitAliLogFunc(AliHLTComponentHandler* pHandler);
114
115   /**
116    * Genaral logging function
117    */
118   int Logging( AliHLTComponentLogSeverity severity, const char* origin, const char* keyword, const char* message, ... );
119
120   /*
121    * Logging function with two origin parameters, used by the log macros
122    */
123   int LoggingVarargs(AliHLTComponentLogSeverity severity, 
124                      const char* originClass, const char* originFunc,
125                      const char* file, int line, ... ) const;
126
127   /**
128    * Evaluate the group of the debug message from the class name.
129    * @return 1 if message should be printed
130    */
131   int CheckGroup(const char* originClass) const;
132
133   /**
134    * Set the black list of classes.
135    * If the white list is set, debug messages are skipped for
136    * all classes matching one of the regular expressions in the string.
137    */
138   static int SetBlackList(const char* classnames);
139
140   /**
141    * Set the white list of classes.
142    * If the white list is set, debug messages are only printed for
143    * classes matching one of the regular expressions in the string.
144    */
145   static int SetWhiteList(const char* classnames);
146
147   /**
148    * Apply filter
149    * @return 1 if message should pass
150    */
151   int CheckFilter(AliHLTComponentLogSeverity severity) const;
152
153   /**
154    * Set global logging level
155    * logging filter for all objects
156    */
157   static void SetGlobalLoggingLevel(AliHLTComponentLogSeverity level);
158
159   /**
160    * Get global logging level
161    * logging filter for all objects
162    */
163   static AliHLTComponentLogSeverity GetGlobalLoggingLevel();
164
165   /**
166    * Set local logging level
167    * logging filter for individual object
168    */
169   void SetLocalLoggingLevel(AliHLTComponentLogSeverity level);
170
171   /**
172    * Get local logging level
173    * logging filter for individual object
174    */
175   AliHLTComponentLogSeverity GetLocalLoggingLevel();
176
177   /**
178    * Print message to stdout
179    */
180   static int Message(void * param, AliHLTComponentLogSeverity severity, const char* origin, const char* keyword, const char* message);
181
182   /**
183    * Build the log string from format specifier and variadac arguments
184    * @param format     format string of printf style
185    * @param ap         opened and initialized argument list
186    * @return const char string with the formatted message 
187    */
188   static const char* BuildLogString(const char *format, va_list ap);
189
190   /**
191    * Get parameter given by the external caller.
192    * This functionality is not yet implemented. It is intended
193    * to pass the parameter pointer given to the component at
194    * initialization back to the caller.
195    */
196   virtual void* GetParameter() const {return NULL;}
197
198   /**
199    * Switch logging through AliLog on or off
200    * @param sw          1 = logging through AliLog
201    */
202   void SwitchAliLog(int sw) {fgUseAliLog=(sw!=0);}
203
204   /** target stream for AliRoot logging methods */
205   static ostringstream fgLogstr;                                   //! transient
206
207   /** 
208    * The message function for dynamic use.
209    * In order to avoid dependencies on AliRoot libraries, libHLTbase loads
210    * the library dynamically and looks for the symbol.
211    */
212   typedef int (*AliHLTDynamicMessage)(AliHLTComponentLogSeverity severity, 
213                                       const char* originClass, 
214                                       const char* originFunc,
215                                       const char* file, int line, 
216                                       const char* message); 
217
218   /**
219    * The init function of the message callback for dynamic use.
220    * In order to avoid dependencies on AliRoot libraries, libHLTbase loads
221    * the library dynamically and looks for the symbol.
222    */
223   typedef int (*InitAliDynamicMessageCallback)();
224   
225 protected:
226   /** the AliRoot logging function */
227   static AliHLTDynamicMessage fgAliLoggingFunc;                    //! transient
228
229 private:
230   /** the global logging filter */
231   static  AliHLTComponentLogSeverity fgGlobalLogFilter;            // see above
232   /** the local logging filter for one class */
233   AliHLTComponentLogSeverity fLocalLogFilter;                      // see above
234   /** logging callback from the framework */
235   static AliHLTfctLogging fgLoggingFunc;                           // see above
236   /** default keyword */
237   const char* fpDefaultKeyword;                                    //! transient
238   /** current keyword */
239   const char* fpCurrentKeyword;                                    //! transient
240   /** switch for logging through AliLog, default on */
241   static int fgUseAliLog;                                          // see above
242   /**
243    * The global logging buffer.
244    * The buffer is created with an initial size and grown dynamically on
245    * demand.
246    */
247   static TArrayC fgAliHLTLoggingTarget;                            //! transient
248   
249   /** the maximum size of the buffer */
250   static const int fgkALIHLTLOGGINGMAXBUFFERSIZE;                  //! transient
251
252   /** groups of classes not to print debug messages */
253   static TString fgBlackList;                                      //! transient
254   
255   /** groups of classes not to print debug messages */
256   static TString fgWhiteList;                                      //! transient
257   
258   ClassDef(AliHLTLogging, 3)
259 };
260
261 /* the class AliHLTKeyword is a simple helper class used by the HLTLogKeyword macro
262  * HLTLogKeyword("a keyword") creates an object of AliHLTKeyword which sets the keyword for the logging class
263  * the object is destroyed automatically when the current scope is left and so the keyword is set
264  * to the original value
265  */
266 class AliHLTKeyword {
267  public:
268   AliHLTKeyword()
269     :
270     fpParent(NULL),
271     fpOriginal(NULL)
272     {
273     }
274
275   AliHLTKeyword(AliHLTLogging* parent, const char* keyword)
276     :
277     fpParent(parent),
278     fpOriginal(NULL)
279     {
280       if (parent) {
281         fpOriginal=fpParent->SetKeyword(keyword);
282       }
283     }
284
285   AliHLTKeyword(const AliHLTKeyword& kw)
286     :
287     fpParent(kw.fpParent),
288     fpOriginal(kw.fpOriginal)
289     {
290     }
291
292   AliHLTKeyword& operator=(const AliHLTKeyword& kw)
293     { 
294       fpParent=kw.fpParent;
295       fpOriginal=kw.fpOriginal;
296       return *this;
297     }
298
299   ~AliHLTKeyword()
300     {
301       if (fpParent) {
302         fpParent->SetKeyword(fpOriginal);
303       }
304     }
305
306  private:
307   AliHLTLogging* fpParent;                                         //! transient
308   const char* fpOriginal;                                          //! transient
309 };
310 #endif
311