00001
00002
00003
00004
00005
00006
00007
00008 #include <cstdlib>
00009 #include <cassert>
00010 #include "StUCMAppender.h"
00011 #include "TSystem.h"
00012 #include "TString.h"
00013 #include "TObjString.h"
00014
00015 #include "StStarLogger/logging/TxEventLog.h"
00016 #include "StStarLogger/logging/TxEventLogFactory.h"
00017
00018
00019 #include <log4cxx/helpers/loglog.h>
00020 #include <log4cxx/level.h>
00021 #include <log4cxx/helpers/optionconverter.h>
00022 #include <log4cxx/patternlayout.h>
00023
00024 #include "TObjArray.h"
00025 using namespace log4cxx;
00026 using namespace log4cxx::helpers;
00027 using namespace log4cxx::db;
00028 using namespace log4cxx::spi;
00029 using namespace TxLogging;
00030
00031 static int lockUcm =0;
00032
00033 IMPLEMENT_LOG4CXX_OBJECT(StUCMAppender)
00034
00035 StUCMAppender::StUCMAppender(const char *mode)
00036 : connection(0),technology(mode), bufferSize(1),fLastId(0),fIsConnectionOpen(false)
00037 {
00038 fprintf(stderr,"StUCMAppender::StUCMAppender() %p %s %i\n", this, mode, lockUcm);
00039 lockUcm++;
00040 }
00041
00042
00043 StUCMAppender::~StUCMAppender()
00044 {
00045
00046 finalize();
00047 if (connection) {
00048 delete connection;
00049 connection = 0;
00050 }
00051 }
00052
00053
00054 void StUCMAppender::setOption(const String& option,
00055 const String& value)
00056 {
00057 if (equalsIgnoreCase(option, _T("buffersize")))
00058 {
00059 setBufferSize((size_t)OptionConverter::toInt(value, 1));
00060 }
00061 else if (equalsIgnoreCase(option, _T("password")))
00062 {
00063 setPassword(value);
00064 }
00065 else if (equalsIgnoreCase(option, _T("url"))
00066 || equalsIgnoreCase(option, _T("dns")))
00067 {
00068 setURL(value);
00069 }
00070 else if (equalsIgnoreCase(option, _T("user")))
00071 {
00072 setUser(value);
00073 }
00074 else
00075 {
00076 AppenderSkeleton::setOption(name, value);
00077 }
00078 }
00079
00080
00081 void StUCMAppender::append(const spi::LoggingEventPtr& event)
00082 {
00083 if (!this->closed) {
00084 buffer.push_back(event);
00085
00086 if (buffer.size() >= bufferSize)
00087 flushBuffer();
00088 }
00089 }
00090
00091
00092
00093 String StUCMAppender::getLogStatement(const spi::LoggingEventPtr& event)
00094 {
00095 #if (STAR_LOG4CXX_VERSION == 9)
00096 StringBuffer sbuf;
00097 ((StUCMAppender*)this)->getLayout()->format(sbuf, event);
00098 return sbuf.str();
00099 #else
00100 String sbuf;
00101 ((StUCMAppender*)this)->getLayout()->format(sbuf,event,pool);
00102 return sbuf;
00103 #endif
00104 }
00105
00106
00107
00108 void StUCMAppender::closeConnection()
00109 {
00110 if (connection) {
00111
00112 delete connection;
00113 }
00114 connection = 0;
00115 fIsConnectionOpen = false;
00116 }
00117
00118
00119 TxEventLog *StUCMAppender::getConnection()
00120 {
00121 if (!fIsConnectionOpen) {
00122
00123 if (!connection) {
00124 connection = TxEventLogFactory::create("WEB");
00125
00126
00127
00128 if ( getenv("JOBINDEX") && getenv("REQUESTID") ) {
00129 const char *JOBINDEX = getenv("JOBINDEX");
00130 std::string UCMJOB = getenv("REQUESTID");
00131 UCMJOB +=JOBINDEX;
00132
00133
00134
00135 } else {
00136 fprintf(stderr,"StUCMAppender::getConnection() no JOBINDEX/REQUESTID was provided \n");
00137
00138
00139
00140
00141
00142 }
00143 }
00144 }
00145 return connection;
00146 }
00147
00148
00149 void StUCMAppender::close()
00150 {
00151 if (!this->closed) {
00152 flushBuffer();
00153 closeConnection();
00154 this->closed = true;
00155 }
00156 }
00157 #if 0
00158
00159 static void ReplaceVariable(TString &string, const char *var)
00160 {
00161
00162 TString spec;
00163 const char *varValue = gSystem->Getenv(var);
00164 if (!varValue) {
00165
00166 spec = var;
00167 if (spec == "REQUESTID") {
00168 spec.Form("%d",gSystem->GetPid());
00169 varValue= spec.Data();
00170 } else if (spec == "JOBINDEX") {
00171 spec.Form("%d",0);
00172 varValue= spec.Data();
00173 }
00174 }
00175
00176 if (varValue) {
00177 TString fullName = "$"; fullName += var;
00178
00179 string.ReplaceAll(fullName,varValue);
00180 }
00181 }
00182 #endif
00183
00184 void StUCMAppender::flushBuffer()
00185 {
00186
00187 std::list<spi::LoggingEventPtr>::iterator i;
00188 if ( getConnection()) {
00189 for (i = buffer.begin(); i != buffer.end(); i++)
00190 {
00191
00192 const LoggingEventPtr& logEvent = *i;
00193 const LevelPtr &level = logEvent->getLevel();
00194 TxEventLog::Level trackingLevel =TxEventLog::LEVEL_INFO;
00195 if (level == LOG4CXX_LEVEL_FATAL) {
00196 trackingLevel = TxEventLog::LEVEL_FATAL;
00197 } else if (level == LOG4CXX_LEVEL_ERROR) {
00198 trackingLevel = TxEventLog::LEVEL_ERROR;
00199 } else if (level == LOG4CXX_LEVEL_WARN) {
00200 trackingLevel = TxEventLog::LEVEL_WARNING;
00201 } else if (level == LOG4CXX_LEVEL_DEBUG) {
00202 trackingLevel = TxEventLog::LEVEL_DEBUG;
00203 } else {
00204
00205 }
00206
00207
00208
00209 String sql = getLogStatement(logEvent);
00210 TString userKeys = sql.c_str();
00211 TObjArray *pair = userKeys.Tokenize(",");
00212
00213 TIter next(pair);
00214 TObjString *nextPair = 0;
00215 int keyCounter=0;
00216 TString ucmParamters[3];
00217 while ( (nextPair = (TObjString *)next()) && (keyCounter<3)) {
00218 assert(nextPair);
00219 TString nextString = nextPair->String();
00220 TObjArray &keyValue = *nextString.Tokenize("=");
00221
00222
00223
00224 ucmParamters[keyCounter] = ((TObjString *)keyValue[1])->String().Strip();
00225 delete &keyValue;
00226 keyCounter++;
00227 }
00228 String context;
00229 #if (STAR_LOG4CXX_VERSION == 9)
00230 context = logEvent->getNDC();
00231 #else
00232 logEvent->getNDC(context);
00233 #endif
00234
00235 ucmParamters[0].ReplaceAll("'","");ucmParamters[1].ReplaceAll("'","");ucmParamters[2].ReplaceAll("'","");
00236 int ucmStage = ucmParamters[0].Atoi();
00237
00238
00239 static bool taskDone = false;
00240 if (!taskDone) {
00241 taskDone = true;
00242 const char *taskSize = getenv("SUMS_nProcesses");
00243 int nSize = 1;
00244 if (taskSize && taskSize[0]) nSize = atoi(taskSize);
00245 connection->logTask(nSize);
00246 }
00247 fprintf(stderr,"%s\n","StUCMAppender::flushBuffer() . . . . . . . . ." );
00248
00249 connection->logEvent( ucmParamters[1].Data()
00250 , ucmParamters[2].Data()
00251 , trackingLevel
00252 , TxEventLog::Stage(ucmStage)
00253 , context
00254 );
00255 pair->Delete();
00256 delete pair;
00257 }
00258 buffer.clear();
00259 }
00260 closeConnection();
00261 }
00262 #if (STAR_LOG4CXX_VERSION == 10)
00263
00264 void StUCMAppender::append(const spi::LoggingEventPtr& event, log4cxx::helpers::Pool& p)
00265 {
00266 append(event);
00267 }
00268 #endif
00269