]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTLogging.h
Allow fetching of the default local logging level.
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTLogging.h
1 //-*- Mode: C++ -*-
2 // $Id$
3
4 #ifndef ALIHLTLOGGING_H
5 #define ALIHLTLOGGING_H
6 /* This file is property of and copyright by the ALICE HLT Project        * 
7  * ALICE Experiment at CERN, All rights reserved.                         *
8  * See cxx source for full Copyright notice                               */
9
10 /** @file   AliHLTLogging.h
11     @author Matthias Richter, Timm Steinbeck
12     @date   
13     @brief  HLT module logging primitives.
14 */
15
16 #include "AliHLTDataTypes.h"
17 #include "AliHLTStdIncludes.h"
18 #include "TString.h"
19 #include "TObject.h"
20 #include "TArrayC.h"
21
22 class AliHLTComponentHandler;
23 //#define LOG_PREFIX ""       // logging prefix, for later extensions
24
25 #define ALILOG_WRAPPER_LIBRARY "libHLTrec.so"
26
27 /* the logging macros can be used inside methods of classes which inherit from 
28  * AliHLTLogging
29  */
30 // function name
31 #if defined(__GNUC__) || defined(__ICC) || defined(__ECC) || defined(__APPLE__)
32 #define FUNCTIONNAME() __FUNCTION__
33 #else
34 #define FUNCTIONNAME() "???"
35 #endif
36
37 #ifndef ALIHLTLOGGINGVARIADICFREE_H
38 // HLTMessage is not filtered
39 #define HLTMessage( ... )   LoggingVarargs(kHLTLogNone,      NULL , NULL , __FILE__ , __LINE__ , __VA_ARGS__ )
40
41 // the following macros are filtered by the Global and Local Log Filter
42 #define HLTLog( level, ... ) if (CheckFilter(level))         LoggingVarargs(level, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
43 #define HLTBenchmark( ... ) LoggingVarargs(kHLTLogBenchmark, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
44 #ifdef __DEBUG
45 #define HLTDebug( ... )     if (CheckFilter(kHLTLogDebug) && CheckGroup(Class_Name())) LoggingVarargs(kHLTLogDebug,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
46 #else
47 #define HLTDebug( ... )
48 #endif
49 #define HLTInfo( ... )      if (CheckFilter(kHLTLogInfo))    LoggingVarargs(kHLTLogInfo,      Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
50 #define HLTWarning( ... )   if (CheckFilter(kHLTLogWarning)) LoggingVarargs(kHLTLogWarning,   Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
51 #define HLTError( ... )     if (CheckFilter(kHLTLogError))   LoggingVarargs(kHLTLogError,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
52 #define HLTFatal( ... )     if (CheckFilter(kHLTLogFatal))   LoggingVarargs(kHLTLogFatal,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
53 #define HLTImportant( ... ) if (CheckFilter(kHLTLogImportant))LoggingVarargs(kHLTLogImportant,Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , __VA_ARGS__ )
54
55 // the same macros are defined variadic free, in that case the message must be complete
56 // include AliHLTLoggingVariadicFree.h
57 #else //ALIHLTLOGGINGVARIADICFREE_H
58 #define HLTMessage( message )   LoggingVarargs(kHLTLogNone,      NULL , NULL , __FILE__ , __LINE__ , message )
59 #define HLTLog( level, message) if (CheckFilter(level))          LoggingVarargs(level, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , message )
60 #define HLTBenchmark( message ) LoggingVarargs(kHLTLogBenchmark, Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , message )
61 #ifdef __DEBUG
62 #define HLTDebug( message )     if (CheckFilter(kHLTLogDebug) && CheckGroup(Class_Name())) LoggingVarargs(kHLTLogDebug,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , message )
63 #else
64 #define HLTDebug( message )
65 #endif
66 #define HLTInfo( message )      if (CheckFilter(kHLTLogInfo))    LoggingVarargs(kHLTLogInfo,      Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , message )
67 #define HLTWarning( message )   if (CheckFilter(kHLTLogWarning)) LoggingVarargs(kHLTLogWarning,   Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , message )
68 #define HLTError( message )     if (CheckFilter(kHLTLogError))   LoggingVarargs(kHLTLogError,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , message )
69 #define HLTFatal( message )     if (CheckFilter(kHLTLogFatal))   LoggingVarargs(kHLTLogFatal,     Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , message )
70 #define HLTImportant( message ) if (CheckFilter(kHLTLogImportant))LoggingVarargs(kHLTLogImportant,Class_Name() , FUNCTIONNAME() , __FILE__ , __LINE__ , message )
71 #endif //ALIHLTLOGGINGVARIADICFREE_H
72
73 // helper macro to set the keyword
74 #define HLTLogKeyword(a)    AliHLTKeyword hltlogTmpkey(this, a)
75
76 #define HLT_DEFAULT_LOG_KEYWORD "no key"
77
78 /**
79  * @class AliHLTLogging
80  * Basic logging class. All classes inherit the besic HLT logging functionality.
81  * Logging levels are controlled by a global logging filter and a local logging
82  * filter.
83  * 
84  * @section alihlt_logging_levels Logging Levels
85  * Logging levels are switched by a bit pattern,  AliHLTComponentLogSeverity {
86  * - ::kHLTLogNone no logging (0)
87  * - ::kHLTLogBenchmark benchmark messages (0x1)
88  * - ::kHLTLogDebug debug messages (0x2)
89  * - ::kHLTLogInfo info messages (0x4)
90  * - ::kHLTLogWarning warning messages (0x8)
91  * - ::kHLTLogError error messages (0x10)
92  * - ::kHLTLogFatal fatal error messages (0x20)
93  * - ::kHLTLogImportant few important messages not to be filtered out (0x40)
94  * - ::kHLTLogAll special value to enable all messages (0x7f)
95  * - ::kHLTLogDefault the default logging level: Warning, Error, Fatal, Important (0x79)
96  *
97  * @section alihlt_logging_filter Logging Filters
98  * The class provides a global and a local logging filter, the AND beween both
99  * defines whether a message is printed or not.
100  *
101  * The global filter is by default set to ::kHLTLogAll. Please note that AliHLTSystem
102  * changes the global logging level to ::kHLTLogDefault. The global filter can be
103  * adjusted by means of SetGlobalLoggingLevel().
104  *
105  * The local filter is set to ::kHLTLogAll and can be adjusted by
106  * SetLocalLoggingLevel(). The default can be changed for all objects by
107  * SetLocalLoggingDefault(). Please note that a change of the default level only
108  * applies to objects generated after the change of the default.
109  *
110  * @section alihlt_logging_external Redirection
111  * - external logging function
112  * - keyword
113  *
114  * @section alihlt_logging_aliroot AliRoot Redirection
115  * - switching of redirection
116  * - logging options in AliSimulation/AliReconstruction
117  *
118  * @ingroup alihlt_component
119  */
120 class AliHLTLogging {
121 public:
122   AliHLTLogging();
123   AliHLTLogging(const AliHLTLogging&);
124   AliHLTLogging& operator=(const AliHLTLogging&);
125   virtual ~AliHLTLogging();
126
127   /** set the default key word
128    * the keyword is intended to simplify the use of logging macros
129    */ 
130   void SetDefaultKeyword(const char* keyword) { fpDefaultKeyword=keyword; }
131
132   /**
133    * Set a temporary keyword
134    * returns the old key value
135    */
136   const char* SetKeyword(const char* keyword) 
137     { 
138       const char* currentKeyword=fpCurrentKeyword;
139       fpCurrentKeyword=keyword;
140       return currentKeyword; 
141     }
142
143   /**
144    * Get the current keyword
145    */
146   const char* GetKeyword() const
147     {
148       if (fpCurrentKeyword) return fpCurrentKeyword;
149       else if (fpDefaultKeyword) return fpDefaultKeyword;
150       return HLT_DEFAULT_LOG_KEYWORD;
151     }
152   
153   /**
154    * Init the AliLogging class for use from external package.
155    * This initializes the logging callback. <br>
156    * Only deployed by external users of the C wrapper interface, not used
157    * when running in AliRoot
158    */
159   static int Init(AliHLTfctLogging pFun);
160
161   /**
162    * Init the message trap in AliLog.
163    * This initializes the AliLog trap, the AliLog class is the logging
164    * mechanism of AliRoot. The trap can fetch log messages written to
165    * AliLog, components and detector algorithms can use the AliLog
166    * mechanism to be as close as possible to Offline habits. <br>
167    * Only used with external users of the C wrapper interface, not used
168    * when running in AliRoot
169    */
170   static int InitAliLogTrap(AliHLTComponentHandler* pHandler);
171
172   /**
173    * Init the AliRoot logging function.
174    * All log messages are redirected to AliLog when running in AliRoot.
175    * Note: when running in PubSub, AliLog messages are redirected to PubSub,
176    * see AliHLTLogging::InitAliLogTrap
177    */
178   static int InitAliLogFunc(AliHLTComponentHandler* pHandler);
179
180   /**
181    * Genaral logging function
182    */
183   int Logging( AliHLTComponentLogSeverity severity, const char* origin, const char* keyword, const char* message, ... );
184
185   /**
186    * Logging function with two origin parameters, used by the log macros
187    */
188   virtual int LoggingVarargs(AliHLTComponentLogSeverity severity, 
189                              const char* originClass, const char* originFunc,
190                              const char* file, int line, ... ) const;
191
192   /**
193    * Send formatted string to the different channels
194    */
195   int SendMessage(AliHLTComponentLogSeverity severity, 
196                   const char* originClass, const char* originFunc,
197                   const char* file, int line,
198                   const char* message) const;
199
200   /**
201    * Evaluate the group of the debug message from the class name.
202    * @return 1 if message should be printed
203    */
204   int CheckGroup(const char* originClass) const;
205
206   /**
207    * Set the black list of classes.
208    * If the white list is set, debug messages are skipped for
209    * all classes matching one of the regular expressions in the string.
210    */
211   static int SetBlackList(const char* classnames);
212
213   /**
214    * Set the white list of classes.
215    * If the white list is set, debug messages are only printed for
216    * classes matching one of the regular expressions in the string.
217    */
218   static int SetWhiteList(const char* classnames);
219
220   /**
221    * Apply filter
222    * @return 1 if message should pass
223    */
224   int CheckFilter(AliHLTComponentLogSeverity severity) const;
225
226   /**
227    * Set global logging level
228    * logging filter for all objects
229    */
230   static void SetGlobalLoggingLevel(AliHLTComponentLogSeverity level);
231
232   /**
233    * Get global logging level
234    * logging filter for all objects
235    */
236   static AliHLTComponentLogSeverity GetGlobalLoggingLevel();
237
238   /**
239    * Set local logging level
240    * logging filter for individual object
241    */
242   virtual void SetLocalLoggingLevel(AliHLTComponentLogSeverity level);
243
244   /**
245    * Set local logging default
246    * Default logging filter for individual objects.
247    */
248   static void SetLocalLoggingDefault(AliHLTComponentLogSeverity level);
249
250   /**
251    * Get default setting for local logging filter for individual objects.
252    */
253   static AliHLTComponentLogSeverity GetLocalLoggingDefault() { return fgLocalLogDefault; }
254
255   /**
256    * Get local logging level
257    * logging filter for individual object
258    */
259   AliHLTComponentLogSeverity GetLocalLoggingLevel();
260
261   /**
262    * Print message to stdout
263    */
264   static int Message(void * param, AliHLTComponentLogSeverity severity, const char* origin, const char* keyword, const char* message);
265
266   /**
267    * Build the log string from format specifier and variadac arguments
268    * @param format     format string of printf style
269    * @param ap         opened and initialized argument list
270    * @param bAppend    append to current content
271    * @return const char string with the formatted message 
272    */
273   static const char* BuildLogString(const char *format, va_list ap, bool bAppend=false);
274
275   /**
276    * Set the log string from format specifier and from variable arguments.
277    * @param format     format string of printf style
278    * @return const char string with the formatted message 
279    */
280   static const char* SetLogString(const void* p, const char* pfmt, const char *format, ... );
281
282   /**
283    * Get parameter given by the external caller.
284    * This functionality is not yet implemented. It is intended
285    * to pass the parameter pointer given to the component at
286    * initialization back to the caller.
287    */
288   virtual void* GetParameter() const {return NULL;}
289
290   /**
291    * Switch logging through AliLog on or off
292    * @param sw          1 = logging through AliLog
293    */
294   void SwitchAliLog(int sw) {fgUseAliLog=(sw!=0);}
295
296   /** target stream for AliRoot logging methods */
297   static ostringstream fgLogstr;                                   //! transient
298
299   /** 
300    * The message function for dynamic use.
301    * In order to avoid dependencies on AliRoot libraries, libHLTbase loads
302    * the library dynamically and looks for the symbol.
303    */
304   typedef int (*AliHLTDynamicMessage)(AliHLTComponentLogSeverity severity, 
305                                       const char* originClass, 
306                                       const char* originFunc,
307                                       const char* file, int line, 
308                                       const char* message); 
309
310   /**
311    * The init function of the message callback for dynamic use.
312    * In order to avoid dependencies on AliRoot libraries, libHLTbase loads
313    * the library dynamically and looks for the symbol.
314    */
315   typedef int (*InitAliDynamicMessageCallback)();
316   
317 protected:
318   /** the AliRoot logging function */
319   static AliHLTDynamicMessage fgAliLoggingFunc;                    //! transient
320
321 private:
322   /** the global logging filter */
323   static  AliHLTComponentLogSeverity fgGlobalLogFilter;            // see above
324   /** the local logging filter for one class */
325   AliHLTComponentLogSeverity fLocalLogFilter;                      // see above
326   /** the global logging filter */
327   static  AliHLTComponentLogSeverity fgLocalLogDefault;            // see above
328   /** logging callback from the framework */
329   static AliHLTfctLogging fgLoggingFunc;                           // see above
330   /** default keyword */
331   const char* fpDefaultKeyword;                                    //! transient
332   /** current keyword */
333   const char* fpCurrentKeyword;                                    //! transient
334   /** switch for logging through AliLog, default on */
335   static int fgUseAliLog;                                          // see above
336   /**
337    * The global logging buffer.
338    * The buffer is created with an initial size and grown dynamically on
339    * demand.
340    */
341   static TArrayC fgAliHLTLoggingTarget;                            //! transient
342   
343   /** the maximum size of the buffer */
344   static const int fgkALIHLTLOGGINGMAXBUFFERSIZE;                  //! transient
345
346   /** groups of classes not to print debug messages */
347   static TString fgBlackList;                                      //! transient
348   
349   /** groups of classes not to print debug messages */
350   static TString fgWhiteList;                                      //! transient
351   
352   ClassDef(AliHLTLogging, 0)
353 };
354
355 /* the class AliHLTKeyword is a simple helper class used by the HLTLogKeyword macro
356  * HLTLogKeyword("a keyword") creates an object of AliHLTKeyword which sets the keyword for the logging class
357  * the object is destroyed automatically when the current scope is left and so the keyword is set
358  * to the original value
359  */
360 class AliHLTKeyword {
361  public:
362   AliHLTKeyword()
363     :
364     fpParent(NULL),
365     fpOriginal(NULL)
366     {
367     }
368
369   AliHLTKeyword(AliHLTLogging* parent, const char* keyword)
370     :
371     fpParent(parent),
372     fpOriginal(NULL)
373     {
374       if (parent) {
375         fpOriginal=fpParent->SetKeyword(keyword);
376       }
377     }
378
379   AliHLTKeyword(const AliHLTKeyword& kw)
380     :
381     fpParent(kw.fpParent),
382     fpOriginal(kw.fpOriginal)
383     {
384     }
385
386   AliHLTKeyword& operator=(const AliHLTKeyword& kw)
387     { 
388       fpParent=kw.fpParent;
389       fpOriginal=kw.fpOriginal;
390       return *this;
391     }
392
393   ~AliHLTKeyword()
394     {
395       if (fpParent) {
396         fpParent->SetKeyword(fpOriginal);
397       }
398     }
399
400  private:
401   AliHLTLogging* fpParent;                                         //! transient
402   const char* fpOriginal;                                          //! transient
403 };
404 #endif
405