Add Logger Class

This commit is contained in:
2020-03-05 12:59:29 +01:00
parent 96d4dc0be9
commit b4f9aa4cb2
6 changed files with 73 additions and 24 deletions
+6 -5
View File
@@ -7,6 +7,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "Util/clipp.h" #include "Util/clipp.h"
#include "Util/INIh.h" #include "Util/INIh.h"
#include "Util/Logger.h"
#include "UniFi/Device.h" #include "UniFi/Device.h"
#include "Bluetooth/Device.h" #include "Bluetooth/Device.h"
@@ -15,8 +16,8 @@ int main(int argc, char** argv)
{ {
try try
{ {
setlogmask(LOG_UPTO(LOG_INFO)); setlogmask(LOG_UPTO(LOG_DEBUG));
openlog("PresenceDetection", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER); openlog("PresenceDetection", LOG_NDELAY | LOG_PID, LOG_USER);
bool daemon = false; bool daemon = false;
PresenceDetection::Util::group cli { PresenceDetection::Util::group cli {
@@ -38,7 +39,7 @@ int main(int argc, char** argv)
if (daemon) if (daemon)
{ {
syslog(LOG_INFO, "Starting Daemon"); PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Starting Daemon");
pid_t pid, sid; pid_t pid, sid;
pid = fork(); pid = fork();
@@ -112,7 +113,7 @@ int main(int argc, char** argv)
int sig; int sig;
sigwait(&wset,&sig); sigwait(&wset,&sig);
syslog(LOG_INFO, "Stopping..."); PresenceDetection::Util::Logger::Log(PresenceDetection::Util::Logger::Severity::Info, "Stopping...");
if (pUniFiDevice) if (pUniFiDevice)
pUniFiDevice->Stop(); pUniFiDevice->Stop();
@@ -134,7 +135,7 @@ int main(int argc, char** argv)
ss << "ERROR: " << e.what() << std::endl; ss << "ERROR: " << e.what() << std::endl;
std::cerr << ss.str() << 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(); closelog();
+4 -7
View File
@@ -1,7 +1,7 @@
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include <syslog.h>
#include "Util/JSON.h" #include "Util/JSON.h"
#include "Util/Logger.h"
#include "Functions.h" #include "Functions.h"
#include "Device.h" #include "Device.h"
@@ -57,7 +57,7 @@ void Device::UpdateDevicesFromInventory()
{ {
std::stringstream ss; std::stringstream ss;
ss << "Bluetooth::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl; 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; std::stringstream ss;
ss << "Bluetooth::Device::UpdatePresentDevices() - Error: " << e.what() << std::endl; 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; std::stringstream ss;
ss << "Bluetooth: " << sign << " " << macAddress; ss << "Bluetooth: " << sign << " " << macAddress;
syslog(LOG_INFO, "%s", ss.str().c_str()); Util::Logger::Log(Util::Logger::Severity::Info, ss.str());
std::stringstream url; std::stringstream url;
url << m_target << "/Bluetooth/" << sign << "/" << macAddress; url << m_target << "/Bluetooth/" << sign << "/" << macAddress;
@@ -114,9 +114,6 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
} }
catch (const std::exception& e) 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());
} }
} }
-1
View File
@@ -1,7 +1,6 @@
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <stdexcept> #include <stdexcept>
#include <syslog.h>
#include <bluetooth/bluetooth.h> #include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h> #include <bluetooth/hci.h>
#include <bluetooth/l2cap.h> #include <bluetooth/l2cap.h>
+6 -11
View File
@@ -1,6 +1,6 @@
#include <sstream> #include <sstream>
#include <syslog.h>
#include "Util/JSON.h" #include "Util/JSON.h"
#include "Util/Logger.h"
#include "Device.h" #include "Device.h"
@@ -69,7 +69,7 @@ bool Device::Login()
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::Login() - Error: " << e.what() << std::endl; 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<std::mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
m_loggedIn = false; m_loggedIn = false;
return m_loggedIn; return m_loggedIn;
@@ -96,7 +96,7 @@ void Device::Logout()
{ {
std::stringstream ss; std::stringstream ss;
ss << "UniFi::Device::Logout() - Error: " << e.what() << std::endl; 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; std::stringstream ss;
ss << "UniFi::Device::GetDevicesFromInventory() - Error: " << e.what() << std::endl; 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; std::stringstream ss;
ss << "UniFi::Device::IsDevicePresent() - Error: " << e.what() << std::endl; 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(); Logout();
return; return;
} }
@@ -206,10 +206,8 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
std::stringstream ss; std::stringstream ss;
ss << "UniFi: " << sign << " " << macAddress; 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 try
{ {
@@ -217,9 +215,6 @@ void Device::SendStateChange(bool present, const std::string& macAddress)
} }
catch (const std::exception& e) 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());
} }
} }
+20
View File
@@ -0,0 +1,20 @@
#include <iostream>
#include <syslog.h>
#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<int>(severity), "%s", message.c_str());
}
} // namespace Util
} // namespace PresenceDetection
+37
View File
@@ -0,0 +1,37 @@
#ifndef UTIL_LOGGER_H
#define UTIL_LOGGER_H
#include <string>
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