1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/streamlogger.hpp"
4 #include "base/streamlogger-ti.cpp"
5 #include "base/utility.hpp"
6 #include "base/objectlock.hpp"
7 #include "base/console.hpp"
10 using namespace icinga;
12 REGISTER_TYPE(StreamLogger);
14 boost::mutex StreamLogger::m_Mutex;
16 void StreamLogger::Stop(bool runtimeRemoved)
18 ObjectImpl<StreamLogger>::Stop(runtimeRemoved);
20 // make sure we flush the log data on shutdown, even if we don't call the destructor
26 * Destructor for the StreamLogger class.
28 StreamLogger::~StreamLogger()
31 m_FlushLogTimer->Stop();
33 if (m_Stream && m_OwnsStream)
37 void StreamLogger::FlushLogTimerHandler()
42 void StreamLogger::Flush()
48 void StreamLogger::BindStream(std::ostream *stream, bool ownsStream)
50 ObjectLock olock(this);
52 if (m_Stream && m_OwnsStream)
56 m_OwnsStream = ownsStream;
58 if (!m_FlushLogTimer) {
59 m_FlushLogTimer = new Timer();
60 m_FlushLogTimer->SetInterval(1);
61 m_FlushLogTimer->OnTimerExpired.connect(std::bind(&StreamLogger::FlushLogTimerHandler, this));
62 m_FlushLogTimer->Start();
67 * Processes a log entry and outputs it to a stream.
69 * @param stream The output stream.
70 * @param entry The log entry.
72 void StreamLogger::ProcessLogEntry(std::ostream& stream, const LogEntry& entry)
74 String timestamp = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", entry.Timestamp);
76 boost::mutex::scoped_lock lock(m_Mutex);
78 if (Logger::IsTimestampEnabled())
79 stream << "[" << timestamp << "] ";
83 switch (entry.Severity) {
85 color = Console_ForegroundCyan;
88 color = Console_ForegroundBlue;
91 color = Console_ForegroundGreen;
94 color = Console_ForegroundYellow | Console_Bold;
97 color = Console_ForegroundRed | Console_Bold;
103 stream << ConsoleColorTag(color);
104 stream << Logger::SeverityToString(entry.Severity);
105 stream << ConsoleColorTag(Console_Normal);
106 stream << "/" << entry.Facility << ": " << entry.Message << "\n";
110 * Processes a log entry and outputs it to a stream.
112 * @param entry The log entry.
114 void StreamLogger::ProcessLogEntry(const LogEntry& entry)
116 ProcessLogEntry(*m_Stream, entry);