submitting patch from enterprise version
[csql.git] / src / storage / Logger.cxx
blob7e9b854d79c710ba2722f136c19bc7dc3d25821a
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include<Debug.h>
17 #include<Config.h>
18 Logger Conf::logger;
19 int Logger::createLogRecord(LogLevel level, char* filename,
20 int lineNo, char* message, char **buffer)
22 char tempBuffer[25];
23 struct timeval timeStamp;
24 os::gettimeofday(&timeStamp);
25 struct tm* tempTm = os::localtime(&timeStamp.tv_sec);
26 #if defined(SOLARIS) && defined(REMOTE_SOLARIS)
27 strftime(tempBuffer, 25, "%d/%m/%Y %H:%M:%S", (struct std::tm*) tempTm);
28 #else
29 strftime(tempBuffer, 25, "%d/%m/%Y %H:%M:%S", tempTm);
30 #endif
31 snprintf(*buffer, MAX_TRACE_LOG_LENGTH, "%s.%6d:%5d:%10lu:%s:%d:%s\n",
32 tempBuffer, timeStamp.tv_usec,
33 os::getpid(),
34 os::getthrid(),
35 filename, lineNo,
36 message);
37 return 0;
39 void Logger::rollOverIfRequired()
41 char *fileName = Conf::config.getLogFile();
42 int fileSize = os::getFileSize(fileName);
43 char cmd[MAX_FILE_LEN];
44 int ret =0;
45 int tries=0, totalTries=3;
46 while (tries < totalTries) {
47 ret = os::lockFile(fdLog);
48 if (ret ==0) break;
49 os::usleep(10000);
50 tries++;
52 if (tries == totalTries)
54 printError(ErrLockTimeOut,"Unable to lock log file for rollover");
55 return ;
58 if (fileSize > LOG_ROLLOVER_SIZE) {
59 time_t cnow = ::time(NULL);
60 struct tm *tmval = localtime(&cnow);
61 sprintf(cmd, "cp %s %s.%d-%d-%d:%d:%d:%d", fileName, fileName,
62 tmval->tm_year+1900,
63 tmval->tm_mon+1, tmval->tm_mday, tmval->tm_hour,
64 tmval->tm_min, tmval->tm_sec);
65 ret = system(cmd);
66 if (ret != 0) {
67 printError(ErrWarning, "Unable to rollover the log file");
69 truncate(fileName, 0);
71 os::unlockFile(fdLog);
72 return;
75 int Logger::log(LogLevel level, char* filename,
76 int lineNo, char *format, ...)
78 int configLevel = Conf::config.getLogLevel();
79 if (LogOff == configLevel) return 0;
80 int ret =0;
81 if (level <= configLevel )
83 rollOverIfRequired();
84 va_list ap;
85 char mesgBuf[1024];
86 va_start(ap, format);
88 int err = ::vsnprintf(mesgBuf, sizeof(mesgBuf), format,ap);
89 if(err < 0) {
90 return err;
92 char *buffer = new char[MAX_TRACE_LOG_LENGTH];
93 createLogRecord(level, filename, lineNo, mesgBuf, &buffer);
94 int tries=0, totalTries=3;
95 while (tries < totalTries) {
96 ret = os::lockFile(fdLog);
97 if (ret ==0) break;
98 os::usleep(10000);
99 tries++;
101 if (tries == totalTries)
103 printError(ErrLockTimeOut,"Unable to lock log file %d", ret);
104 delete[] buffer;
105 return -1;
107 int bytesWritten = os::write(fdLog, buffer, strlen(buffer));
108 if (bytesWritten != strlen(buffer))
110 printf("Unable to write log entry");
111 ret = -1;
113 os::unlockFile(fdLog);
114 delete[] buffer;
116 return ret;
119 DbRetVal Logger::startLogger(char *filename, bool isCreate)
121 configLevel= (LogLevel) Conf::config.getLogLevel();
122 if (LogOff == configLevel) return OK;
123 char cmd[MAX_FILE_LEN];
124 int ret =0;
125 if (isCreate)
127 if (::access(filename, F_OK) == 0 ) {
128 //move the existing log file with timestamp and create new file
129 time_t cnow = ::time(NULL);
130 struct tm *tmval = localtime(&cnow);
131 sprintf(cmd, "cp %s %s.%d-%d-%d:%d:%d:%d", filename, filename,
132 tmval->tm_year+1900,
133 tmval->tm_mon+1, tmval->tm_mday, tmval->tm_hour,
134 tmval->tm_min, tmval->tm_sec);
135 ret = system(cmd);
136 if (ret != 0) {
137 printError(ErrWarning, "Unable to copy old log file");
139 truncate(filename, 0);
142 fdLog = os::openFile(filename, fileOpenAppend,0);
143 if (fdLog == -1)
145 printError(ErrSysInit,"Unable to open log file");
146 return ErrSysInit;
148 return OK;
151 void Logger::stopLogger()
153 if (configLevel == 0) return ;
154 os::closeFile(fdLog);