25
26
#include <boost/bind.hpp>
26
27
#include <core/core.h>
27
28
#include <NuxCore/Logger.h>
29
#include <NuxCore/LoggingWriter.h>
29
31
#include "DebugDBusInterface.h"
30
32
#include "Introspectable.h"
41
nux::logging::Logger logger("unity.debug.DebugDBusInterface");
43
nux::logging::Logger logger("unity.debug.DebugDBusInterface");
47
std::ofstream output_file;
44
51
GVariant* GetState(std::string const& query);
52
void StartLogToFile(std::string const& file_path);
54
void SetLogSeverity(std::string const& log_component,
55
std::string const& severity);
56
void LogMessage(std::string const& severity,
57
std::string const& message);
46
59
const char* DebugDBusInterface::DBUS_DEBUG_OBJECT_PATH = "/com/canonical/Unity/Debug";
54
67
" <arg type='aa{sv}' name='state' direction='out' />"
70
" <method name='StartLogToFile'>"
71
" <arg type='s' name='file_path' direction='in' />"
74
" <method name='ResetLogging'>"
77
" <method name='SetLogSeverity'>"
78
" <arg type='s' name='log_component' direction='in' />"
79
" <arg type='s' name='severity' direction='in' />"
82
" <method name='LogMessage'>"
83
" <arg type='s' name='severity' direction='in' />"
84
" <arg type='s' name='message' direction='in' />"
67
97
static CompScreen* _screen;
68
98
static Introspectable* _parent_introspectable;
70
DebugDBusInterface::DebugDBusInterface(Introspectable* parent,
100
DebugDBusInterface::DebugDBusInterface(Introspectable* parent,
71
101
CompScreen* screen)
149
180
g_dbus_method_invocation_return_value(invocation, ret);
150
181
g_variant_unref(ret);
183
else if (g_strcmp0(method_name, "StartLogToFile") == 0)
185
const gchar* log_path;
186
g_variant_get(parameters, "(&s)", &log_path);
188
StartLogToFile(log_path);
189
g_dbus_method_invocation_return_value(invocation, NULL);
191
else if (g_strcmp0(method_name, "ResetLogging") == 0)
194
g_dbus_method_invocation_return_value(invocation, NULL);
196
else if (g_strcmp0(method_name, "SetLogSeverity") == 0)
198
const gchar* component;
199
const gchar* severity;
200
g_variant_get(parameters, "(&s&s)", &component, &severity);
202
SetLogSeverity(component, severity);
203
g_dbus_method_invocation_return_value(invocation, NULL);
205
else if (g_strcmp0(method_name, "LogMessage") == 0)
207
const gchar* severity;
208
const gchar* message;
209
g_variant_get(parameters, "(&s&s)", &severity, &message);
211
LogMessage(severity, message);
212
g_dbus_method_invocation_return_value(invocation, NULL);
154
g_dbus_method_invocation_return_dbus_error(invocation,
216
g_dbus_method_invocation_return_dbus_error(invocation,
155
217
unity::DBUS_BUS_NAME.c_str(),
156
218
"Failed to find method");
164
226
std::list<Introspectable*> parts = GetIntrospectableNodesFromQuery(query, _parent_introspectable);
165
227
GVariantBuilder builder;
166
228
g_variant_builder_init(&builder, G_VARIANT_TYPE("aa{sv}"));
168
230
for (Introspectable *node : parts)
170
232
g_variant_builder_add_value(&builder, node->Introspect());
173
235
return g_variant_new("(aa{sv})", &builder);
238
void StartLogToFile(std::string const& file_path)
240
if (local::output_file.is_open())
241
local::output_file.close();
242
local::output_file.open(file_path.c_str());
243
nux::logging::Writer::Instance().SetOutputStream(local::output_file);
248
if (local::output_file.is_open())
249
local::output_file.close();
250
nux::logging::Writer::Instance().SetOutputStream(std::cout);
251
nux::logging::reset_logging();
254
void SetLogSeverity(std::string const& log_component,
255
std::string const& severity)
257
nux::logging::Logger(log_component).SetLogLevel(nux::logging::get_logging_level(severity));
260
void LogMessage(std::string const& severity,
261
std::string const& message)
263
nux::logging::Level level = nux::logging::get_logging_level(severity);
264
if (logger.GetEffectiveLogLevel() <= level)
266
nux::logging::LogStream(level, logger.module(), __FILE__, __LINE__).stream()
177
* Do a breadth-first search of the introspection tree and find all nodes that match the
272
* Do a breadth-first search of the introspection tree and find all nodes that match the
180
275
std::list<Introspectable*> GetIntrospectableNodesFromQuery(std::string const& query, Introspectable* tree_root)
197
292
std::list<std::string> query_strings;
198
293
boost::algorithm::split(query_strings, sanitised_query, boost::algorithm::is_any_of("/"));
199
// Boost's split() implementation does not match it's documentation! According to the
200
// docs, it's not supposed to add empty strings, but it does, which is a PITA. This
294
// Boost's split() implementation does not match it's documentation! According to the
295
// docs, it's not supposed to add empty strings, but it does, which is a PITA. This
201
296
// next line removes them:
202
query_strings.erase( std::remove_if( query_strings.begin(),
204
boost::bind( &std::string::empty, _1 ) ),
297
query_strings.erase( std::remove_if( query_strings.begin(),
299
boost::bind( &std::string::empty, _1 ) ),
205
300
query_strings.end());
206
301
foreach(std::string part, query_strings)
253
348
// now we have the tree start points, process them:
254
349
query_parts.pop_front();
255
350
typedef std::pair<Introspectable*, std::list<XPathQueryPart>::iterator> node_match_pair;
257
352
std::queue<node_match_pair> traverse_queue;
258
353
foreach(Introspectable *node, start_points)
260
355
traverse_queue.push(node_match_pair(node, query_parts.begin()));
262
357
start_points.clear();
264
359
while (!traverse_queue.empty())
266
361
node_match_pair p = traverse_queue.front();