]> granicus.if.org Git - icinga2/blob - lib/cli/objectlistutility.cpp
add some object locking to the Dump method (which could theoreticylly suffer from...
[icinga2] / lib / cli / objectlistutility.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "cli/objectlistutility.hpp"
4 #include "base/json.hpp"
5 #include "base/utility.hpp"
6 #include "base/console.hpp"
7 #include "base/objectlock.hpp"
8 #include "base/convert.hpp"
9 #include <iostream>
10 #include <iomanip>
11
12 using namespace icinga;
13
14 bool ObjectListUtility::PrintObject(std::ostream& fp, bool& first, const String& message, std::map<String, int>& type_count, const String& name_filter, const String& type_filter)
15 {
16         Dictionary::Ptr object = JsonDecode(message);
17
18         Dictionary::Ptr properties = object->Get("properties");
19
20         String internal_name = properties->Get("__name");
21         String name = object->Get("name");
22         String type = object->Get("type");
23
24         if (!name_filter.IsEmpty() && !Utility::Match(name_filter, name) && !Utility::Match(name_filter, internal_name))
25                 return false;
26         if (!type_filter.IsEmpty() && !Utility::Match(type_filter, type))
27                 return false;
28
29         if (first)
30                 first = false;
31         else
32                 fp << "\n";
33
34         Dictionary::Ptr debug_hints = object->Get("debug_hints");
35
36         fp << "Object '" << ConsoleColorTag(Console_ForegroundBlue | Console_Bold) << internal_name << ConsoleColorTag(Console_Normal) << "'";
37         fp << " of type '" << ConsoleColorTag(Console_ForegroundMagenta | Console_Bold) << type << ConsoleColorTag(Console_Normal) << "':\n";
38
39         Array::Ptr di = object->Get("debug_info");
40
41         if (di) {
42                 fp << ConsoleColorTag(Console_ForegroundCyan) << "  % declared in '" << di->Get(0) << "', lines "
43                         << di->Get(1) << ":" << di->Get(2) << "-" << di->Get(3) << ":" << di->Get(4) << ConsoleColorTag(Console_Normal) << "\n";
44         }
45
46         PrintProperties(fp, properties, debug_hints, 2);
47
48         type_count[type]++;
49         return true;
50 }
51
52 void ObjectListUtility::PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent)
53 {
54         /* get debug hint props */
55         Dictionary::Ptr debug_hint_props;
56         if (debug_hints)
57                 debug_hint_props = debug_hints->Get("properties");
58
59         int offset = 2;
60
61         ObjectLock olock(props);
62         for (const Dictionary::Pair& kv : props)
63         {
64                 String key = kv.first;
65                 Value val = kv.second;
66
67                 /* key & value */
68                 fp << std::setw(indent) << " " << "* " << ConsoleColorTag(Console_ForegroundGreen) << key << ConsoleColorTag(Console_Normal);
69
70                 /* extract debug hints for key */
71                 Dictionary::Ptr debug_hints_fwd;
72                 if (debug_hint_props)
73                         debug_hints_fwd = debug_hint_props->Get(key);
74
75                 /* print dicts recursively */
76                 if (val.IsObjectType<Dictionary>()) {
77                         fp << "\n";
78                         PrintHints(fp, debug_hints_fwd, indent + offset);
79                         PrintProperties(fp, val, debug_hints_fwd, indent + offset);
80                 } else {
81                         fp << " = ";
82                         PrintValue(fp, val);
83                         fp << "\n";
84                         PrintHints(fp, debug_hints_fwd, indent + offset);
85                 }
86         }
87 }
88
89 void ObjectListUtility::PrintHints(std::ostream& fp, const Dictionary::Ptr& debug_hints, int indent)
90 {
91         if (!debug_hints)
92                 return;
93
94         Array::Ptr messages = debug_hints->Get("messages");
95
96         if (messages) {
97                 ObjectLock olock(messages);
98
99                 for (const Value& msg : messages)
100                 {
101                         PrintHint(fp, msg, indent);
102                 }
103         }
104 }
105
106 void ObjectListUtility::PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent)
107 {
108         fp << std::setw(indent) << " " << ConsoleColorTag(Console_ForegroundCyan) << "% " << msg->Get(0) << " modified in '" << msg->Get(1) << "', lines "
109                 << msg->Get(2) << ":" << msg->Get(3) << "-" << msg->Get(4) << ":" << msg->Get(5) << ConsoleColorTag(Console_Normal) << "\n";
110 }
111
112 void ObjectListUtility::PrintValue(std::ostream& fp, const Value& val)
113 {
114         if (val.IsObjectType<Array>()) {
115                 PrintArray(fp, val);
116                 return;
117         }
118
119         if (val.IsString()) {
120                 fp << "\"" << Convert::ToString(val) << "\"";
121                 return;
122         }
123
124         if (val.IsEmpty()) {
125                 fp << "null";
126                 return;
127         }
128
129         fp << Convert::ToString(val);
130 }
131
132 void ObjectListUtility::PrintArray(std::ostream& fp, const Array::Ptr& arr)
133 {
134         bool first = true;
135
136         fp << "[ ";
137
138         if (arr) {
139                 ObjectLock olock(arr);
140                 for (const Value& value : arr)
141                 {
142                         if (first)
143                                 first = false;
144                         else
145                                 fp << ", ";
146
147                         PrintValue(fp, value);
148                 }
149         }
150
151         if (!first)
152                 fp << " ";
153
154         fp << "]";
155 }