basic logging + windows service fix

This commit is contained in:
Naim A 2016-02-05 16:36:44 +02:00
parent d1d879c27c
commit c0d93e6340
7 changed files with 90 additions and 22 deletions

View file

@ -21,6 +21,8 @@
#define DATABASE_HPP_
#include <boost/program_options.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
namespace UDPT
{

View file

@ -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);
}
};

View file

@ -46,6 +46,7 @@ namespace UDPT
virtual ~SQLite3Driver();
private:
sqlite3 *db;
boost::log::sources::severity_channel_logger_mt<> m_logger;
void doSetup();
};

View file

@ -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<LPSTR>(&buffer), 1, NULL);
std::shared_ptr<void> formatStr = std::shared_ptr<void>(
buffer,
::LocalFree);
m_errorMessage = std::string(reinterpret_cast<char*>(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;
};
}

View file

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

View file

@ -83,10 +83,11 @@ namespace UDPT
void Service::setup()
{
SERVICE_TABLE_ENTRY service = { 0 };
service.lpServiceName = const_cast<char*>(m_conf["service.name"].as<std::string>().c_str());
service.lpServiceProc = reinterpret_cast<LPSERVICE_MAIN_FUNCTION>(&Service::serviceMain);
if (FALSE == ::StartServiceCtrlDispatcher(&service))
SERVICE_TABLE_ENTRY service[] = {
{ const_cast<char*>(m_conf["service.name"].as<std::string>().c_str()), reinterpret_cast<LPSERVICE_MAIN_FUNCTION>(&Service::serviceMain) },
{0, 0}
};
if (FALSE == ::StartServiceCtrlDispatcher(service))
{
throw OSError();
}

View file

@ -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<LPVOID>(&m_localEndpoint.sin_addr), buff, sizeof(buff)) << ":" << ::htons(m_localEndpoint.sin_port);
}
this->m_sock = sock;
// create maintainer thread.