refactor(tray): improve error handling and add debug logs

This commit is contained in:
Aleksei Bavshin 2019-05-20 08:00:07 -07:00
parent afd291a566
commit 50bfe78aed

View File

@ -1,6 +1,32 @@
#include "modules/sni/item.hpp"
#include <glibmm/main.h> #include <glibmm/main.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include "modules/sni/item.hpp"
template <>
struct fmt::formatter<Glib::ustring> : formatter<std::string> {
template <typename FormatContext>
auto format(const Glib::ustring& value, FormatContext& ctx) {
return formatter<std::string>::format(value, ctx);
}
};
template <>
struct fmt::formatter<Glib::VariantBase> : formatter<std::string> {
bool is_printable(const Glib::VariantBase& value) {
auto type = value.get_type_string();
/* Print only primitive (single character excluding 'v') and short complex types */
return (type.length() == 1 && islower(type[0]) && type[0] != 'v') || value.get_size() <= 32;
}
template <typename FormatContext>
auto format(const Glib::VariantBase& value, FormatContext& ctx) {
if (is_printable(value)) {
return formatter<std::string>::format(value.print(), ctx);
} else {
return formatter<std::string>::format(value.get_type_string(), ctx);
}
}
};
namespace waybar::modules::SNI { namespace waybar::modules::SNI {
@ -54,8 +80,7 @@ void Item::proxyReady(Glib::RefPtr<Gio::AsyncResult>& result) {
// this->event_box.set_tooltip_text(this->title); // this->event_box.set_tooltip_text(this->title);
} catch (const Glib::Error& err) { } catch (const Glib::Error& err) {
spdlog::error( spdlog::error("Failed to create DBus Proxy for {} {}: {}", bus_name, object_path, err.what());
"Failed to create DBus Proxy for {} {}: {}", bus_name, object_path, err.what().raw());
} catch (const std::exception& err) { } catch (const std::exception& err) {
spdlog::error("Failed to create DBus Proxy for {} {}: {}", bus_name, object_path, err.what()); spdlog::error("Failed to create DBus Proxy for {} {}: {}", bus_name, object_path, err.what());
} }
@ -67,6 +92,9 @@ T get_variant(Glib::VariantBase& value) {
} }
void Item::setProperty(const Glib::ustring& name, Glib::VariantBase& value) { void Item::setProperty(const Glib::ustring& name, Glib::VariantBase& value) {
try {
spdlog::trace("Set tray item property: {}.{} = {}", id.empty() ? bus_name : id, name, value);
if (name == "Category") { if (name == "Category") {
category = get_variant<std::string>(value); category = get_variant<std::string>(value);
} else if (name == "Id") { } else if (name == "Id") {
@ -103,6 +131,19 @@ void Item::setProperty(const Glib::ustring& name, Glib::VariantBase& value) {
} else if (name == "ItemIsMenu") { } else if (name == "ItemIsMenu") {
item_is_menu = get_variant<bool>(value); item_is_menu = get_variant<bool>(value);
} }
} catch (const Glib::Error& err) {
spdlog::warn("Failed to set tray item property: {}.{}, value = {}, err = {}",
id.empty() ? bus_name : id,
name,
value,
err.what());
} catch (const std::exception& err) {
spdlog::warn("Failed to set tray item property: {}.{}, value = {}, err = {}",
id.empty() ? bus_name : id,
name,
value,
err.what());
}
} }
void Item::getUpdatedProperties() { void Item::getUpdatedProperties() {
@ -135,7 +176,7 @@ void Item::processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& _result) {
this->updateImage(); this->updateImage();
// this->event_box.set_tooltip_text(this->title); // this->event_box.set_tooltip_text(this->title);
} catch (const Glib::Error& err) { } catch (const Glib::Error& err) {
spdlog::warn("Failed to update properties: {}", err.what().raw()); spdlog::warn("Failed to update properties: {}", err.what());
} catch (const std::exception& err) { } catch (const std::exception& err) {
spdlog::warn("Failed to update properties: {}", err.what()); spdlog::warn("Failed to update properties: {}", err.what());
} }
@ -143,6 +184,7 @@ void Item::processUpdatedProperties(Glib::RefPtr<Gio::AsyncResult>& _result) {
void Item::onSignal(const Glib::ustring& sender_name, const Glib::ustring& signal_name, void Item::onSignal(const Glib::ustring& sender_name, const Glib::ustring& signal_name,
const Glib::VariantContainerBase& arguments) { const Glib::VariantContainerBase& arguments) {
spdlog::trace("Tray item '{}' got signal {}", id, signal_name);
if (!update_pending_ && signal_name.compare(0, 3, "New") == 0) { if (!update_pending_ && signal_name.compare(0, 3, "New") == 0) {
/* Debounce signals and schedule update of all properties. /* Debounce signals and schedule update of all properties.
* Based on behavior of Plasma dataengine for StatusNotifierItem. * Based on behavior of Plasma dataengine for StatusNotifierItem.