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