00001 #include "eventLog.h"
00002 #include "rmClientTypes.h"
00003 #include "rmClient.h"
00004
00010 eventLog::eventLog(eventQuery * aEventQuery)
00011 {
00012 mDefaultQuery = 0;
00013 mLogFilePath.erase();
00014 mEventQuery = aEventQuery;
00015
00016 }
00017
00022 eventLog::~eventLog()
00023 {
00024 posix_log_close(mLog);
00025 }
00026
00037 int
00038 eventLog::init()
00039 {
00040 int ret;
00041
00042 const char * path = NULL;
00043
00044 if(mLogFilePath.size())
00045 path = mLogFilePath.c_str();
00046
00047 ret = posix_log_open(&mLog, path);
00048 if(ret)
00049 return RMCLIENT_ERROR_LOG_OPEN;
00050
00051 return RMCLIENT_SUCCESS;
00052 }
00053
00064 const EventLog *
00065 eventLog::getConstElement(unsigned long aID)
00066 {
00067 int ret;
00068
00069 if(mLastEntry.entry.log_recid == aID)
00070 return & mLastEntry;
00071 else {
00072 ret = findId(aID);
00073 if(ret)
00074 return NULL;
00075 else
00076 return & mLastEntry;
00077 }
00078 }
00079
00090 int
00091 eventLog::findId(unsigned long aID)
00092 {
00093 unsigned long next;
00094 return SearchAndReadLog(FIND_ID, aID, next);
00095 }
00096
00108 int
00109 eventLog::getNextId(unsigned long aID, unsigned long & aNextID)
00110 {
00111 return SearchAndReadLog(FIND_NEXT_ID, aID, aNextID);
00112 }
00113
00120 int
00121 eventLog::getInteger32(RMAttribute aName, unsigned long aID, long & aValue)
00122 {
00123 aValue = -1;
00124
00125 const EventLog * log = getConstElement(aID);
00126 if(!log) {
00127 return RMCLIENT_ERROR_LOG;
00128 }
00129
00130
00131 switch(aName) {
00132
00133 case EVENTLOG_FORMAT:
00134 aValue = log->entry.log_format;
00135 break;
00136
00137 case EVENTLOG_TYPE:
00138 aValue = log->entry.log_event_type;
00139 break;
00140
00141 case EVENTLOG_FACILITY:
00142 aValue = log->entry.log_facility;
00143 break;
00144
00145 case EVENTLOG_SEVERITY:
00146 aValue = log->entry.log_severity;
00147 break;
00148
00149 default:
00150 return RMCLIENT_ERROR_NOSUCHNAME;
00151 }
00152
00153 return RMCLIENT_SUCCESS;
00154 }
00155
00163 int
00164 eventLog::getUnsigned(RMAttribute aName, unsigned long aID, unsigned long & aValue)
00165 {
00166 aValue = 0;
00167
00168 if(aName == EVENTLOG_DEFAULTQUERY) {
00169 aValue = mDefaultQuery;
00170 return RMCLIENT_SUCCESS;
00171 }
00172
00173 const EventLog * log = getConstElement(aID);
00174 if(!log)
00175 return RMCLIENT_ERROR_LOG;
00176
00177 switch(aName) {
00178
00179 case EVENTLOG_SIZE:
00180 aValue = log->entry.log_size;
00181 break;
00182
00183 case EVENTLOG_UID:
00184 aValue = log->entry.log_uid;
00185 break;
00186
00187 case EVENTLOG_GID:
00188 aValue = log->entry.log_gid;
00189 break;
00190
00191 case EVENTLOG_PID:
00192 aValue = log->entry.log_pid;
00193 break;
00194
00195 case EVENTLOG_PGID:
00196 aValue = log->entry.log_pgrp;
00197 break;
00198
00199 case EVENTLOG_FLAG:
00200 aValue = log->entry.log_flags;
00201 break;
00202
00203 case EVENTLOG_THREAD:
00204 aValue = log->entry.log_thread;
00205 break;
00206
00207 case EVENTLOG_PROCESSOR:
00208 aValue = log->entry.log_processor;
00209 break;
00210
00211 default:
00212 return RMCLIENT_ERROR_NOSUCHNAME;
00213 }
00214
00215 return RMCLIENT_SUCCESS;
00216 }
00217
00225 int
00226 eventLog::getString(RMAttribute aName, unsigned long aID, char * & aBuf)
00227 {
00228 aBuf = NULL;
00229 size_t len;
00230 char * buf = NULL;
00231 char * time;
00232
00233 const EventLog * log = getConstElement(aID);
00234 if(!log)
00235 return RMCLIENT_ERROR_LOG;
00236
00237 switch(aName) {
00238
00239 case EVENTLOG_DEFAULTFILE:
00240 len = mLogFilePath.size();
00241 if(len) {
00242
00243 buf = (char *) malloc(sizeof(char)*len);
00244 if(!buf)
00245 return RMCLIENT_ERROR_MEMORY;
00246
00247 mLogFilePath.copy(buf, len);
00248 }
00249 break;
00250
00251 case EVENTLOG_TIMESTAMP:
00252
00253 time = ctime(&(log->entry.log_time.tv_sec));
00254 if(time) {
00255 buf = (char *) malloc (sizeof(char) * (strlen(time) + 1));
00256 if(!buf)
00257 return RMCLIENT_ERROR_MEMORY;
00258
00259 strncpy(buf, time, strlen(time) + 1);
00260 }
00261 break;
00262
00263 case EVENTLOG_STRING:
00264 if(log->entry.log_format == POSIX_LOG_STRING) {
00265 len = log->entry.log_size;
00266 if(len) {
00267 buf = (char *)malloc(sizeof(char)*len);
00268 if(!buf)
00269 return RMCLIENT_ERROR_MEMORY;
00270
00271 log->data.copy(buf, len);
00272 }
00273 }
00274 else
00275 return RMCLIENT_ERROR_NOSUCHNAME;
00276
00277 break;
00278
00279 default:
00280 return RMCLIENT_ERROR_NOSUCHNAME;
00281 }
00282
00283 aBuf = buf;
00284 return RMCLIENT_SUCCESS;
00285
00286 }
00287
00301 int
00302 eventLog::getBinary(RMAttribute aName, unsigned long aID, char * & aBuf, unsigned long & aLength)
00303 {
00304 aLength = 0;
00305 aBuf = NULL;
00306
00307 size_t len;
00308 char * buf;
00309
00310 const EventLog * log = getConstElement(aID);
00311 if(!log)
00312 return RMCLIENT_ERROR_LOG;
00313
00314 if(aName == EVENTLOG_BINARY && log->entry.log_format == POSIX_LOG_BINARY) {
00315
00316 len = log->entry.log_size;
00317 if(len) {
00318 buf = (char *)malloc(sizeof(char)*len);
00319 if(!buf)
00320 return RMCLIENT_ERROR_MEMORY;
00321
00322 log->data.copy(buf, len);
00323 }
00324 }
00325 else
00326 return RMCLIENT_ERROR_NOSUCHNAME;
00327
00328 aBuf = buf;
00329 aLength = len;
00330 return RMCLIENT_SUCCESS;
00331 }
00332
00340 int
00341 eventLog::setString(RMAttribute aName, unsigned long aID, char * aBuf)
00342 {
00343 int ret;
00344
00345 if(aName != EVENTLOG_DEFAULTFILE)
00346 return RMCLIENT_ERROR_NOSUCHNAME;
00347
00348 ret = posix_log_close(mLog);
00349 if(ret)
00350 return RMCLIENT_ERROR_LOG_CLOSE;
00351
00352 ret = posix_log_open(&mLog, aBuf);
00353 if(ret) {
00354
00355 posix_log_open(&mLog, 0);
00356
00357 return RMCLIENT_ERROR_LOG_OPEN;
00358 }
00359
00360 mLogFilePath = aBuf;
00361
00362 return RMCLIENT_SUCCESS;
00363 }
00364
00372 int
00373 eventLog::setUnsigned(RMAttribute aName, unsigned long aID, unsigned long aValue)
00374 {
00375 int ret;
00376
00377 if(aName != EVENTLOG_DEFAULTQUERY)
00378 return RMCLIENT_ERROR_NOSUCHNAME;
00379
00380 if (aValue > 0) {
00381
00382 ret = ::findId(EVENTQUERY, aValue);
00383 if(ret)
00384 return RMCLIENT_ERROR_NOSUCHVALUE;
00385 }
00386
00387 mDefaultQuery = aValue;
00388
00389 return RMCLIENT_SUCCESS;
00390 }
00391
00398 int
00399 eventLog::SearchAndReadLog(enum SearchOption aMethod, unsigned long aID, unsigned long & aNextID)
00400 {
00401 int ret;
00402
00403 posix_log_query_t query;
00404
00405 size_t buf(36);
00406
00407 char id[buf];
00408
00409 snprintf(id, buf, "%lu", aID);
00410
00411 string qs;
00412
00413 aNextID = 0;
00414
00415 if(mDefaultQuery) {
00416
00417 const EventQuery * defaultQuery = mEventQuery->getConstElement(mDefaultQuery);
00418 if(!defaultQuery)
00419 return RMCLIENT_ERROR_QUERY;
00420
00421 qs = defaultQuery->qstr;
00422 qs += " && ";
00423 }
00424
00425 if(aMethod == FIND_ID)
00426 qs += " recid = " ;
00427 else
00428 qs += " recid > " ;
00429
00430 qs += id;
00431
00432 ret = posix_log_query_create(qs.c_str(), POSIX_LOG_PRPS_SEEK, &query, 0, 0);
00433 if(ret)
00434 return RMCLIENT_ERROR_QUERY_CREATE;
00435
00436
00437 ret = posix_log_seek(mLog, 0, POSIX_LOG_SEEK_START);
00438 if(ret)
00439 return RMCLIENT_ERROR_LOG_SEEK;
00440
00441
00442 ret = posix_log_seek(mLog, &query, POSIX_LOG_SEEK_FIRST);
00443 if(ret)
00444 return RMCLIENT_ERROR_LOG;
00445
00446
00447 ret = posix_log_read(mLog, &(mLastEntry.entry), 0, 0);
00448 if(ret)
00449 return RMCLIENT_ERROR_LOG_READ;
00450
00451
00452 size_t bufSize = mLastEntry.entry.log_size;
00453 if(bufSize) {
00454
00455 void * logBuf = malloc( sizeof(char) * bufSize);
00456 if(!logBuf)
00457 return RMCLIENT_ERROR_MEMORY;
00458
00459
00460 ret = posix_log_seek(mLog, &query, POSIX_LOG_SEEK_BACKWARD);
00461 if(ret)
00462 return RMCLIENT_ERROR_LOG_SEEK;
00463
00464
00465 ret = posix_log_read(mLog, &(mLastEntry.entry), logBuf, bufSize);
00466 if(ret)
00467 return RMCLIENT_ERROR_LOG_READ;
00468
00469 mLastEntry.data.assign((const char *)logBuf, bufSize);
00470
00471
00472 free(logBuf);
00473 }
00474
00475 aNextID = mLastEntry.entry.log_recid;
00476
00477 return RMCLIENT_SUCCESS;
00478 }
00479
00480
00481