]> granicus.if.org Git - icinga2/blob - lib/base/filelogger.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / filelogger.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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"
8 #include <fstream>
9
10 using namespace icinga;
11
12 REGISTER_TYPE(FileLogger);
13
14 REGISTER_STATSFUNCTION(FileLogger, &FileLogger::StatsFunc);
15
16 void FileLogger::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
17 {
18         DictionaryData nodes;
19
20         for (const FileLogger::Ptr& filelogger : ConfigType::GetObjectsByType<FileLogger>()) {
21                 nodes.emplace_back(filelogger->GetName(), 1); //add more stats
22         }
23
24         status->Set("filelogger", new Dictionary(std::move(nodes)));
25 }
26
27 /**
28  * Constructor for the FileLogger class.
29  */
30 void FileLogger::Start(bool runtimeCreated)
31 {
32         ReopenLogFile();
33
34         Application::OnReopenLogs.connect(std::bind(&FileLogger::ReopenLogFile, this));
35
36         ObjectImpl<FileLogger>::Start(runtimeCreated);
37
38         Log(LogInformation, "FileLogger")
39                 << "'" << GetName() << "' started.";
40 }
41
42 void FileLogger::ReopenLogFile()
43 {
44         auto *stream = new std::ofstream();
45
46         String path = GetPath();
47
48         try {
49                 stream->open(path.CStr(), std::fstream::app | std::fstream::out);
50
51                 if (!stream->good())
52                         BOOST_THROW_EXCEPTION(std::runtime_error("Could not open logfile '" + path + "'"));
53         } catch (...) {
54                 delete stream;
55                 throw;
56         }
57
58         BindStream(stream, true);
59 }