1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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::%d::%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 int ret
= mutex_
.tryLock();
55 printError(ErrLockTimeOut
,"Unable to acquire logger Mutex");
59 os::write(fdLog
, buffer
, strlen(buffer
));
67 void Logger::startLogger(char *filename
, bool isCreate
)
70 fdLog
= os::openFile(filename
, fileOpenCreat
,0);
72 fdLog
= os::openFile(filename
, fileOpenAppend
,0);
73 if (fdLog
== -1) printf("Logger failed\n");
74 //TODO::get this value from configuration file
75 configLevel
= LogFinest
;
78 void Logger::stopLogger()