Initial revision
[csql.git] / src / server / Logger.c
blobf00ca326081217e42323ec80f7fedf401faafdcf
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.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 int Logger::createLogRecord(LogLevel level, char* filename,
18 int lineNo, char* message, char **buffer)
20 char tempBuffer[25];
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,
27 filename, lineNo,
28 os::getpid(),
29 os::getthrid(),
30 message);
31 return 0;
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 )
42 va_list ap;
43 char mesgBuf[1024];
44 va_start(ap, format);
46 int err = ::vsnprintf(mesgBuf, sizeof(mesgBuf), format,ap);
47 if(err < 0) {
48 return err;
50 char *buffer = new char[MAX_TRACE_LOG_LENGTH];
51 createLogRecord(level, filename, lineNo, mesgBuf, &buffer);
52 int ret = mutex_.tryLock();
53 if (ret != 0)
55 printError(ErrLockTimeOut,"Unable to acquire logger Mutex");
56 delete[] buffer;
57 return -1;
59 os::write(fdLog, buffer, strlen(buffer));
60 os::fsync(fdLog);
61 mutex_.releaseLock();
62 delete[] buffer;
64 return 0;
67 void Logger::startLogger(char *filename, bool isCreate)
69 if (isCreate)
70 fdLog = os::openFile(filename, fileOpenCreat,0);
71 else
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()
80 os::closeFile(fdLog);