1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
19 int Logger::createLogRecord(LogLevel level
, char* filename
,
20 int lineNo
, char* message
, char **buffer
)
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
);
29 strftime(tempBuffer
, 25, "%d/%m/%Y %H:%M:%S", tempTm
);
31 snprintf(*buffer
, MAX_TRACE_LOG_LENGTH
, "%s.%6d:%5d:%10lu:%s:%d:%s\n",
32 tempBuffer
, timeStamp
.tv_usec
,
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
];
46 int tries
=0, totalTries
=Conf::config
.getMutexRetries();
47 while (tries
< totalTries
) {
48 ret
= os::lockFile(fdLog
);
53 if (tries
== totalTries
)
55 printError(ErrLockTimeOut
,"Unable to lock log file for rollover %d",fdLog
);
59 time_t cnow
= ::time(NULL
);
61 struct std::tm
*tmval
= localtime(&cnow
);
63 struct tm
*tmval
= localtime(&cnow
);
65 sprintf(cmd
, "cp %s %s.%d-%d-%d:%d:%d:%d", fileName
, fileName
,
67 tmval
->tm_mon
+1, tmval
->tm_mday
, tmval
->tm_hour
,
68 tmval
->tm_min
, tmval
->tm_sec
);
71 printError(ErrWarning
, "Unable to rollover the log file");
73 os::truncate(fileName
);
75 os::unlockFile(fdLog
);
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;
85 if (level
<= configLevel
)
92 int err
= ::vsnprintf(mesgBuf
, sizeof(mesgBuf
), format
,ap
);
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
);
102 if (ret
!=0 && errno
== EBADF
) {
103 fdLog
= os::open(filename
, fileOpenAppend
,0);
106 printError(ErrSysInit
,"Unable to open log file");
114 if (tries
== totalTries
)
116 printError(ErrLockTimeOut
,"Unable to lock log file %d", ret
);
120 int bytesWritten
= os::write(fdLog
, buffer
, strlen(buffer
));
121 if (bytesWritten
!= strlen(buffer
))
123 printError(ErrSysInternal
, "Unable to write log entry");
126 os::unlockFile(fdLog
);
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
];
140 if (os::fileExists(filename
)) {
141 //move the existing log file with timestamp and create new file
142 time_t cnow
= ::time(NULL
);
144 struct std::tm
*tmval
= localtime(&cnow
);
146 struct tm
*tmval
= localtime(&cnow
);
148 sprintf(cmd
, "cp %s %s.%d-%d-%d:%d:%d:%d", filename
, filename
,
150 tmval
->tm_mon
+1, tmval
->tm_mday
, tmval
->tm_hour
,
151 tmval
->tm_min
, tmval
->tm_sec
);
154 printError(ErrWarning
, "Unable to copy old log file");
156 os::truncate(filename
);
159 fdLog
= os::open(filename
, fileOpenAppend
,0);
162 printError(ErrSysInit
,"Unable to open log file");
168 void Logger::stopLogger()
170 if (configLevel
== 0) return ;