]> granicus.if.org Git - icinga2/blob - lib/base/streamlogger.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / streamlogger.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef STREAMLOGGER_H
4 #define STREAMLOGGER_H
5
6 #include "base/i2-base.hpp"
7 #include "base/streamlogger-ti.hpp"
8 #include "base/timer.hpp"
9 #include <iosfwd>
10
11 namespace icinga
12 {
13
14 /**
15  * A logger that logs to an iostream.
16  *
17  * @ingroup base
18  */
19 class StreamLogger : public ObjectImpl<StreamLogger>
20 {
21 public:
22         DECLARE_OBJECT(StreamLogger);
23
24         void Stop(bool runtimeRemoved) override;
25         ~StreamLogger() override;
26
27         void BindStream(std::ostream *stream, bool ownsStream);
28
29         static void ProcessLogEntry(std::ostream& stream, const LogEntry& entry);
30
31 protected:
32         void ProcessLogEntry(const LogEntry& entry) final;
33         void Flush() final;
34
35 private:
36         static boost::mutex m_Mutex;
37         std::ostream *m_Stream{nullptr};
38         bool m_OwnsStream{false};
39
40         Timer::Ptr m_FlushLogTimer;
41
42         void FlushLogTimerHandler();
43 };
44
45 }
46
47 #endif /* STREAMLOGGER_H */