mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / src / common / logger / FileLogHandler.cpp
blob2a2b7b66b0d4415f210b634284cc8dd1db38efa9
1 /* Copyright (c) 2003-2006 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include <ndb_global.h>
17 #include <FileLogHandler.hpp>
18 #include <File.hpp>
21 // PUBLIC
24 FileLogHandler::FileLogHandler() :
25 LogHandler(),
26 m_maxNoFiles(MAX_NO_FILES),
27 m_maxFileSize(MAX_FILE_SIZE),
28 m_maxLogEntries(MAX_LOG_ENTRIES)
31 m_pLogFile = new File_class("logger.log", "a+");
34 FileLogHandler::FileLogHandler(const char* aFileName,
35 int maxNoFiles,
36 long maxFileSize,
37 unsigned int maxLogEntries) :
38 LogHandler(),
39 m_maxNoFiles(maxNoFiles),
40 m_maxFileSize(maxFileSize),
41 m_maxLogEntries(maxLogEntries)
43 m_pLogFile = new File_class(aFileName, "a+");
46 FileLogHandler::~FileLogHandler()
48 delete m_pLogFile;
51 bool
52 FileLogHandler::open()
54 bool rc = true;
56 if (m_pLogFile->open())
58 if (isTimeForNewFile())
60 if (!createNewFile())
62 setErrorCode(errno);
63 rc = false;
67 else
69 setErrorCode(errno);
70 rc = false;
73 return rc;
76 bool
77 FileLogHandler::close()
79 bool rc = true;
80 if (!m_pLogFile->close())
82 setErrorCode(errno);
83 rc = false;
86 return rc;
89 void
90 FileLogHandler::writeHeader(const char* pCategory, Logger::LoggerLevel level)
92 char str[LogHandler::MAX_HEADER_LENGTH];
93 m_pLogFile->writeChar(getDefaultHeader(str, pCategory, level));
96 void
97 FileLogHandler::writeMessage(const char* pMsg)
99 m_pLogFile->writeChar(pMsg);
102 void
103 FileLogHandler::writeFooter()
105 static int callCount = 0;
106 m_pLogFile->writeChar(getDefaultFooter());
108 * The reason I also check the number of log entries instead of
109 * only the log size, is that I do not want to check the file size
110 * after each log entry which requires system calls and is quite slow.
111 * TODO: Any better way?
113 if (callCount % m_maxLogEntries != 0) // Check every m_maxLogEntries
115 if (isTimeForNewFile())
117 if (!createNewFile())
119 // Baby one more time...
120 createNewFile();
123 callCount = 0;
125 callCount++;
127 m_pLogFile->flush();
132 // PRIVATE
135 bool
136 FileLogHandler::isTimeForNewFile()
138 return (m_pLogFile->size() >= m_maxFileSize);
141 bool
142 FileLogHandler::createNewFile()
144 bool rc = true;
145 int fileNo = 1;
146 char newName[PATH_MAX];
147 time_t newMtime, preMtime = 0;
151 if (fileNo >= m_maxNoFiles)
153 fileNo = 1;
154 BaseString::snprintf(newName, sizeof(newName),
155 "%s.%d", m_pLogFile->getName(), fileNo);
156 break;
158 BaseString::snprintf(newName, sizeof(newName),
159 "%s.%d", m_pLogFile->getName(), fileNo++);
160 newMtime = File_class::mtime(newName);
161 if (newMtime < preMtime)
163 break;
165 else
167 preMtime = newMtime;
169 } while (File_class::exists(newName));
171 m_pLogFile->close();
172 if (!File_class::rename(m_pLogFile->getName(), newName))
174 setErrorCode(errno);
175 rc = false;
178 // Open again
179 if (!m_pLogFile->open())
181 setErrorCode(errno);
182 rc = false;
185 return rc;
188 bool
189 FileLogHandler::setParam(const BaseString &param, const BaseString &value){
190 if(param == "filename")
191 return setFilename(value);
192 if(param == "maxsize")
193 return setMaxSize(value);
194 if(param == "maxfiles")
195 return setMaxFiles(value);
196 setErrorStr("Invalid parameter");
197 return false;
200 bool
201 FileLogHandler::setFilename(const BaseString &filename) {
202 close();
203 if(m_pLogFile)
204 delete m_pLogFile;
205 m_pLogFile = new File_class(filename.c_str(), "a+");
206 return open();
209 bool
210 FileLogHandler::setMaxSize(const BaseString &size) {
211 char *end;
212 long val = strtol(size.c_str(), &end, 0); /* XXX */
213 if(size.c_str() == end || val < 0)
215 setErrorStr("Invalid file size");
216 return false;
218 if(end[0] == 'M')
219 val *= 1024*1024;
220 if(end[0] == 'k')
221 val *= 1024;
223 m_maxFileSize = val;
225 return true;
228 bool
229 FileLogHandler::setMaxFiles(const BaseString &files) {
230 char *end;
231 long val = strtol(files.c_str(), &end, 0);
232 if(files.c_str() == end || val < 1)
234 setErrorStr("Invalid maximum number of files");
235 return false;
237 m_maxNoFiles = val;
239 return true;
242 bool
243 FileLogHandler::checkParams() {
244 if(m_pLogFile == NULL)
246 setErrorStr("Log file cannot be null.");
247 return false;
249 return true;