00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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
00035
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
00053
00054
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
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
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
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
00105 configLevel= LogFinest;
00106 return OK;
00107 }
00108
00109 void Logger::stopLogger()
00110 {
00111 os::closeFile(fdLog);
00112 }