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 char cmd
[MAX_FILE_LEN
];
45 int tries
=0, totalTries
=3;
46 while (tries
< totalTries
) {
47 ret
= os::lockFile(fdLog
);
52 if (tries
== totalTries
)
54 printError(ErrLockTimeOut
,"Unable to lock log file for rollover");
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
,
63 tmval
->tm_mon
+1, tmval
->tm_mday
, tmval
->tm_hour
,
64 tmval
->tm_min
, tmval
->tm_sec
);
67 printError(ErrWarning
, "Unable to rollover the log file");
69 truncate(fileName
, 0);
71 os::unlockFile(fdLog
);
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;
81 if (level
<= configLevel
)
88 int err
= ::vsnprintf(mesgBuf
, sizeof(mesgBuf
), format
,ap
);
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
);
101 if (tries
== totalTries
)
103 printError(ErrLockTimeOut
,"Unable to lock log file %d", ret
);
107 int bytesWritten
= os::write(fdLog
, buffer
, strlen(buffer
));
108 if (bytesWritten
!= strlen(buffer
))
110 printf("Unable to write log entry");
113 os::unlockFile(fdLog
);
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
];
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
,
133 tmval
->tm_mon
+1, tmval
->tm_mday
, tmval
->tm_hour
,
134 tmval
->tm_min
, tmval
->tm_sec
);
137 printError(ErrWarning
, "Unable to copy old log file");
139 truncate(filename
, 0);
142 fdLog
= os::openFile(filename
, fileOpenAppend
,0);
145 printError(ErrSysInit
,"Unable to open log file");
151 void Logger::stopLogger()
153 if (configLevel
== 0) return ;
154 os::closeFile(fdLog
);