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>
24 FileLogHandler::FileLogHandler() :
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
,
37 unsigned int maxLogEntries
) :
39 m_maxNoFiles(maxNoFiles
),
40 m_maxFileSize(maxFileSize
),
41 m_maxLogEntries(maxLogEntries
)
43 m_pLogFile
= new File_class(aFileName
, "a+");
46 FileLogHandler::~FileLogHandler()
52 FileLogHandler::open()
56 if (m_pLogFile
->open())
58 if (isTimeForNewFile())
77 FileLogHandler::close()
80 if (!m_pLogFile
->close())
90 FileLogHandler::writeHeader(const char* pCategory
, Logger::LoggerLevel level
)
92 char str
[LogHandler::MAX_HEADER_LENGTH
];
93 m_pLogFile
->writeChar(getDefaultHeader(str
, pCategory
, level
));
97 FileLogHandler::writeMessage(const char* pMsg
)
99 m_pLogFile
->writeChar(pMsg
);
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...
136 FileLogHandler::isTimeForNewFile()
138 return (m_pLogFile
->size() >= m_maxFileSize
);
142 FileLogHandler::createNewFile()
146 char newName
[PATH_MAX
];
147 time_t newMtime
, preMtime
= 0;
151 if (fileNo
>= m_maxNoFiles
)
154 BaseString::snprintf(newName
, sizeof(newName
),
155 "%s.%d", m_pLogFile
->getName(), fileNo
);
158 BaseString::snprintf(newName
, sizeof(newName
),
159 "%s.%d", m_pLogFile
->getName(), fileNo
++);
160 newMtime
= File_class::mtime(newName
);
161 if (newMtime
< preMtime
)
169 } while (File_class::exists(newName
));
172 if (!File_class::rename(m_pLogFile
->getName(), newName
))
179 if (!m_pLogFile
->open())
189 FileLogHandler::setParam(const BaseString
¶m
, 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");
201 FileLogHandler::setFilename(const BaseString
&filename
) {
205 m_pLogFile
= new File_class(filename
.c_str(), "a+");
210 FileLogHandler::setMaxSize(const BaseString
&size
) {
212 long val
= strtol(size
.c_str(), &end
, 0); /* XXX */
213 if(size
.c_str() == end
|| val
< 0)
215 setErrorStr("Invalid file size");
229 FileLogHandler::setMaxFiles(const BaseString
&files
) {
231 long val
= strtol(files
.c_str(), &end
, 0);
232 if(files
.c_str() == end
|| val
< 1)
234 setErrorStr("Invalid maximum number of files");
243 FileLogHandler::checkParams() {
244 if(m_pLogFile
== NULL
)
246 setErrorStr("Log file cannot be null.");