From c0d93e63407919206aca0242804f9cc0de25aa27 Mon Sep 17 00:00:00 2001 From: Naim A Date: Fri, 5 Feb 2016 16:36:44 +0200 Subject: [PATCH] basic logging + windows service fix --- src/db/database.hpp | 2 ++ src/db/driver_sqlite.cpp | 18 ++++++++++++++-- src/db/driver_sqlite.hpp | 1 + src/exceptions.h | 33 ++++++++++++++++++++++++++++-- src/main.cpp | 44 +++++++++++++++++++++++++++------------- src/service.cpp | 9 ++++---- src/udpTracker.cpp | 5 +++++ 7 files changed, 90 insertions(+), 22 deletions(-) diff --git a/src/db/database.hpp b/src/db/database.hpp index 878da73..31513c8 100644 --- a/src/db/database.hpp +++ b/src/db/database.hpp @@ -21,6 +21,8 @@ #define DATABASE_HPP_ #include +#include +#include namespace UDPT { diff --git a/src/db/driver_sqlite.cpp b/src/db/driver_sqlite.cpp index 3cf383d..78f3706 100644 --- a/src/db/driver_sqlite.cpp +++ b/src/db/driver_sqlite.cpp @@ -66,7 +66,7 @@ namespace UDPT return data; } - SQLite3Driver::SQLite3Driver(const boost::program_options::variables_map& conf, bool isDyn) : DatabaseDriver(conf, isDyn) + SQLite3Driver::SQLite3Driver(const boost::program_options::variables_map& conf, bool isDyn) : DatabaseDriver(conf, isDyn), m_logger(boost::log::keywords::channel="SQLite3") { int r; bool doSetup; @@ -177,6 +177,8 @@ namespace UDPT } } + BOOST_LOG_SEV(m_logger, boost::log::trivial::debug) << "Retrieved " << i << " peers"; + sqlite3_finalize(stmt); *max_count = i; @@ -255,7 +257,16 @@ namespace UDPT // create table. r = sqlite3_exec(this->db, sql.c_str(), NULL, NULL, &err_msg); - return (r == SQLITE_OK); + if (SQLITE_OK == r) + { + BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Added torrent"; + return true; + } + else + { + BOOST_LOG_SEV(m_logger, boost::log::trivial::error) << "Failed to add torrent: SQLite3 error code = " << r; + return false; + } } bool SQLite3Driver::isTorrentAllowed(uint8_t *info_hash) @@ -357,6 +368,8 @@ namespace UDPT sqlite3_exec(this->db, str.c_str(), NULL, NULL, NULL); + BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Torrent removed."; + return true; } @@ -411,6 +424,7 @@ namespace UDPT SQLite3Driver::~SQLite3Driver() { + BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Closing SQLite"; sqlite3_close(this->db); } }; diff --git a/src/db/driver_sqlite.hpp b/src/db/driver_sqlite.hpp index 048abbc..55f7014 100644 --- a/src/db/driver_sqlite.hpp +++ b/src/db/driver_sqlite.hpp @@ -46,6 +46,7 @@ namespace UDPT virtual ~SQLite3Driver(); private: sqlite3 *db; + boost::log::sources::severity_channel_logger_mt<> m_logger; void doSetup(); }; diff --git a/src/exceptions.h b/src/exceptions.h index 589bbdc..33c5213 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -7,11 +7,15 @@ namespace UDPT class UDPTException { public: - UDPTException(const char* errorMsg = "", int errorCode = 0) : m_error(errorMsg), m_errorCode(errorCode) + UDPTException(const char* errorMsg, int errorCode = 0) : m_error(errorMsg), m_errorCode(errorCode) { } + UDPTException(int errorCode = 0) : m_errorCode(errorCode), m_error("") + { + } + virtual const char* what() const { return m_error; @@ -39,9 +43,34 @@ namespace UDPT #ifdef WIN32 = ::GetLastError() #endif - ) : UDPTException("OSError", errorCode) + ) : UDPTException(errorCode) { } + virtual ~OSError() {} + + const char* what() const + { + if (m_errorMessage.length() > 0) + { + return m_errorMessage.c_str(); + } + +#ifdef WIN32 + char *buffer = nullptr; + DWORD msgLen = ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 0, m_errorCode, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), reinterpret_cast(&buffer), 1, NULL); + std::shared_ptr formatStr = std::shared_ptr( + buffer, + ::LocalFree); + m_errorMessage = std::string(reinterpret_cast(formatStr.get())); + + return m_errorMessage.c_str(); +#else + return "OSError"; +#endif + } + private: + // allow to generate a message only when needed. + mutable std::string m_errorMessage; }; } diff --git a/src/main.cpp b/src/main.cpp index 7abf65c..9f469fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -64,13 +64,19 @@ static void daemonize(const boost::program_options::variables_map& conf) } #endif +#ifdef WIN32 +void _close_wsa() +{ + ::WSACleanup(); +} +#endif + int main(int argc, char *argv[]) { - Tracker& tracker = UDPT::Tracker::getInstance(); - #ifdef WIN32 WSADATA wsadata; ::WSAStartup(MAKEWORD(2, 2), &wsadata); + ::atexit(_close_wsa); #endif boost::program_options::options_description commandLine("Command line options"); @@ -162,6 +168,10 @@ int main(int argc, char *argv[]) } } + // setup logging... + + boost::log::sources::severity_channel_logger_mt<> logger(boost::log::keywords::channel = "main"); + #ifdef linux if (!var_map.count("interactive")) { @@ -178,15 +188,15 @@ int main(int argc, char *argv[]) { if ("install" == action) { - std::cout << "Installing service..." << std::endl; + std::cerr << "Installing service..." << std::endl; svc.install(); - std::cout << "Installed." << std::endl; + std::cerr << "Installed." << std::endl; } else if ("uninstall" == action) { - std::cout << "Removing service..." << std::endl; + std::cerr << "Removing service..." << std::endl; svc.uninstall(); - std::cout << "Removed." << std::endl; + std::cerr << "Removed." << std::endl; } else if ("start" == action) { @@ -199,7 +209,7 @@ int main(int argc, char *argv[]) } catch (const UDPT::OSError& ex) { - std::cout << "An operating system error occurred: " << ex.getErrorCode() << std::endl; + std::cerr << "An operating system error occurred: " << ex.getErrorCode() << std::endl; return -1; } @@ -214,17 +224,23 @@ int main(int argc, char *argv[]) { if (ERROR_FAILED_SERVICE_CONTROLLER_CONNECT != err.getErrorCode()) { - // TODO: log this error and exit + BOOST_LOG_SEV(logger, boost::log::trivial::fatal) << "Failed to start as a Windows service: (" << err.getErrorCode() << "): " << err.what(); + return -1; } } #endif - tracker.start(var_map); - tracker.wait(); - -#ifdef WIN32 - ::WSACleanup(); -#endif + try + { + Tracker& tracker = UDPT::Tracker::getInstance(); + tracker.start(var_map); + tracker.wait(); + } + catch (const UDPT::UDPTException& ex) + { + BOOST_LOG_SEV(logger, boost::log::trivial::fatal) << "UDPT exception: (" << ex.getErrorCode() << "): " << ex.what(); + return -1; + } return 0; } diff --git a/src/service.cpp b/src/service.cpp index 1d2169b..879639e 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -83,10 +83,11 @@ namespace UDPT void Service::setup() { - SERVICE_TABLE_ENTRY service = { 0 }; - service.lpServiceName = const_cast(m_conf["service.name"].as().c_str()); - service.lpServiceProc = reinterpret_cast(&Service::serviceMain); - if (FALSE == ::StartServiceCtrlDispatcher(&service)) + SERVICE_TABLE_ENTRY service[] = { + { const_cast(m_conf["service.name"].as().c_str()), reinterpret_cast(&Service::serviceMain) }, + {0, 0} + }; + if (FALSE == ::StartServiceCtrlDispatcher(service)) { throw OSError(); } diff --git a/src/udpTracker.cpp b/src/udpTracker.cpp index 2d6c976..a8a04c2 100644 --- a/src/udpTracker.cpp +++ b/src/udpTracker.cpp @@ -100,6 +100,11 @@ namespace UDPT throw UDPT::UDPTException("Failed to bind socket."); } + { + char buff[INET_ADDRSTRLEN]; + BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "UDP tracker bound on " << ::inet_ntop(AF_INET, reinterpret_cast(&m_localEndpoint.sin_addr), buff, sizeof(buff)) << ":" << ::htons(m_localEndpoint.sin_port); + } + this->m_sock = sock; // create maintainer thread.