]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTLogging.cxx
- setup scripts for running environment added
[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
32 // global logging buffer
33 #define LOG_BUFFER_SIZE 100
34 char gAliHLTLoggingBuffer[LOG_BUFFER_SIZE]="";
35 char gAliHLTLoggingOriginBuffer[LOG_BUFFER_SIZE]="";
36
37 /** ROOT macro for the implementation of ROOT specific class methods */
38 ClassImp(AliHLTLogging)
39
40 AliHLTLogging::AliHLTLogging()
41   :
42   //fLocalLogFilter(kHLTLogDefault),
43   fLocalLogFilter(kHLTLogAll),
44   fpDefaultKeyword(NULL),
45   fpCurrentKeyword(NULL)
46 {
47 }
48
49 AliHLTLogging::AliHLTLogging(const AliHLTLogging&)
50   :
51   fLocalLogFilter(kHLTLogAll),
52   fpDefaultKeyword(NULL),
53   fpCurrentKeyword(NULL)
54 {
55   HLTFatal("copy constructor untested");
56 }
57
58 AliHLTLogging& AliHLTLogging::operator=(const AliHLTLogging&)
59
60   HLTFatal("assignment operator untested");
61   return *this;
62 }
63
64 AliHLTComponent_LogSeverity AliHLTLogging::fGlobalLogFilter=kHLTLogAll;
65 AliHLTfctLogging AliHLTLogging::fLoggingFunc=NULL;
66
67 AliHLTLogging::~AliHLTLogging()
68 {
69 }
70
71 int AliHLTLogging::Init(AliHLTfctLogging pFun) 
72
73   if (fLoggingFunc!=NULL && fLoggingFunc!=pFun) {
74     (*fLoggingFunc)(NULL/*fParam*/, kHLTLogWarning, "AliHLTLogging::Init", "no key", "overriding previously initialized logging function");    
75   }
76   fLoggingFunc=pFun; 
77   return 0;
78 }
79
80 int AliHLTLogging::Message(void *param, AliHLTComponent_LogSeverity severity, const char* origin, const char* keyword, const char* message) {
81   int iResult=0;
82   if (param==NULL) {
83     // this is currently just to get rid of the warning "unused parameter"
84   }
85   const char* strSeverity="";
86   switch (severity) {
87   case kHLTLogBenchmark: 
88     strSeverity="benchmark";
89     break;
90   case kHLTLogDebug:
91     strSeverity="debug";
92     break;
93   case kHLTLogInfo:
94     strSeverity="info";
95     break;
96   case kHLTLogWarning:
97     strSeverity="warning";
98     break;
99   case kHLTLogError:
100     strSeverity="error";
101     break;
102   case kHLTLogFatal:
103     strSeverity="fatal";
104     break;
105   default:
106     break;
107   }
108   cout << "HLT Log " << strSeverity << ": " << origin << " " << message;
109   if (strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0)
110     cout << " (" << keyword << ")";
111   cout << endl;
112   return iResult;
113 }
114
115 const char* AliHLTLogging::BuildLogString(const char *format, va_list ap) {
116   int tgtLen=0;
117   int iBufferSize=LOG_BUFFER_SIZE;
118   char* tgtBuffer=gAliHLTLoggingBuffer;
119   tgtBuffer[tgtLen]=0;
120
121 #if (defined LOG_PREFIX)
122   tgtLen = snprintf(tgtBuffer, iBufferSize, LOG_PREFIX); // add logging prefix
123 #endif
124   if (tgtLen>=0) {
125     tgtBuffer+=tgtLen; iBufferSize-=tgtLen;
126     tgtLen = vsnprintf(tgtBuffer, iBufferSize, format, ap);
127     if (tgtLen>0) {
128       tgtBuffer+=tgtLen;
129 //       if (tgtLen<LOG_BUFFER_SIZE-1) {
130 //      *tgtBuffer++='\n'; // add newline if space in buffer
131 //      }
132       *tgtBuffer=0; // terminate the buffer
133     }
134   }
135   return gAliHLTLoggingBuffer;
136 }
137
138 int AliHLTLogging::Logging(AliHLTComponent_LogSeverity severity, const char* origin, const char* keyword, const char* format, ... ) {
139   int iResult=CheckFilter(severity);
140   if (iResult>0) {
141     va_list args;
142     va_start(args, format);
143     if (fLoggingFunc) {
144       iResult = (*fLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
145     } else {
146       iResult = Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
147     }
148   }
149   return iResult;
150 }
151
152 int AliHLTLogging::LoggingVarargs( AliHLTComponent_LogSeverity severity, const char* origin_class, const char* origin_func,  ... ) const
153 {
154   int iResult=CheckFilter(severity);
155   if (iResult>0) {
156     int iMaxSize=LOG_BUFFER_SIZE-1;
157     int iPos=0;
158     const char* separator="";
159     gAliHLTLoggingOriginBuffer[iPos]=0;
160     if (origin_class) {
161       if ((int)strlen(origin_class)<iMaxSize-iPos) {
162         strcpy(&gAliHLTLoggingOriginBuffer[iPos], origin_class);
163         iPos+=strlen(origin_class);
164         separator="::";
165       }
166     }
167     if (origin_func) {
168       if ((int)strlen(origin_func)+(int)strlen(separator)<iMaxSize-iPos) {
169         strcpy(&gAliHLTLoggingOriginBuffer[iPos], separator);
170         iPos+=strlen(separator);
171         strcpy(&gAliHLTLoggingOriginBuffer[iPos], origin_func);
172         iPos+=strlen(origin_func);
173       }
174     }
175     va_list args;
176     va_start(args, origin_func);
177     const char* format = va_arg(args, const char*);
178
179     const char* message=format;
180     char* qualifier=NULL;
181     if ((qualifier=strchr(format, '%'))!=NULL) {
182       message=AliHLTLogging::BuildLogString(format, args);
183     }
184     if (fLoggingFunc) {
185       iResult=(*fLoggingFunc)(NULL/*fParam*/, severity, gAliHLTLoggingOriginBuffer, GetKeyword(), message);
186     } else {
187       iResult=Message(NULL/*fParam*/, severity, gAliHLTLoggingOriginBuffer, GetKeyword(), message);
188     }
189     va_end(args);
190   }
191   return iResult;
192 }
193
194 int AliHLTLogging::CheckFilter(AliHLTComponent_LogSeverity severity) const
195 {
196   int iResult=severity==kHLTLogNone || (severity&fGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
197   return iResult;
198 }