| Kea 3.0.2-git

rotating_file.h
Go to the documentation of this file.
1// Copyright (C) 2016-2025 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef ROTATING_FILE_H
8#define ROTATING_FILE_H
9
10#include <dhcpsrv/legal_log_mgr_factory.h>
11
12#include <mutex>
13
16
17 namespace isc {
18 namespace legal_log {
19
50 class RotatingFile : public isc::dhcp::LegalLogMgr {
51 public:
52
54 enum class TimeUnit {
55 Second,
56 Day,
57 Month,
58 Year
59 };
60
83 RotatingFile(const isc::db::DatabaseConnection::ParameterMap& parameters);
84
88 virtual ~RotatingFile();
89
120 void apply(const isc::db::DatabaseConnection::ParameterMap& parameters);
121
141 virtual void open();
142
146 virtual void close();
147
168 virtual void writeln(const std::string& text, const std::string& addr);
169
175 virtual std::string getType() const {
176 return (std::string("logfile"));
177 }
178
182 std::string getFileName() const {
183 return (file_name_);
184 }
185
189 std::string getBaseName() const {
190 return (base_name_);
191 }
192
196 std::string getPath() const {
197 return (path_);
198 }
199
204 static std::string getYearMonthDay(const struct tm& time_info);
205
213 void updateFileNameAndTimestamp(struct tm& time_info, bool use_existing);
214
215 protected:
216
222 virtual void openInternal(struct tm& time_info, bool use_existing);
223
227 void useExistingFiles(struct tm& time_info);
228
234 virtual void rotate();
235
239 virtual bool isOpen() const;
240
241 private:
249 void writelnInternal(const std::string& text);
250
252 std::string path_;
253
255 std::string base_name_;
256
258 TimeUnit time_unit_;
259
263 uint32_t count_;
264
266 std::string file_name_;
267
269 std::ofstream file_;
270
272 std::string prerotate_;
273
275 std::string postrotate_;
276
278 time_t timestamp_;
279
281 std::mutex mutex_;
282
283 public:
290 static isc::dhcp::LegalLogMgrPtr
291 factory(const isc::db::DatabaseConnection::ParameterMap& parameters);
292};
293
295 struct RotatingFileInit {
296 // Constructor registers
297 RotatingFileInit() {
298 isc::dhcp::LegalLogMgrFactory::registerBackendFactory("logfile", RotatingFile::factory);
299 }
300
301 // Destructor deregisters
302 ~RotatingFileInit() {
303 isc::dhcp::LegalLogMgrFactory::unregisterBackendFactory("logfile");
304 }
305};
306
307} // namespace legal_log
308} // namespace isc
309
310#endif
isc::db::DatabaseConnection::ParameterMap
std::map< std::string, std::string > ParameterMap
Database configuration parameter map.
Definition database_connection.h:137
isc::dhcp::LegalLogMgrFactory::unregisterBackendFactory
static bool unregisterBackendFactory(const std::string &db_type, bool no_log=false)
Unregisters the backend factory function for a given backend type.
Definition legal_log_mgr_factory.cc:53
isc::dhcp::LegalLogMgrFactory::registerBackendFactory
static bool registerBackendFactory(const std::string &db_type, const Factory &factory, bool no_log=false, DBVersion db_version=DBVersion())
Registers new backend factory function for a given backend type.
Definition legal_log_mgr_factory.cc:23
isc::dhcp::LegalLogMgr
LegalLogMgr abstract class.
Definition legal_log_mgr.h:39
isc::legal_log::RotatingFile::writeln
virtual void writeln(const std::string &text, const std::string &addr)
Appends a string to the current file.
Definition rotating_file.cc:374
isc::legal_log::RotatingFile::close
virtual void close()
Closes the underlying file.
Definition rotating_file.cc:410
isc::legal_log::RotatingFile::getFileName
std::string getFileName() const
Returns the current file name.
Definition rotating_file.h:182
isc::legal_log::RotatingFile::getBaseName
std::string getBaseName() const
Returns the file base name.
Definition rotating_file.h:189
isc::legal_log::RotatingFile::getYearMonthDay
static std::string getYearMonthDay(const struct tm &time_info)
Build the year-month-day string from a date.
Definition rotating_file.cc:131
isc::legal_log::RotatingFile::~RotatingFile
virtual ~RotatingFile()
Destructor.
Definition rotating_file.cc:126
isc::legal_log::RotatingFile::factory
static isc::dhcp::LegalLogMgrPtr factory(const isc::db::DatabaseConnection::ParameterMap &parameters)
Factory class method.
Definition rotating_file.cc:427
isc::legal_log::RotatingFile::useExistingFiles
void useExistingFiles(struct tm &time_info)
Update file name with previously created file.
Definition rotating_file.cc:165
isc::legal_log::RotatingFile::open
virtual void open()
Opens the current file for writing.
Definition rotating_file.cc:278
isc::legal_log::RotatingFile::RotatingFile
RotatingFile(const isc::db::DatabaseConnection::ParameterMap &parameters)
Constructor.
Definition rotating_file.cc:35
isc::legal_log::RotatingFile::rotate
virtual void rotate()
Rotates the file if necessary.
Definition rotating_file.cc:306
isc::legal_log::RotatingFile::apply
void apply(const isc::db::DatabaseConnection::ParameterMap &parameters)
Parse file specification and create forensic log backend.
Definition rotating_file.cc:41
isc::legal_log::RotatingFile::getType
virtual std::string getType() const
Return backend type.
Definition rotating_file.h:175
isc::legal_log::RotatingFile::getPath
std::string getPath() const
Returns the file path.
Definition rotating_file.h:196
isc::legal_log::RotatingFile::isOpen
virtual bool isOpen() const
Returns true if the file is open.
Definition rotating_file.cc:405
isc::legal_log::RotatingFile::TimeUnit
TimeUnit
Time unit type used to rotate file.
Definition rotating_file.h:54
isc::legal_log::RotatingFile::TimeUnit::Day
@ Day
Definition rotating_file.h:56
isc::legal_log::RotatingFile::TimeUnit::Year
@ Year
Definition rotating_file.h:58
isc::legal_log::RotatingFile::TimeUnit::Month
@ Month
Definition rotating_file.h:57
isc::legal_log::RotatingFile::TimeUnit::Second
@ Second
Definition rotating_file.h:55
isc::legal_log::RotatingFile::updateFileNameAndTimestamp
void updateFileNameAndTimestamp(struct tm &time_info, bool use_existing)
Function which updates the file name and internal timestamp from previously created file name (if it ...
Definition rotating_file.cc:138
isc::legal_log::RotatingFile::openInternal
virtual void openInternal(struct tm &time_info, bool use_existing)
Open file using specified timestamp.
Definition rotating_file.cc:288
legal_log_mgr_factory.h
isc::dhcp::LegalLogMgrPtr
boost::shared_ptr< LegalLogMgr > LegalLogMgrPtr
Defines a smart pointer to a LegalLogMgr.
Definition legal_log_mgr.h:36
isc::legal_log
Definition legal_log_log.cc:13
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::legal_log::RotatingFileInit::~RotatingFileInit
~RotatingFileInit()
Definition rotating_file.h:302
isc::legal_log::RotatingFileInit::RotatingFileInit
RotatingFileInit()
Definition rotating_file.h:297