FreeBSD Porting
[csql.git] / src / storage / Logger.cxx
blob901475b5e6d277160e98d4ca335eaf087d461a8d
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=Conf::config.getMutexRetries();
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 #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");
73 truncate(fileName, 0);
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 os::usleep(10000);
103 tries++;
105 if (tries == totalTries)
107 printError(ErrLockTimeOut,"Unable to lock log file %d", ret);
108 delete[] buffer;
109 return -1;
111 int bytesWritten = os::write(fdLog, buffer, strlen(buffer));
112 if (bytesWritten != strlen(buffer))
114 printf("Unable to write log entry");
115 ret = -1;
117 os::unlockFile(fdLog);
118 delete[] buffer;
120 return ret;
123 DbRetVal Logger::startLogger(char *filename, bool isCreate)
125 configLevel= (LogLevel) Conf::config.getLogLevel();
126 if (LogOff == configLevel) return OK;
127 char cmd[MAX_FILE_LEN];
128 int ret =0;
129 if (isCreate)
131 if (::access(filename, F_OK) == 0 ) {
132 //move the existing log file with timestamp and create new file
133 time_t cnow = ::time(NULL);
134 #ifdef SOLARIS
135 struct std::tm *tmval = localtime(&cnow);
136 #else
137 struct tm *tmval = localtime(&cnow);
138 #endif
139 sprintf(cmd, "cp %s %s.%d-%d-%d:%d:%d:%d", filename, filename,
140 tmval->tm_year+1900,
141 tmval->tm_mon+1, tmval->tm_mday, tmval->tm_hour,
142 tmval->tm_min, tmval->tm_sec);
143 ret = system(cmd);
144 if (ret != 0) {
145 printError(ErrWarning, "Unable to copy old log file");
147 truncate(filename, 0);
150 fdLog = os::openFile(filename, fileOpenAppend,0);
151 if (fdLog == -1)
153 printError(ErrSysInit,"Unable to open log file");
154 return ErrSysInit;
156 return OK;
159 void Logger::stopLogger()
161 if (configLevel == 0) return ;
162 os::closeFile(fdLog);