]> granicus.if.org Git - icinga2/blob - lib/base/context.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / context.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "base/context.hpp"
4 #include <boost/thread/tss.hpp>
5 #include <iostream>
6
7 using namespace icinga;
8
9 static boost::thread_specific_ptr<std::list<String> > l_Frames;
10
11 ContextFrame::ContextFrame(const String& message)
12 {
13         GetFrames().push_front(message);
14 }
15
16 ContextFrame::~ContextFrame()
17 {
18         GetFrames().pop_front();
19 }
20
21 std::list<String>& ContextFrame::GetFrames()
22 {
23         if (!l_Frames.get())
24                 l_Frames.reset(new std::list<String>());
25
26         return *l_Frames;
27 }
28
29 ContextTrace::ContextTrace()
30         : m_Frames(ContextFrame::GetFrames())
31 { }
32
33 void ContextTrace::Print(std::ostream& fp) const
34 {
35         if (m_Frames.empty())
36                 return;
37
38         fp << "\n";
39
40         int i = 0;
41         for (const String& frame : m_Frames) {
42                 fp << "\t(" << i << ") " << frame << "\n";
43                 i++;
44         }
45 }
46
47 size_t ContextTrace::GetLength() const
48 {
49         return m_Frames.size();
50 }
51
52 std::ostream& icinga::operator<<(std::ostream& stream, const ContextTrace& trace)
53 {
54         trace.Print(stream);
55         return stream;
56 }