logging class extended and macros for easy log messages introduced, code changed...
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTLogging.cxx
CommitLineData
3495cce2 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 * Artur Szostak <artursz@iafrica.com> *
9 * for The ALICE Off-line Project. *
10 * *
11 * Permission to use, copy, modify and distribute this software and its *
12 * documentation strictly for non-commercial purposes is hereby granted *
13 * without fee, provided that the above copyright notice appears in all *
14 * copies and that both the copyright notice and this permission notice *
15 * appear in the supporting documentation. The authors make no claims *
16 * about the suitability of this software for any purpose. It is *
17 * provided "as is" without express or implied warranty. *
18 **************************************************************************/
19
20///////////////////////////////////////////////////////////////////////////////
21// //
22// HLT logging tools //
23// //
24///////////////////////////////////////////////////////////////////////////////
25
26#if __GNUC__== 3
27using namespace std;
28#endif
29
30#include <errno.h>
31//#include <string.h>
32#include "AliL3StandardIncludes.h"
33#include "AliHLTLogging.h"
34#include <stdarg.h>
35#include <string.h>
36
37ClassImp(AliHLTLogging)
38
39char AliHLTLogging::fLogBuffer[LOG_BUFFER_SIZE]="";
40char AliHLTLogging::fOriginBuffer[LOG_BUFFER_SIZE]="";
41
85465857 42AliHLTComponent_LogSeverity AliHLTLogging::fGlobalLogFilter=kHLTLogAll;
5ec8e281 43AliHLTfctLogging AliHLTLogging::fLoggingFunc=NULL;
3495cce2 44
45AliHLTLogging::AliHLTLogging()
46{
85465857 47 fpDefaultKeyword=NULL;
48 fpCurrentKeyword=NULL;
49 //fLocalLogFilter=kHLTLogDefault;
50 fLocalLogFilter=kHLTLogAll;
3495cce2 51}
52
53
54AliHLTLogging::~AliHLTLogging()
55{
56}
57
58int AliHLTLogging::Message(void *param, AliHLTComponent_LogSeverity severity, const char* origin, const char* keyword, const char* message) {
59 int iResult=0;
60 const char* strSeverity="";
61 switch (severity) {
62 case kHLTLogBenchmark:
63 strSeverity="benchmark";
64 break;
65 case kHLTLogDebug:
66 strSeverity="debug";
67 break;
68 case kHLTLogInfo:
69 strSeverity="info";
70 break;
71 case kHLTLogWarning:
72 strSeverity="warning";
73 break;
74 case kHLTLogError:
75 strSeverity="error";
76 break;
77 case kHLTLogFatal:
78 strSeverity="fatal";
79 break;
80 default:
81 break;
82 }
85465857 83 cout << "HLT Log " << strSeverity << ": " << origin << " " << message;
84 if (strcmp(keyword, HLT_DEFAULT_LOG_KEYWORD)!=0)
85 cout << " (" << keyword << ")";
86 cout << endl;
3495cce2 87 return iResult;
88}
89
90const char* AliHLTLogging::BuildLogString(const char *format, va_list ap) {
91 int tgtLen=0;
92 int iBufferSize=LOG_BUFFER_SIZE;
93 char* tgtBuffer=fLogBuffer;
94 tgtBuffer[tgtLen]=0;
95
85465857 96#if (defined LOG_PREFIX)
3495cce2 97 tgtLen = snprintf(tgtBuffer, iBufferSize, LOG_PREFIX); // add logging prefix
85465857 98#endif
3495cce2 99 if (tgtLen>=0) {
100 tgtBuffer+=tgtLen; iBufferSize-=tgtLen;
101 tgtLen = vsnprintf(tgtBuffer, iBufferSize, format, ap);
102 if (tgtLen>0) {
103 tgtBuffer+=tgtLen;
104// if (tgtLen<LOG_BUFFER_SIZE-1) {
105// *tgtBuffer++='\n'; // add newline if space in buffer
106// }
107 *tgtBuffer=0; // terminate the buffer
108 }
109 }
110 return fLogBuffer;
111}
112
113int AliHLTLogging::Logging(AliHLTComponent_LogSeverity severity, const char* origin, const char* keyword, const char* format, ... ) {
85465857 114 int iResult=CheckFilter(severity);
115 if (iResult>0) {
116 va_list args;
117 va_start(args, format);
118 if (fLoggingFunc) {
119 iResult = (*fLoggingFunc)(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
120 } else {
121 iResult = Message(NULL/*fParam*/, severity, origin, keyword, AliHLTLogging::BuildLogString(format, args ));
122 }
3495cce2 123 }
85465857 124 return iResult;
3495cce2 125}
126
127int AliHLTLogging::LoggingVarargs( AliHLTComponent_LogSeverity severity, const char* origin_class, const char* origin_func, ... )
128{
85465857 129 int iResult=CheckFilter(severity);
130 if (iResult>0) {
131 int iMaxSize=LOG_BUFFER_SIZE-1;
132 int iPos=0;
133 const char* separator="";
134 fOriginBuffer[iPos]=0;
135 if (origin_class) {
136 if ((int)strlen(origin_class)<iMaxSize-iPos) {
137 strcpy(&fOriginBuffer[iPos], origin_class);
138 iPos+=strlen(origin_class);
139 separator="::";
140 }
3495cce2 141 }
85465857 142 if (origin_func) {
143 if ((int)strlen(origin_func)+(int)strlen(separator)<iMaxSize-iPos) {
144 strcpy(&fOriginBuffer[iPos], separator);
145 iPos+=strlen(separator);
146 strcpy(&fOriginBuffer[iPos], origin_func);
147 iPos+=strlen(origin_func);
148 }
3495cce2 149 }
85465857 150 va_list args;
151 va_start(args, origin_func);
152 const char* format = va_arg(args, const char*);
153
154 const char* message=format;
155 char* qualifier=NULL;
156 if ((qualifier=strchr(format, '%'))!=NULL) {
157 message=AliHLTLogging::BuildLogString(format, args);
158 }
159 if (fLoggingFunc) {
160 iResult=(*fLoggingFunc)(NULL/*fParam*/, severity, fOriginBuffer, GetKeyword(), message);
161 } else {
162 iResult=Message(NULL/*fParam*/, severity, fOriginBuffer, GetKeyword(), message);
163 }
164 va_end(args);
3495cce2 165 }
3495cce2 166 return iResult;
167}
168
169int AliHLTLogging::CheckFilter(AliHLTComponent_LogSeverity severity)
170{
85465857 171 int iResult=severity==kHLTLogNone || (severity&fGlobalLogFilter)>0 && (severity&fLocalLogFilter)>0;
3495cce2 172 return iResult;
173}