From 46be2528441f6ebf3ddd0e0b46d60741b600a6c7 Mon Sep 17 00:00:00 2001 From: William C Bonner Date: Wed, 20 Nov 2024 12:12:35 -0800 Subject: [PATCH] add error reporting and clean up some levels --- CMakeLists.txt | 2 +- goveebttemplogger.cpp | 90 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b559078..e1e3dba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,7 @@ endif() add_compile_definitions(_BLUEZ_HCI_) project (GoveeBTTempLogger - VERSION 3.20241118.0 + VERSION 3.20241120.0 DESCRIPTION "Listen and log Govee Thermometer Bluetooth Low Energy Advertisments via BlueZ and DBus" HOMEPAGE_URL https://github.com/wcbonner/GoveeBTTempLogger ) diff --git a/goveebttemplogger.cpp b/goveebttemplogger.cpp index 5c8fd55..2854c45 100644 --- a/goveebttemplogger.cpp +++ b/goveebttemplogger.cpp @@ -3633,7 +3633,8 @@ bool bluez_power_on(DBusConnection* dbus_conn, const char* adapter_path, const b { if (ConsoleVerbosity > 0) ssOutput << "[ ] "; - ssOutput << "Can't allocate dbus_message_new_method_call: " << __FILE__ << "(" << __LINE__ << ")" << std::endl; + ssOutput << "Can't allocate dbus_message_new_method_call: " << __FILE__ << "(" << __LINE__ << ")"; + rVal = false; } else { @@ -3668,12 +3669,11 @@ bool bluez_power_on(DBusConnection* dbus_conn, const char* adapter_path, const b else dbus_message_unref(dbus_reply); dbus_message_unref(dbus_msg); // https://dbus.freedesktop.org/doc/api/html/group__DBusMessage.html#gab69441efe683918f6a82469c8763f464 - ssOutput << std::endl; } - if (ConsoleVerbosity > 0) - std::cout << ssOutput.str(); - else - std::cerr << ssOutput.str(); + if (ConsoleVerbosity > 1) + std::cout << ssOutput.str() << std::endl; + else if (!rVal) + std::cerr << ssOutput.str() << std::endl; return(rVal); } bool bluez_filter_le(DBusConnection* dbus_conn, const char* adapter_path, const bool DuplicateData = true, const bool bFilter = true) @@ -3757,9 +3757,9 @@ bool bluez_filter_le(DBusConnection* dbus_conn, const char* adapter_path, const dbus_message_unref(dbus_msg); ssOutput << std::endl; } - if (ConsoleVerbosity > 0) + if (ConsoleVerbosity > 1) std::cout << ssOutput.str(); - else + else if (!rVal) std::cerr << ssOutput.str(); return(rVal); } @@ -4231,6 +4231,7 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set& BT_W { int rVal(0); time_t TimeStart(0), TimeLog(0), TimeSVG(0); + std::ostringstream ssOutput; // Main loop bRun = true; while (bRun) @@ -4242,15 +4243,26 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set& BT_W DBusConnection* dbus_conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga77ba5250adb84620f16007e1b023cf26 if (dbus_error_is_set(&dbus_error)) // https://dbus.freedesktop.org/doc/api/html/group__DBusErrors.html#gab0ed62e9fc2685897eb2d41467c89405 { - std::cout << "[" << getTimeISO8601(true) << "] Error connecting to the D-Bus system bus: " << dbus_error.message << std::endl; + if (ConsoleVerbosity > 0) + ssOutput << "[" << getTimeISO8601(true) << "] "; + ssOutput << "Error connecting to the D-Bus system bus: " << dbus_error.message; dbus_error_free(&dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusErrors.html#gaac6c14ead14829ee4e090f39de6a7568 + if (ConsoleVerbosity > 0) + std::cout << ssOutput.str() << std::endl; + else + std::cerr << ssOutput.str() << std::endl; + ssOutput = std::ostringstream(); // reinitialize my output stringstream } else { if (ConsoleVerbosity > 0) - std::cout << "[" << getTimeISO8601(true) << "] D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Opened" << std::endl; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b + ssOutput << "[" << getTimeISO8601(true) << "] "; + ssOutput << "D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Opened"; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b + if (ConsoleVerbosity > 0) + std::cout << ssOutput.str() << std::endl; else - std::cerr << "D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Opened" << std::endl; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b + std::cerr << ssOutput.str() << std::endl; + ssOutput = std::ostringstream(); // reinitialize my output stringstream std::map BlueZAdapterMap; bluez_find_adapters(dbus_conn, BlueZAdapterMap); if (BlueZAdapterMap.empty()) @@ -4258,9 +4270,13 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set& BT_W bRun = false; rVal = 1; if (ConsoleVerbosity > 0) - std::cout << "[" << getTimeISO8601(true) << "] Could not get list of adapters from BlueZ over DBus. Reverting to HCI interface." << std::endl; + ssOutput << "[" << getTimeISO8601(true) << "] "; + ssOutput << "Could not get list of adapters from BlueZ over DBus. Reverting to HCI interface."; + if (ConsoleVerbosity > 0) + std::cout << ssOutput.str() << std::endl; else - std::cerr << "Could not get list of adapters from BlueZ over DBus. Reverting to HCI interface." << std::endl; + std::cerr << ssOutput.str() << std::endl; + ssOutput = std::ostringstream(); // reinitialize my output stringstream } else { @@ -4281,13 +4297,23 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set& BT_W MatchRules.push_back("type='signal',sender='org.bluez',member='PropertiesChanged'"); for (auto& MatchRule : MatchRules) { + if (ConsoleVerbosity > 0) + ssOutput << "[ ] "; + ssOutput << "Add Match Rule: \"" << MatchRule << "\" "; + bool MatchRuleError(false); dbus_error_init(&dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusErrors.html#ga8937f0b7cdf8554fa6305158ce453fbe dbus_bus_add_match(dbus_conn, MatchRule.c_str(), &dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga4eb6401ba014da3dbe3dc4e2a8e5b3ef if (dbus_error_is_set(&dbus_error)) { - std::cout << "Error adding a match rule on the D-Bus system bus: " << dbus_error.message << std::endl; + ssOutput << "Error adding a match rule on the D-Bus system bus: " << dbus_error.message; dbus_error_free(&dbus_error); + MatchRuleError = true; } + if (ConsoleVerbosity > 1) + std::cout << ssOutput.str() << std::endl; + else if (MatchRuleError) + std::cerr << ssOutput.str() << std::endl; + ssOutput = std::ostringstream(); // reinitialize my output stringstream } time_t TimeNow(0); do @@ -4297,9 +4323,13 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set& BT_W { time(&TimeNow); if (ConsoleVerbosity > 0) - std::cout << "[" << timeToISO8601(TimeNow, true) << "] D-Bus connection Closed" << std::endl; + ssOutput << "[" << timeToISO8601(TimeNow, true) << "] "; + ssOutput << "D-Bus connection Closed"; + if (ConsoleVerbosity > 0) + std::cout << ssOutput.str() << std::endl; else - std::cerr << "D-Bus connection Closed" << std::endl; + std::cerr << ssOutput.str() << std::endl; + ssOutput = std::ostringstream(); // reinitialize my output stringstream bRun = false; } else @@ -4382,6 +4412,26 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set& BT_W #else } while (bRun && difftime(TimeNow, TimeStart) < (60 * 30)); // Maintain DBus connection for no more than 30 minutes #endif // DEBUG + for (auto& MatchRule : MatchRules) + { + if (ConsoleVerbosity > 0) + ssOutput << "[ ] "; + ssOutput << "Remove Match Rule: \"" << MatchRule << "\" "; + bool MatchRuleError(false); + dbus_error_init(&dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusErrors.html#ga8937f0b7cdf8554fa6305158ce453fbe + dbus_bus_remove_match(dbus_conn, MatchRule.c_str(), &dbus_error); // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga4eb6401ba014da3dbe3dc4e2a8e5b3ef + if (dbus_error_is_set(&dbus_error)) + { + ssOutput << "Error removing a match rule on the D-Bus system bus: " << dbus_error.message; + dbus_error_free(&dbus_error); + MatchRuleError = true; + } + if (ConsoleVerbosity > 1) + std::cout << ssOutput.str() << std::endl; + else if (MatchRuleError) + std::cerr << ssOutput.str() << std::endl; + ssOutput = std::ostringstream(); // reinitialize my output stringstream + } bluez_discovery(dbus_conn, BlueZAdapter.c_str(), false); bluez_dbus_RemoveKnownDevices(dbus_conn, BlueZAdapter.c_str(), GoveeThermometers); //bluez_filter_le(dbus_conn, BlueZAdapter.c_str(), false, false); // remove discovery filter @@ -4391,9 +4441,13 @@ int BlueZ_DBus_Mainloop(std::string& ControllerAddress, std::set& BT_W usleep(1000000); // 1,000,000 = 1 second. } if (ConsoleVerbosity > 0) - std::cout << "[" << getTimeISO8601(true) << "] D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Closed" << std::endl; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b + ssOutput << "[" << getTimeISO8601(true) << "] "; + ssOutput << "D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Closed"; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b + if (ConsoleVerbosity > 0) + std::cout << ssOutput.str() << std::endl; else - std::cerr << "D-Bus connection \"" << dbus_bus_get_unique_name(dbus_conn) << "\" Closed" << std::endl; // https://dbus.freedesktop.org/doc/api/html/group__DBusBus.html#ga8c10339a1e62f6a2e5752d9c2270d37b + std::cerr << ssOutput.str() << std::endl; + ssOutput = std::ostringstream(); // reinitialize my output stringstream dbus_connection_close(dbus_conn); // https://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga2522ac5075dfe0a1535471f6e045e1ee dbus_connection_unref(dbus_conn); // https://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html#ga6385ff09bc108238c4429e7c195dab25 }