*** empty log message ***
[csql.git] / src / base / Logger.cxx
blobe3904ca454583d522ccd14ce48a930568a7ed69a
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 if (fileSize < LOG_ROLLOVER_SIZE) return ;
44 char cmd[MAX_FILE_LEN];
45 int ret =0;
46 int tries=0, totalTries=Conf::config.getMutexRetries();
47 while (tries < totalTries) {
48 ret = os::lockFile(fdLog);
49 if (ret ==0) break;
50 os::usleep(10000);
51 tries++;
53 if (tries == totalTries)
55 printError(ErrLockTimeOut,"Unable to lock log file for rollover %d",fdLog);
56 return ;
59 time_t cnow = ::time(NULL);
60 #ifdef SOLARIS
61 struct std::tm *tmval = localtime(&cnow);
62 #else
63 struct tm *tmval = localtime(&cnow);
64 #endif
65 sprintf(cmd, "cp %s %s.%d-%d-%d:%d:%d:%d", fileName, fileName,
66 tmval->tm_year+1900,
67 tmval->tm_mon+1, tmval->tm_mday, tmval->tm_hour,
68 tmval->tm_min, tmval->tm_sec);
69 ret = system(cmd);
70 if (ret != 0) {
71 printError(ErrWarning, "Unable to rollover the log file");
72 }else {
73 os::truncate(fileName);
75 os::unlockFile(fdLog);
76 return;
79 int Logger::log(LogLevel level, char* filename,
80 int lineNo, char *format, ...)
82 int configLevel = Conf::config.getLogLevel();
83 if (LogOff == configLevel) return 0;
84 int ret =0;
85 if (level <= configLevel )
87 rollOverIfRequired();
88 va_list ap;
89 char mesgBuf[1024];
90 va_start(ap, format);
92 int err = ::vsnprintf(mesgBuf, sizeof(mesgBuf), format,ap);
93 if(err < 0) {
94 return err;
96 char *buffer = new char[MAX_TRACE_LOG_LENGTH];
97 createLogRecord(level, filename, lineNo, mesgBuf, &buffer);
98 int tries=0, totalTries=Conf::config.getMutexRetries();
99 while (tries < totalTries) {
100 ret = os::lockFile(fdLog);
101 if (ret ==0) break;
102 if (ret !=0 && errno == EBADF) {
103 fdLog = os::open(filename, fileOpenAppend,0);
104 if (fdLog == -1)
106 printError(ErrSysInit,"Unable to open log file");
107 delete[] buffer;
108 return ErrSysInit;
111 os::usleep(10000);
112 tries++;
114 if (tries == totalTries)
116 printError(ErrLockTimeOut,"Unable to lock log file %d", ret);
117 delete[] buffer;
118 return -1;
120 int bytesWritten = os::write(fdLog, buffer, strlen(buffer));
121 if (bytesWritten != strlen(buffer))
123 printError(ErrSysInternal, "Unable to write log entry");
124 ret = -1;
126 os::unlockFile(fdLog);
127 delete[] buffer;
129 return ret;
132 DbRetVal Logger::startLogger(char *filename, bool isCreate)
134 configLevel= (LogLevel) Conf::config.getLogLevel();
135 if (LogOff == configLevel) return OK;
136 char cmd[MAX_FILE_LEN];
137 int ret =0;
138 if (isCreate)
140 if (os::fileExists(filename)) {
141 //move the existing log file with timestamp and create new file
142 time_t cnow = ::time(NULL);
143 #ifdef SOLARIS
144 struct std::tm *tmval = localtime(&cnow);
145 #else
146 struct tm *tmval = localtime(&cnow);
147 #endif
148 sprintf(cmd, "cp %s %s.%d-%d-%d:%d:%d:%d", filename, filename,
149 tmval->tm_year+1900,
150 tmval->tm_mon+1, tmval->tm_mday, tmval->tm_hour,
151 tmval->tm_min, tmval->tm_sec);
152 ret = system(cmd);
153 if (ret != 0) {
154 printError(ErrWarning, "Unable to copy old log file");
156 os::truncate(filename);
159 fdLog = os::open(filename, fileOpenAppend,0);
160 if (fdLog == -1)
162 printError(ErrSysInit,"Unable to open log file");
163 return ErrSysInit;
165 return OK;
168 void Logger::stopLogger()
170 if (configLevel == 0) return ;
171 os::close(fdLog);