1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/filelogger.hpp"
4 #include "base/filelogger-ti.cpp"
5 #include "base/configtype.hpp"
6 #include "base/statsfunction.hpp"
7 #include "base/application.hpp"
10 using namespace icinga;
12 REGISTER_TYPE(FileLogger);
14 REGISTER_STATSFUNCTION(FileLogger, &FileLogger::StatsFunc);
16 void FileLogger::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
20 for (const FileLogger::Ptr& filelogger : ConfigType::GetObjectsByType<FileLogger>()) {
21 nodes.emplace_back(filelogger->GetName(), 1); //add more stats
24 status->Set("filelogger", new Dictionary(std::move(nodes)));
28 * Constructor for the FileLogger class.
30 void FileLogger::Start(bool runtimeCreated)
34 Application::OnReopenLogs.connect(std::bind(&FileLogger::ReopenLogFile, this));
36 ObjectImpl<FileLogger>::Start(runtimeCreated);
38 Log(LogInformation, "FileLogger")
39 << "'" << GetName() << "' started.";
42 void FileLogger::ReopenLogFile()
44 auto *stream = new std::ofstream();
46 String path = GetPath();
49 stream->open(path.CStr(), std::fstream::app | std::fstream::out);
52 BOOST_THROW_EXCEPTION(std::runtime_error("Could not open logfile '" + path + "'"));
58 BindStream(stream, true);