From b4f9aa4cb2fb535fe2b4803ef685c45d0f1f89d8 Mon Sep 17 00:00:00 2001 From: JDierkse Date: Thu, 5 Mar 2020 12:59:29 +0100 Subject: [PATCH] Add Logger Class --- Application/PresenceDetection.cc | 11 +++++----- Bluetooth/Device.cpp | 11 ++++------ Bluetooth/Functions.cpp | 1 - UniFi/Device.cpp | 17 ++++++--------- Util/Logger.cpp | 20 +++++++++++++++++ Util/Logger.h | 37 ++++++++++++++++++++++++++++++++ 6 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 Util/Logger.cpp create mode 100644 Util/Logger.h diff --git a/Application/PresenceDetection.cc b/Application/PresenceDetection.cc index 5b444f5..53d1e62 100644 --- a/Application/PresenceDetection.cc +++ b/Application/PresenceDetection.cc @@ -7,6 +7,7 @@ #include #include "Util/clipp.h" #include "Util/INIh.h" +#include "Util/Logger.h" #include "UniFi/Device.h" #include "Bluetooth/Device.h" @@ -15,8 +16,8 @@ int main(int argc, char** argv) { try { - setlogmask(LOG_UPTO(LOG_INFO)); - openlog("PresenceDetection", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); + setlogmask(LOG_UPTO(LOG_DEBUG)); + openlog("PresenceDetection", LOG_NDELAY | LOG_PID, LOG_USER); bool daemon = false; PresenceDetection::Util::group cli { @@ -38,7 +39,7 @@ int main(int argc, char** argv) if (daemon) { - syslog(LOG_INFO, "Starting Daemon"); + PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Starting Daemon"); pid_t pid, sid; pid = fork(); @@ -112,7 +113,7 @@ int main(int argc, char** argv) int sig; sigwait(&wset,&sig); - syslog(LOG_INFO, "Stopping..."); + PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Stopping..."); if (pUniFiDevice) pUniFiDevice->Stop(); @@ -134,7 +135,7 @@ int main(int argc, char** argv) ss << "ERROR: " << e.what() << std::endl; std::cerr << ss.str() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); + PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Error, ss.str()); closelog(); diff --git a/Bluetooth/Device.cpp b/Bluetooth/Device.cpp index 7b6f7d6..f1b0589 100644 --- a/Bluetooth/Device.cpp +++ b/Bluetooth/Device.cpp @@ -1,7 +1,7 @@ #include #include -#include #include "Util/JSON.h" +#include "Util/Logger.h" #include "Functions.h" #include "Device.h" @@ -57,7 +57,7 @@ void Device::UpdateDevicesFromInventory() { std::stringstream ss; ss << "Bluetooth::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); + Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); } } @@ -82,7 +82,7 @@ void Device::UpdatePresentDevices() { std::stringstream ss; ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); + Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); } } @@ -103,7 +103,7 @@ void Device::SendStateChange(bool present, const std::string& macAddress) std::stringstream ss; ss << "Bluetooth: " << sign << " " << macAddress; - syslog(LOG_INFO, "%s", ss.str().c_str()); + Util::Logger::Log(Util::Logger::Severity::Info, ss.str()); std::stringstream url; url << m_target << "/Bluetooth/" << sign << "/" << macAddress; @@ -114,9 +114,6 @@ void Device::SendStateChange(bool present, const std::string& macAddress) } catch (const std::exception& e) { - std::stringstream ss; - ss << "Bluetooth::Device::SendStateChange() - Error: " << e.what() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); } } diff --git a/Bluetooth/Functions.cpp b/Bluetooth/Functions.cpp index 4d941a9..54db8ab 100644 --- a/Bluetooth/Functions.cpp +++ b/Bluetooth/Functions.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/UniFi/Device.cpp b/UniFi/Device.cpp index 6e27c88..9380a4d 100644 --- a/UniFi/Device.cpp +++ b/UniFi/Device.cpp @@ -1,6 +1,6 @@ #include -#include #include "Util/JSON.h" +#include "Util/Logger.h" #include "Device.h" @@ -69,7 +69,7 @@ bool Device::Login() { std::stringstream ss; ss << "UniFi::Device::Login() - Error: " << e.what() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); + Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); std::lock_guard lock(m_mutex); m_loggedIn = false; return m_loggedIn; @@ -96,7 +96,7 @@ void Device::Logout() { std::stringstream ss; ss << "UniFi::Device::Logout() - Error: " << e.what() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); + Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); } } @@ -120,7 +120,7 @@ void Device::UpdateDevicesFromInventory() { std::stringstream ss; ss << "UniFi::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); + Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); } } @@ -178,7 +178,7 @@ void Device::UpdatePresentDevices() { std::stringstream ss; ss << "UniFi::Device::IsDevicePresent() - Error: " << e.what() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); + Util::Logger::Log(Util::Logger::Severity::Error, ss.str()); Logout(); return; } @@ -206,10 +206,8 @@ void Device::SendStateChange(bool present, const std::string& macAddress) std::stringstream ss; ss << "UniFi: " << sign << " " << macAddress; - syslog(LOG_INFO, "%s", ss.str().c_str()); + Util::Logger::Log(Util::Logger::Severity::Info, ss.str()); - std::stringstream url; - url << m_target << "/UniFi/" << sign << "/" << macAddress; try { @@ -217,9 +215,6 @@ void Device::SendStateChange(bool present, const std::string& macAddress) } catch (const std::exception& e) { - std::stringstream ss; - ss << "UniFi::Device::SendStateChange() - Error: " << e.what() << std::endl; - syslog(LOG_ERR, "%s", ss.str().c_str()); } } diff --git a/Util/Logger.cpp b/Util/Logger.cpp new file mode 100644 index 0000000..48eeedc --- /dev/null +++ b/Util/Logger.cpp @@ -0,0 +1,20 @@ +#include +#include +#include "Logger.h" + + +namespace PresenceDetection { +namespace Util { + +void Logger::Log(Severity::type severity, const std::string& message) +{ + if (severity > Severity::Error) + std::cout << message << std::endl; + else + std::cerr << message << std::endl; + + syslog(static_cast(severity), "%s", message.c_str()); +} + +} // namespace Util +} // namespace PresenceDetection diff --git a/Util/Logger.h b/Util/Logger.h new file mode 100644 index 0000000..b807d07 --- /dev/null +++ b/Util/Logger.h @@ -0,0 +1,37 @@ +#ifndef UTIL_LOGGER_H +#define UTIL_LOGGER_H + +#include + + +namespace PresenceDetection { +namespace Util { + +class Logger +{ +public: + class Severity + { + public: + enum type + { + Emergency = 0, + Alert = 1, + Critical = 2, + Error = 3, + Warning = 4, + Notice = 5, + Info = 6, + Debug = 7 + }; + }; + + +public: + static void Log(Severity::type severity, const std::string& message); +}; + +} // namespace Util +} // namespace PresenceDetection + +#endif // UTIL_LOGGER_H