]> granicus.if.org Git - icinga2/blob - lib/base/context.cpp
lib->compat->statusdatawriter: fix notifications_enabled
[icinga2] / lib / base / context.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/context.hpp"
21 #include <boost/thread/tss.hpp>
22 #include <iostream>
23
24 using namespace icinga;
25
26 static boost::thread_specific_ptr<std::list<String> > l_Frames;
27
28 ContextFrame::ContextFrame(const String& message)
29 {
30         GetFrames().push_front(message);
31 }
32
33 ContextFrame::~ContextFrame()
34 {
35         GetFrames().pop_front();
36 }
37
38 std::list<String>& ContextFrame::GetFrames()
39 {
40         if (!l_Frames.get())
41                 l_Frames.reset(new std::list<String>());
42
43         return *l_Frames;
44 }
45
46 ContextTrace::ContextTrace()
47         : m_Frames(ContextFrame::GetFrames())
48 { }
49
50 void ContextTrace::Print(std::ostream& fp) const
51 {
52         if (m_Frames.empty())
53                 return;
54
55         fp << "\n";
56
57         int i = 0;
58         for (const String& frame : m_Frames) {
59                 fp << "\t(" << i << ") " << frame << "\n";
60                 i++;
61         }
62 }
63
64 size_t ContextTrace::GetLength() const
65 {
66         return m_Frames.size();
67 }
68
69 std::ostream& icinga::operator<<(std::ostream& stream, const ContextTrace& trace)
70 {
71         trace.Print(stream);
72         return stream;
73 }