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 ***************************************************************************/
17 int Logger::createLogRecord(LogLevel level
, char* filename
,
18 int lineNo
, char* message
, char **buffer
)
21 struct timeval timeStamp
;
22 os::gettimeofday(&timeStamp
);
23 struct tm
* tempTm
= os::localtime(&timeStamp
.tv_sec
);
24 strftime(tempBuffer
, 25, "%d/%m/%Y %H:%M:%S.", tempTm
);
25 snprintf(*buffer
, MAX_TRACE_LOG_LENGTH
, "%s::%s:%d::%s::%d::%d::%lu::%s\n",
26 levelNames
[level
], tempBuffer
, timeStamp
.tv_usec
,
34 //TODO::Multiple files: If it exceeeds some configured size, it rolls over to
35 //next with suffix like file.1, file.2, ...
36 int Logger::log(LogLevel level
, char* filename
,
37 int lineNo
, char *format
, ...)
39 if (LogOff
== configLevel
) return 0;
40 if (level
<= configLevel
)
46 int err
= ::vsnprintf(mesgBuf
, sizeof(mesgBuf
), format
,ap
);
50 char *buffer
= new char[MAX_TRACE_LOG_LENGTH
];
51 createLogRecord(level
, filename
, lineNo
, mesgBuf
, &buffer
);
52 //TODO::There is some issue in locking. Need to look into this and then
53 //uncomment the below lines
54 //int ret = mutex_.tryLock(5, 100000);
58 printError(ErrLockTimeOut
,"Unable to acquire logger Mutex");
62 os::write(fdLog
, buffer
, strlen(buffer
));
64 //mutex_.releaseLock();
70 DbRetVal
Logger::startLogger(char *filename
, bool isCreate
)
78 sprintf(file
, "%s.%d", filename
, i
);
79 //check if file exists. If not create it
80 if (::access(file
, F_OK
) != 0 ) break;
83 fdLog
= os::openFile(file
, fileOpenCreat
,0);
87 int newlyCreatedID
=0;
90 sprintf(file
, "%s.%d", filename
, i
);
91 //check if file exists. If not create it
92 if (::access(file
, F_OK
) != 0 ) break;
96 sprintf(file
, "%s.%d", filename
, newlyCreatedID
);
97 fdLog
= os::openFile(file
, fileOpenAppend
,0);
101 printError(ErrSysInit
,"Unable to create log file. Check whether server started\n");
104 //TODO::get this value from configuration file
105 configLevel
= LogFinest
;
109 void Logger::stopLogger()
111 os::closeFile(fdLog
);