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