1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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"
12 using namespace icinga;
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)
16 Dictionary::Ptr object = JsonDecode(message);
18 Dictionary::Ptr properties = object->Get("properties");
20 String internal_name = properties->Get("__name");
21 String name = object->Get("name");
22 String type = object->Get("type");
24 if (!name_filter.IsEmpty() && !Utility::Match(name_filter, name) && !Utility::Match(name_filter, internal_name))
26 if (!type_filter.IsEmpty() && !Utility::Match(type_filter, type))
34 Dictionary::Ptr debug_hints = object->Get("debug_hints");
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";
39 Array::Ptr di = object->Get("debug_info");
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";
46 PrintProperties(fp, properties, debug_hints, 2);
52 void ObjectListUtility::PrintProperties(std::ostream& fp, const Dictionary::Ptr& props, const Dictionary::Ptr& debug_hints, int indent)
54 /* get debug hint props */
55 Dictionary::Ptr debug_hint_props;
57 debug_hint_props = debug_hints->Get("properties");
61 ObjectLock olock(props);
62 for (const Dictionary::Pair& kv : props)
64 String key = kv.first;
65 Value val = kv.second;
68 fp << std::setw(indent) << " " << "* " << ConsoleColorTag(Console_ForegroundGreen) << key << ConsoleColorTag(Console_Normal);
70 /* extract debug hints for key */
71 Dictionary::Ptr debug_hints_fwd;
73 debug_hints_fwd = debug_hint_props->Get(key);
75 /* print dicts recursively */
76 if (val.IsObjectType<Dictionary>()) {
78 PrintHints(fp, debug_hints_fwd, indent + offset);
79 PrintProperties(fp, val, debug_hints_fwd, indent + offset);
84 PrintHints(fp, debug_hints_fwd, indent + offset);
89 void ObjectListUtility::PrintHints(std::ostream& fp, const Dictionary::Ptr& debug_hints, int indent)
94 Array::Ptr messages = debug_hints->Get("messages");
97 ObjectLock olock(messages);
99 for (const Value& msg : messages)
101 PrintHint(fp, msg, indent);
106 void ObjectListUtility::PrintHint(std::ostream& fp, const Array::Ptr& msg, int indent)
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";
112 void ObjectListUtility::PrintValue(std::ostream& fp, const Value& val)
114 if (val.IsObjectType<Array>()) {
119 if (val.IsString()) {
120 fp << "\"" << Convert::ToString(val) << "\"";
129 fp << Convert::ToString(val);
132 void ObjectListUtility::PrintArray(std::ostream& fp, const Array::Ptr& arr)
139 ObjectLock olock(arr);
140 for (const Value& value : arr)
147 PrintValue(fp, value);