066babaf0305c9332282e292447d7f67097b8b8a
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTLogging.cxx
1 // $Id: 
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
7  *          Timm Steinbeck <timm@kip.uni-heidelberg.de>                   *
8  *          for The ALICE Off-line Project.                               *
9  *                                                                        *
10  * Permission to use, copy, modify and distribute this software and its   *
11  * documentation strictly for non-commercial purposes is hereby granted   *
12  * without fee, provided that the above copyright notice appears in all   *
13  * copies and that both the copyright notice and this permission notice   *
14  * appear in the supporting documentation. The authors make no claims     *
15  * about the suitability of this software for any purpose. It is          *
16  * provided "as is" without express or implied warranty.                  *
17  **************************************************************************/
18
19 /** @file   AliHLTLogging.cxx
20     @author Matthias Richter, Timm Steinbeck
21     @date   
22     @brief  Implementation of HLT logging primitives.
23 */
24
25 #if __GNUC__>= 3
26 using namespace std;
27 #endif
28
29 #include "AliHLTStdIncludes.h"
30 #include "AliHLTLogging.h"
31 #include "TString.h"
32
33 // global logging buffer
34 #define LOG_BUFFER_SIZE 100
35 char gAliHLTLoggingBuffer[LOG_BUFFER_SIZE]="";
36 char gAliHLTLoggingOriginBuffer[LOG_BUFFER_SIZE]="";
37
38 /** ROOT macro for the implementation of ROOT specific class methods */
39 ClassImp(AliHLTLogging)
40
41 AliHLTLogging::AliHLTLogging()
42   :
43   //fLocalLogFilter(kHLTLogDefault),
44   fLocalLogFilter(kHLTLogAll),
45   fpDefaultKeyword(NULL),
46   fpCurrentKeyword(NULL)
47 {
48 }
49
50 AliHLTLogging::AliHLTLogging(const AliHLTLogging&)
51   :
52   fLocalLogFilter(kHLTLogAll),
53   fpDefaultKeyword(NULL),
54   fpCurrentKeyword(NULL)
55 {
56   HLTFatal("copy constructor untested");
57 }
58
59 AliHLTLogging& AliHLTLogging::operator=(const AliHLTLogging&)
60
61   HLTFatal("assignment operator untested");
62   return *this;
63 }
64
65 AliHLTComponentLogSeverity AliHLTLogging::fGlobalLogFilter=kHLTLogAll;
66 AliHLTfctLogging AliHLTLogging::fLoggingFunc=NULL;
67
68 AliHLTLogging::~AliHLTLogging()
69 {
70 }
71
72 int AliHLTLogging::Init(AliHLTfctLogging pFun) 
73
74   if (fLoggingFunc!=NULL && fLoggingFunc!=pFun) {
75     (*fLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");    
76   }
77   fLoggingFunc=pFun; 
78   return 0;
79 }
80
81 int AliHLTLogging::Message(void *param, AliHLTComponentLogSeverity severity, const char* origin, const char* keyword, const char* message) {
82   int iResult=0;
83   if (param==NULL) {
84     // this is currently just to get rid of the warning "unused parameter"
85   }
86   const char* strSeverity="";
87   switch (severity) {
88   case kHLTLogBenchmark: 
89     strSeverity="benchmark";
90     break;
91   case kHLTLogDebug:
92     strSeverity="debug";
93     break;
94   case kHLTLogInfo:
95     strSeverity="info";
96     break;
97   case kHLTLogWarning:
98     strSeverity="warning";
99     break;
100   case kHLTLogError:
101     strSeverity="error";
102     break;
103   case kHLTLogFatal:
104     strSeverity="fatal";
105     break;
106   default:
107     break;
108   }
109   TString out="HLT Log ";
110   out+=strSeverity;
111   if (origin) {out+=": "; out+=origin;}
112   out+=" "; out+=message;
113   if (keyword!=NULL && strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0) {
114     out+=" ("; out+=keyword; out +=")";
115   }
116   cout << out.Data() << endl;
117   return iResult;
118 }
119
120 const char* AliHLTLogging::BuildLogString(const char *format, va_list ap) {
121   int tgtLen=0;
122   int iBufferSize=LOG_BUFFER_SIZE;
123   char* tgtBuffer=gAliHLTLoggingBuffer;
124   tgtBuffer[tgtLen]=0;
125
126 #if (defined LOG_PREFIX)
127   tgtLen = snprintf(tgtBuffer, iBufferSize, LOG_PREFIX); // add logging prefix
128 #endif
129   if (tgtLen>=0) {
130     tgtBuffer+=tgtLen; iBufferSize-=tgtLen;
131     tgtLen = vsnprintf(tgtBuffer, iBufferSize, format, ap);
132     if (tgtLen>0) {
133       tgtBuffer+=tgtLen;
134 //       if (tgtLen<LOG_BUFFER_SIZE-1) {
135 //      *tgtBuffer++='\n'; // add newline if space in buffer
136 //      }
137       *tgtBuffer=0; // terminate the buffer
138     }
139   }
140   return gAliHLTLoggingBuffer;
141 }
142
143 int AliHLTLogging::Logging(AliHLTComponentLogSeverity severity, const char* origin, const char* keyword, const char* format, ... ) {
144   int iResult=CheckFilter(severity);
145   if (iResult>0) {
146     va_list args;
147     va_start(args, format);
148     if (fLoggingFunc) {
149       iResult = (*fLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
150     } else {
151       iResult = Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
152     }
153   }
154   return iResult;
155 }
156
157 int AliHLTLogging::LoggingVarargs( AliHLTComponentLogSeverity severity, const char* origin_class, const char* origin_func,  ... ) const
158 {
159   int iResult=CheckFilter(severity);
160   if (iResult>0) {
161     int iMaxSize=LOG_BUFFER_SIZE-1;
162     int iPos=0;
163     const char* separator="";
164     gAliHLTLoggingOriginBuffer[iPos]=0;
165     if (origin_class) {
166       if ((int)strlen(origin_class)<iMaxSize-iPos) {
167         strcpy(&gAliHLTLoggingOriginBuffer[iPos], origin_class);
168         iPos+=strlen(origin_class);
169         separator="::";
170       }
171     }
172     if (origin_func) {
173       if ((int)strlen(origin_func)+(int)strlen(separator)<iMaxSize-iPos) {
174         strcpy(&gAliHLTLoggingOriginBuffer[iPos], separator);
175         iPos+=strlen(separator);
176         strcpy(&gAliHLTLoggingOriginBuffer[iPos], origin_func);
177         iPos+=strlen(origin_func);
178       }
179     }
180     va_list args;
181     va_start(args, origin_func);
182     const char* format = va_arg(args, const char*);
183
184     const char* message=format;
185     const char* qualifier=NULL;
186     if ((qualifier=strchr(format, '%'))!=NULL) {
187       message=AliHLTLogging::BuildLogString(format, args);
188     }
189     if (fLoggingFunc) {
190       iResult=(*fLoggingFunc)(NULL/*fParam*/, severity, gAliHLTLoggingOriginBuffer, GetKeyword(), message);
191     } else {
192       iResult=Message(NULL/*fParam*/, severity, gAliHLTLoggingOriginBuffer, GetKeyword(), message);
193     }
194     va_end(args);
195   }
196   return iResult;
197 }
198
199 int AliHLTLogging::CheckFilter(AliHLTComponentLogSeverity severity) const
200 {
201   int iResult=severity==kHLTLogNone || (severity&fGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
202   return iResult;
203 }