2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-06-06 05:26:00 -04:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/filelogger.hpp"
|
2018-01-18 07:50:38 -05:00
|
|
|
#include "base/filelogger-ti.cpp"
|
2015-08-15 14:28:05 -04:00
|
|
|
#include "base/configtype.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/statsfunction.hpp"
|
|
|
|
|
#include "base/application.hpp"
|
2013-06-06 05:26:30 -04:00
|
|
|
#include <fstream>
|
2013-06-06 05:26:00 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2013-06-06 05:26:30 -04:00
|
|
|
REGISTER_TYPE(FileLogger);
|
2013-06-06 05:26:00 -04:00
|
|
|
|
2015-09-21 05:44:58 -04:00
|
|
|
REGISTER_STATSFUNCTION(FileLogger, &FileLogger::StatsFunc);
|
2014-02-17 10:34:18 -05:00
|
|
|
|
2015-02-13 05:28:43 -05:00
|
|
|
void FileLogger::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
|
2014-02-17 10:34:18 -05:00
|
|
|
{
|
2018-01-11 05:17:38 -05:00
|
|
|
DictionaryData nodes;
|
2014-02-18 04:53:44 -05:00
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const FileLogger::Ptr& filelogger : ConfigType::GetObjectsByType<FileLogger>()) {
|
2018-01-11 05:17:38 -05:00
|
|
|
nodes.emplace_back(filelogger->GetName(), 1); //add more stats
|
2014-02-18 04:53:44 -05:00
|
|
|
}
|
|
|
|
|
|
2018-01-11 05:17:38 -05:00
|
|
|
status->Set("filelogger", new Dictionary(std::move(nodes)));
|
2014-02-17 10:34:18 -05:00
|
|
|
}
|
|
|
|
|
|
2013-06-06 05:26:00 -04:00
|
|
|
/**
|
2013-06-06 05:26:30 -04:00
|
|
|
* Constructor for the FileLogger class.
|
2013-06-06 05:26:00 -04:00
|
|
|
*/
|
2015-08-20 11:18:48 -04:00
|
|
|
void FileLogger::Start(bool runtimeCreated)
|
2013-06-06 05:26:00 -04:00
|
|
|
{
|
2014-05-22 05:22:30 -04:00
|
|
|
ReopenLogFile();
|
|
|
|
|
|
2021-01-18 08:29:05 -05:00
|
|
|
Application::OnReopenLogs.connect([this]() { ReopenLogFile(); });
|
2017-12-18 03:32:09 -05:00
|
|
|
|
|
|
|
|
ObjectImpl<FileLogger>::Start(runtimeCreated);
|
2018-07-19 06:48:01 -04:00
|
|
|
|
|
|
|
|
Log(LogInformation, "FileLogger")
|
|
|
|
|
<< "'" << GetName() << "' started.";
|
2014-05-22 05:22:30 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void FileLogger::ReopenLogFile()
|
2014-05-22 05:22:30 -04:00
|
|
|
{
|
2018-01-04 03:07:03 -05:00
|
|
|
auto *stream = new std::ofstream();
|
2013-06-06 05:26:30 -04:00
|
|
|
|
2013-10-26 03:41:45 -04:00
|
|
|
String path = GetPath();
|
2013-06-06 05:26:30 -04:00
|
|
|
|
|
|
|
|
try {
|
2014-01-14 11:25:05 -05:00
|
|
|
stream->open(path.CStr(), std::fstream::app | std::fstream::out);
|
2013-06-06 05:26:30 -04:00
|
|
|
|
|
|
|
|
if (!stream->good())
|
|
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open logfile '" + path + "'"));
|
|
|
|
|
} catch (...) {
|
|
|
|
|
delete stream;
|
|
|
|
|
throw;
|
2013-06-06 05:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
2013-06-06 05:26:30 -04:00
|
|
|
BindStream(stream, true);
|
2013-08-20 05:06:04 -04:00
|
|
|
}
|