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