basic logging + windows service fix
This commit is contained in:
parent
d1d879c27c
commit
c0d93e6340
|
@ -21,6 +21,8 @@
|
||||||
#define DATABASE_HPP_
|
#define DATABASE_HPP_
|
||||||
|
|
||||||
#include <boost/program_options.hpp>
|
#include <boost/program_options.hpp>
|
||||||
|
#include <boost/log/trivial.hpp>
|
||||||
|
#include <boost/log/sources/severity_channel_logger.hpp>
|
||||||
|
|
||||||
namespace UDPT
|
namespace UDPT
|
||||||
{
|
{
|
||||||
|
|
|
@ -66,7 +66,7 @@ namespace UDPT
|
||||||
return data;
|
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;
|
int r;
|
||||||
bool doSetup;
|
bool doSetup;
|
||||||
|
@ -177,6 +177,8 @@ namespace UDPT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_LOG_SEV(m_logger, boost::log::trivial::debug) << "Retrieved " << i << " peers";
|
||||||
|
|
||||||
sqlite3_finalize(stmt);
|
sqlite3_finalize(stmt);
|
||||||
|
|
||||||
*max_count = i;
|
*max_count = i;
|
||||||
|
@ -255,7 +257,16 @@ namespace UDPT
|
||||||
// create table.
|
// create table.
|
||||||
r = sqlite3_exec(this->db, sql.c_str(), NULL, NULL, &err_msg);
|
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)
|
bool SQLite3Driver::isTorrentAllowed(uint8_t *info_hash)
|
||||||
|
@ -357,6 +368,8 @@ namespace UDPT
|
||||||
|
|
||||||
sqlite3_exec(this->db, str.c_str(), NULL, NULL, NULL);
|
sqlite3_exec(this->db, str.c_str(), NULL, NULL, NULL);
|
||||||
|
|
||||||
|
BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Torrent removed.";
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -411,6 +424,7 @@ namespace UDPT
|
||||||
|
|
||||||
SQLite3Driver::~SQLite3Driver()
|
SQLite3Driver::~SQLite3Driver()
|
||||||
{
|
{
|
||||||
|
BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Closing SQLite";
|
||||||
sqlite3_close(this->db);
|
sqlite3_close(this->db);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -46,6 +46,7 @@ namespace UDPT
|
||||||
virtual ~SQLite3Driver();
|
virtual ~SQLite3Driver();
|
||||||
private:
|
private:
|
||||||
sqlite3 *db;
|
sqlite3 *db;
|
||||||
|
boost::log::sources::severity_channel_logger_mt<> m_logger;
|
||||||
|
|
||||||
void doSetup();
|
void doSetup();
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,11 +7,15 @@ namespace UDPT
|
||||||
class UDPTException
|
class UDPTException
|
||||||
{
|
{
|
||||||
public:
|
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
|
virtual const char* what() const
|
||||||
{
|
{
|
||||||
return m_error;
|
return m_error;
|
||||||
|
@ -39,9 +43,34 @@ namespace UDPT
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
= ::GetLastError()
|
= ::GetLastError()
|
||||||
#endif
|
#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;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
44
src/main.cpp
44
src/main.cpp
|
@ -64,13 +64,19 @@ static void daemonize(const boost::program_options::variables_map& conf)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
void _close_wsa()
|
||||||
|
{
|
||||||
|
::WSACleanup();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
Tracker& tracker = UDPT::Tracker::getInstance();
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
WSADATA wsadata;
|
WSADATA wsadata;
|
||||||
::WSAStartup(MAKEWORD(2, 2), &wsadata);
|
::WSAStartup(MAKEWORD(2, 2), &wsadata);
|
||||||
|
::atexit(_close_wsa);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
boost::program_options::options_description commandLine("Command line options");
|
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
|
#ifdef linux
|
||||||
if (!var_map.count("interactive"))
|
if (!var_map.count("interactive"))
|
||||||
{
|
{
|
||||||
|
@ -178,15 +188,15 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if ("install" == action)
|
if ("install" == action)
|
||||||
{
|
{
|
||||||
std::cout << "Installing service..." << std::endl;
|
std::cerr << "Installing service..." << std::endl;
|
||||||
svc.install();
|
svc.install();
|
||||||
std::cout << "Installed." << std::endl;
|
std::cerr << "Installed." << std::endl;
|
||||||
}
|
}
|
||||||
else if ("uninstall" == action)
|
else if ("uninstall" == action)
|
||||||
{
|
{
|
||||||
std::cout << "Removing service..." << std::endl;
|
std::cerr << "Removing service..." << std::endl;
|
||||||
svc.uninstall();
|
svc.uninstall();
|
||||||
std::cout << "Removed." << std::endl;
|
std::cerr << "Removed." << std::endl;
|
||||||
}
|
}
|
||||||
else if ("start" == action)
|
else if ("start" == action)
|
||||||
{
|
{
|
||||||
|
@ -199,7 +209,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
catch (const UDPT::OSError& ex)
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,17 +224,23 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (ERROR_FAILED_SERVICE_CONTROLLER_CONNECT != err.getErrorCode())
|
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
|
#endif
|
||||||
|
|
||||||
tracker.start(var_map);
|
try
|
||||||
tracker.wait();
|
{
|
||||||
|
Tracker& tracker = UDPT::Tracker::getInstance();
|
||||||
#ifdef WIN32
|
tracker.start(var_map);
|
||||||
::WSACleanup();
|
tracker.wait();
|
||||||
#endif
|
}
|
||||||
|
catch (const UDPT::UDPTException& ex)
|
||||||
|
{
|
||||||
|
BOOST_LOG_SEV(logger, boost::log::trivial::fatal) << "UDPT exception: (" << ex.getErrorCode() << "): " << ex.what();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,10 +83,11 @@ namespace UDPT
|
||||||
|
|
||||||
void Service::setup()
|
void Service::setup()
|
||||||
{
|
{
|
||||||
SERVICE_TABLE_ENTRY service = { 0 };
|
SERVICE_TABLE_ENTRY service[] = {
|
||||||
service.lpServiceName = const_cast<char*>(m_conf["service.name"].as<std::string>().c_str());
|
{ const_cast<char*>(m_conf["service.name"].as<std::string>().c_str()), reinterpret_cast<LPSERVICE_MAIN_FUNCTION>(&Service::serviceMain) },
|
||||||
service.lpServiceProc = reinterpret_cast<LPSERVICE_MAIN_FUNCTION>(&Service::serviceMain);
|
{0, 0}
|
||||||
if (FALSE == ::StartServiceCtrlDispatcher(&service))
|
};
|
||||||
|
if (FALSE == ::StartServiceCtrlDispatcher(service))
|
||||||
{
|
{
|
||||||
throw OSError();
|
throw OSError();
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,6 +100,11 @@ namespace UDPT
|
||||||
throw UDPT::UDPTException("Failed to bind socket.");
|
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;
|
this->m_sock = sock;
|
||||||
|
|
||||||
// create maintainer thread.
|
// create maintainer thread.
|
||||||
|
|
Loading…
Reference in a new issue