]> granicus.if.org Git - icinga2/blob - lib/base/streamlogger.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / streamlogger.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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"
8 #include <iostream>
9
10 using namespace icinga;
11
12 REGISTER_TYPE(StreamLogger);
13
14 boost::mutex StreamLogger::m_Mutex;
15
16 void StreamLogger::Stop(bool runtimeRemoved)
17 {
18         ObjectImpl<StreamLogger>::Stop(runtimeRemoved);
19
20         // make sure we flush the log data on shutdown, even if we don't call the destructor
21         if (m_Stream)
22                 m_Stream->flush();
23 }
24
25 /**
26  * Destructor for the StreamLogger class.
27  */
28 StreamLogger::~StreamLogger()
29 {
30         if (m_FlushLogTimer)
31                 m_FlushLogTimer->Stop();
32
33         if (m_Stream && m_OwnsStream)
34                 delete m_Stream;
35 }
36
37 void StreamLogger::FlushLogTimerHandler()
38 {
39         Flush();
40 }
41
42 void StreamLogger::Flush()
43 {
44         if (m_Stream)
45                 m_Stream->flush();
46 }
47
48 void StreamLogger::BindStream(std::ostream *stream, bool ownsStream)
49 {
50         ObjectLock olock(this);
51
52         if (m_Stream && m_OwnsStream)
53                 delete m_Stream;
54
55         m_Stream = stream;
56         m_OwnsStream = ownsStream;
57
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();
63         }
64 }
65
66 /**
67  * Processes a log entry and outputs it to a stream.
68  *
69  * @param stream The output stream.
70  * @param entry The log entry.
71  */
72 void StreamLogger::ProcessLogEntry(std::ostream& stream, const LogEntry& entry)
73 {
74         String timestamp = Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", entry.Timestamp);
75
76         boost::mutex::scoped_lock lock(m_Mutex);
77
78         if (Logger::IsTimestampEnabled())
79                 stream << "[" << timestamp << "] ";
80
81         int color;
82
83         switch (entry.Severity) {
84                 case LogDebug:
85                         color = Console_ForegroundCyan;
86                         break;
87                 case LogNotice:
88                         color = Console_ForegroundBlue;
89                         break;
90                 case LogInformation:
91                         color = Console_ForegroundGreen;
92                         break;
93                 case LogWarning:
94                         color = Console_ForegroundYellow | Console_Bold;
95                         break;
96                 case LogCritical:
97                         color = Console_ForegroundRed | Console_Bold;
98                         break;
99                 default:
100                         return;
101         }
102
103         stream << ConsoleColorTag(color);
104         stream << Logger::SeverityToString(entry.Severity);
105         stream << ConsoleColorTag(Console_Normal);
106         stream << "/" << entry.Facility << ": " << entry.Message << "\n";
107 }
108
109 /**
110  * Processes a log entry and outputs it to a stream.
111  *
112  * @param entry The log entry.
113  */
114 void StreamLogger::ProcessLogEntry(const LogEntry& entry)
115 {
116         ProcessLogEntry(*m_Stream, entry);
117 }