src/server/Logger.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by www.databasecache.com                           *
00003  *   Contact: praba_tuty@databasecache.com                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015   ***************************************************************************/
00016 #include<Debug.h>
00017 int Logger::createLogRecord(LogLevel level, char* filename,
00018                               int lineNo, char* message, char **buffer)
00019 {
00020     char    tempBuffer[25];
00021     struct timeval timeStamp;
00022     os::gettimeofday(&timeStamp);
00023     struct tm*      tempTm = os::localtime(&timeStamp.tv_sec);
00024     strftime(tempBuffer, 25, "%d/%m/%Y %H:%M:%S.", tempTm);
00025     snprintf(*buffer, MAX_TRACE_LOG_LENGTH, "%s::%s:%d::%s::%d::%d::%lu::%s\n",
00026         levelNames[level], tempBuffer, timeStamp.tv_usec,
00027         filename, lineNo,
00028         os::getpid(),
00029         os::getthrid(),
00030         message);
00031     return 0;
00032 }
00033 
00034 //TODO::Multiple files: If it exceeeds some configured size, it rolls over to
00035 //next with suffix like file.1, file.2, ...
00036 int Logger::log(LogLevel level, char* filename,
00037                   int lineNo, char *format, ...)
00038 {
00039     if (LogOff == configLevel) return 0;
00040     if (level <= configLevel )
00041     {
00042         va_list ap;
00043         char mesgBuf[1024]; 
00044         va_start(ap, format);
00045 
00046         int err = ::vsnprintf(mesgBuf, sizeof(mesgBuf), format,ap);
00047         if(err < 0) {
00048             return err;
00049         } 
00050         char *buffer = new char[MAX_TRACE_LOG_LENGTH];
00051         createLogRecord(level, filename, lineNo, mesgBuf, &buffer);
00052         //TODO::There is some issue in locking. Need to look into this and then
00053         //uncomment the below lines
00054         //int ret = mutex_.tryLock(5, 100000);
00055         int ret = 0;
00056         if (ret != 0)
00057         {
00058             printError(ErrLockTimeOut,"Unable to acquire logger Mutex");
00059             delete[] buffer;
00060             return -1;
00061         }        
00062         os::write(fdLog, buffer, strlen(buffer));
00063         os::fsync(fdLog);
00064         //mutex_.releaseLock();
00065         delete[] buffer;
00066     }
00067     return 0;
00068 }
00069 
00070 DbRetVal Logger::startLogger(char *filename, bool isCreate)
00071 {
00072     char file[256];
00073     int i =0;
00074     if (isCreate) 
00075     {
00076         while (true)
00077         {
00078             sprintf(file, "%s.%d", filename, i);
00079             //check if file exists. If not create it
00080             if (::access(file, F_OK) != 0 ) break;
00081             i++;
00082         }
00083         fdLog = os::openFile(file, fileOpenCreat,0);
00084     }
00085     else
00086     {
00087         int newlyCreatedID =0;
00088         while (true)
00089         {
00090             sprintf(file, "%s.%d", filename, i);
00091             //check if file exists. If not create it
00092             if (::access(file, F_OK) != 0 ) break;
00093             newlyCreatedID = i;
00094             i++;
00095         }
00096         sprintf(file, "%s.%d", filename, newlyCreatedID );
00097         fdLog = os::openFile(file, fileOpenAppend,0);
00098     }
00099     if (fdLog == -1)
00100     {
00101         printError(ErrSysInit,"Unable to create log file. Check whether server started\n");
00102         return ErrSysInit;
00103     }
00104     //TODO::get this value from configuration file
00105     configLevel= LogFinest;
00106     return OK;
00107 }
00108 
00109 void Logger::stopLogger()
00110 {
00111     os::closeFile(fdLog);
00112 }

Generated on Mon Jun 9 22:37:15 2008 for csql by  doxygen 1.4.7