Explorar o código

Add Logger Class

JDierkse %!s(int64=5) %!d(string=hai) anos
pai
achega
b4f9aa4cb2
Modificáronse 6 ficheiros con 73 adicións e 24 borrados
  1. 6 5
      Application/PresenceDetection.cc
  2. 4 7
      Bluetooth/Device.cpp
  3. 0 1
      Bluetooth/Functions.cpp
  4. 6 11
      UniFi/Device.cpp
  5. 20 0
      Util/Logger.cpp
  6. 37 0
      Util/Logger.h

+ 6 - 5
Application/PresenceDetection.cc

@@ -7,6 +7,7 @@
 #include <sys/stat.h>
 #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();
 

+ 4 - 7
Bluetooth/Device.cpp

@@ -1,7 +1,7 @@
 #include <algorithm>
 #include <sstream>
-#include <syslog.h>
 #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());
 	}
 }
 

+ 0 - 1
Bluetooth/Functions.cpp

@@ -1,7 +1,6 @@
 #include <sstream>
 #include <vector>
 #include <stdexcept>
-#include <syslog.h>
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/hci.h>
 #include <bluetooth/l2cap.h>

+ 6 - 11
UniFi/Device.cpp

@@ -1,6 +1,6 @@
 #include <sstream>
-#include <syslog.h>
 #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<std::mutex> 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());
 	}
 }
 

+ 20 - 0
Util/Logger.cpp

@@ -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 - 0
Util/Logger.h

@@ -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