64 bit build fix
[csql.git] / src / server / Logger.cxx
blob42f1d1389ca835565a79e2ae8de59a1d6c3f7331
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.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::%lu::%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 //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);
55 int ret = 0;
56 if (ret != 0)
58 printError(ErrLockTimeOut,"Unable to acquire logger Mutex");
59 delete[] buffer;
60 return -1;
62 os::write(fdLog, buffer, strlen(buffer));
63 os::fsync(fdLog);
64 //mutex_.releaseLock();
65 delete[] buffer;
67 return 0;
70 DbRetVal Logger::startLogger(char *filename, bool isCreate)
72 char file[256];
73 int i =0;
74 if (isCreate)
76 while (true)
78 sprintf(file, "%s.%d", filename, i);
79 //check if file exists. If not create it
80 if (::access(file, F_OK) != 0 ) break;
81 i++;
83 fdLog = os::openFile(file, fileOpenCreat,0);
85 else
87 int newlyCreatedID =0;
88 while (true)
90 sprintf(file, "%s.%d", filename, i);
91 //check if file exists. If not create it
92 if (::access(file, F_OK) != 0 ) break;
93 newlyCreatedID = i;
94 i++;
96 sprintf(file, "%s.%d", filename, newlyCreatedID );
97 fdLog = os::openFile(file, fileOpenAppend,0);
99 if (fdLog == -1)
101 printError(ErrSysInit,"Unable to create log file. Check whether server started\n");
102 return ErrSysInit;
104 //TODO::get this value from configuration file
105 configLevel= LogFinest;
106 return OK;
109 void Logger::stopLogger()
111 os::closeFile(fdLog);