10 #include "StUCMAppender.h"
13 #include "TObjString.h"
15 #include "StStarLogger/logging/TxEventLog.h"
16 #include "StStarLogger/logging/TxEventLogFactory.h"
19 #include <log4cxx/helpers/loglog.h>
20 #include <log4cxx/level.h>
21 #include <log4cxx/helpers/optionconverter.h>
22 #include <log4cxx/patternlayout.h>
24 #include "TObjArray.h"
25 using namespace log4cxx;
26 using namespace log4cxx::helpers;
27 using namespace log4cxx::db;
28 using namespace log4cxx::spi;
29 using namespace TxLogging;
31 static int lockUcm =0;
33 IMPLEMENT_LOG4CXX_OBJECT(StUCMAppender)
35 StUCMAppender::StUCMAppender(const
char *mode)
36 : connection(0),technology(mode), bufferSize(1),fLastId(0),fIsConnectionOpen(false)
38 fprintf(stderr,
"StUCMAppender::StUCMAppender() %p %s %i\n",
this, mode, lockUcm);
43 StUCMAppender::~StUCMAppender()
54 void StUCMAppender::setOption(
const String& option,
57 if (equalsIgnoreCase(option, _T(
"buffersize")))
59 setBufferSize((
size_t)OptionConverter::toInt(value, 1));
61 else if (equalsIgnoreCase(option, _T(
"password")))
65 else if (equalsIgnoreCase(option, _T(
"url"))
66 || equalsIgnoreCase(option, _T(
"dns")))
70 else if (equalsIgnoreCase(option, _T(
"user")))
76 AppenderSkeleton::setOption(name, value);
81 void StUCMAppender::append(
const spi::LoggingEventPtr& event)
84 buffer.push_back(event);
86 if (buffer.size() >= bufferSize)
93 String StUCMAppender::getLogStatement(
const spi::LoggingEventPtr& event)
95 #if (STAR_LOG4CXX_VERSION == 9)
97 ((StUCMAppender*)
this)->getLayout()->format(sbuf, event);
101 ((StUCMAppender*)
this)->getLayout()->format(sbuf,event,pool);
108 void StUCMAppender::closeConnection()
115 fIsConnectionOpen =
false;
121 if (!fIsConnectionOpen) {
124 connection = TxEventLogFactory::create(
"WEB");
128 if ( getenv(
"JOBINDEX") && getenv(
"REQUESTID") ) {
129 const char *JOBINDEX = getenv(
"JOBINDEX");
130 std::string UCMJOB = getenv(
"REQUESTID");
136 fprintf(stderr,
"StUCMAppender::getConnection() no JOBINDEX/REQUESTID was provided \n");
149 void StUCMAppender::close()
159 static void ReplaceVariable(TString &
string,
const char *var)
163 const char *varValue = gSystem->Getenv(var);
167 if (spec ==
"REQUESTID") {
168 spec.Form(
"%d",gSystem->GetPid());
169 varValue= spec.Data();
170 }
else if (spec ==
"JOBINDEX") {
172 varValue= spec.Data();
177 TString fullName =
"$"; fullName += var;
179 string.ReplaceAll(fullName,varValue);
184 void StUCMAppender::flushBuffer()
187 std::list<spi::LoggingEventPtr>::iterator i;
188 if ( getConnection()) {
189 for (i = buffer.begin(); i != buffer.end(); i++)
192 const LoggingEventPtr& logEvent = *i;
193 const LevelPtr &level = logEvent->getLevel();
194 TxEventLog::Level trackingLevel =TxEventLog::LEVEL_INFO;
195 if (level == LOG4CXX_LEVEL_FATAL) {
196 trackingLevel = TxEventLog::LEVEL_FATAL;
197 }
else if (level == LOG4CXX_LEVEL_ERROR) {
198 trackingLevel = TxEventLog::LEVEL_ERROR;
199 }
else if (level == LOG4CXX_LEVEL_WARN) {
200 trackingLevel = TxEventLog::LEVEL_WARNING;
201 }
else if (level == LOG4CXX_LEVEL_DEBUG) {
202 trackingLevel = TxEventLog::LEVEL_DEBUG;
209 String sql = getLogStatement(logEvent);
210 TString userKeys = sql.c_str();
211 TObjArray *pair = userKeys.Tokenize(
",");
214 TObjString *nextPair = 0;
216 TString ucmParamters[3];
217 while ( (nextPair = (TObjString *)next()) && (keyCounter<3)) {
219 TString nextString = nextPair->String();
220 TObjArray &keyValue = *nextString.Tokenize(
"=");
224 ucmParamters[keyCounter] = ((TObjString *)keyValue[1])->String().Strip();
229 #if (STAR_LOG4CXX_VERSION == 9)
230 context = logEvent->getNDC();
232 logEvent->getNDC(context);
235 ucmParamters[0].ReplaceAll(
"'",
"");ucmParamters[1].ReplaceAll(
"'",
"");ucmParamters[2].ReplaceAll(
"'",
"");
236 int ucmStage = ucmParamters[0].Atoi();
239 static bool taskDone =
false;
242 const char *taskSize = getenv(
"SUMS_nProcesses");
244 if (taskSize && taskSize[0]) nSize = atoi(taskSize);
245 connection->logTask(nSize);
247 fprintf(stderr,
"%s\n",
"StUCMAppender::flushBuffer() . . . . . . . . ." );
249 connection->logEvent( ucmParamters[1].Data()
250 , ucmParamters[2].Data()
252 , TxEventLog::Stage(ucmStage)
262 #if (STAR_LOG4CXX_VERSION == 10)
264 void StUCMAppender::append(
const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p)