From 74de90badea2b0a09b9ad860e0bce57d7fef8633 Mon Sep 17 00:00:00 2001 From: Naim A Date: Sat, 10 Aug 2013 22:12:08 +0300 Subject: [PATCH] Added Logger; Partial fix to issue #5 --- src/db/driver_sqlite.cpp | 14 ++++++++++- src/logging.cpp | 34 +++++++++++++++++++++++++++ src/logging.h | 51 ++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 8 +++++++ src/udpTracker.cpp | 17 +++++++++++--- 5 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 src/logging.cpp create mode 100644 src/logging.h diff --git a/src/db/driver_sqlite.cpp b/src/db/driver_sqlite.cpp index 47dcba8..9601420 100644 --- a/src/db/driver_sqlite.cpp +++ b/src/db/driver_sqlite.cpp @@ -26,6 +26,9 @@ #include #include // memcpy #include "../multiplatform.h" +#include "../logging.h" + +extern UDPT::Logger *logger; using namespace std; @@ -309,7 +312,16 @@ namespace UDPT sqlite3_stmt *collectStats; sqlite3_prepare(this->db, sStr.str().c_str(), sStr.str().length(), &collectStats, NULL); - cout << "[" << sqlite3_errmsg(this->db) << "]" << endl; + + if (sqlite3_errcode(this->db) != SQLITE_OK) + { + string str; + str = "["; + str += sqlite3_errmsg(this->db); + str += "]"; + logger->log(Logger::LL_ERROR, str); + } + int seeders = 0, leechers = 0; while (sqlite3_step(collectStats) == SQLITE_ROW) // expecting two results. { diff --git a/src/logging.cpp b/src/logging.cpp new file mode 100644 index 0000000..bb49555 --- /dev/null +++ b/src/logging.cpp @@ -0,0 +1,34 @@ +/* + * Copyright © 2012,2013 Naim A. + * + * This file is part of UDPT. + * + * UDPT is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * UDPT is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with UDPT. If not, see . + */ + +#include "logging.h" + +namespace UDPT { + Logger::Logger(Settings *s, ostream &os) : logfile (os) + { + this->max_time = 120; + this->queue_limit = 50; + } + + void Logger::log(enum LogLevel lvl, string msg) + { + logfile << time (NULL) << ": (" << ((char)lvl) << "): "; + logfile << msg << "\n"; + } +}; diff --git a/src/logging.h b/src/logging.h new file mode 100644 index 0000000..b366592 --- /dev/null +++ b/src/logging.h @@ -0,0 +1,51 @@ +/* + * Copyright © 2012,2013 Naim A. + * + * This file is part of UDPT. + * + * UDPT is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * UDPT is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with UDPT. If not, see . + */ + +#ifndef LOGGING_H_ +#define LOGGING_H_ + +#include "settings.hpp" +#include +#include +#include +#include + +namespace UDPT { + using namespace std; + class Logger { + + public: + enum LogLevel { + LL_ERROR = 'E', + LL_WARNING = 'W', + LL_INFO = 'I', + LL_DEBUG = 'D' + }; + + Logger (Settings *s, ostream &os); + + void log (enum LogLevel, string msg); + private: + ostream &logfile; + unsigned int queue_limit; + int max_time; + }; +}; + +#endif /* LOGGING_H_ */ diff --git a/src/main.cpp b/src/main.cpp index cb72cb9..a814fa1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include +#include "logging.h" #include "multiplatform.h" #include "udpTracker.hpp" #include "settings.hpp" @@ -30,6 +31,8 @@ using namespace std; using namespace UDPT; using namespace UDPT::Server; +Logger *logger; + static void _print_usage () { cout << "Usage: udpt []" << endl; @@ -90,6 +93,10 @@ int main(int argc, char *argv[]) { _print_usage (); } + else if (argc >= 2) + { + config_file = argv[1]; // reported in issue #5. + } settings = new Settings (config_file); @@ -119,6 +126,7 @@ int main(int argc, char *argv[]) cerr << "Failed to read from '" << config_file.c_str() << "'. Using default settings." << endl; } + logger = new Logger (settings, cout); usi = new UDPTracker (settings); HTTPServer *apiSrv = NULL; diff --git a/src/udpTracker.cpp b/src/udpTracker.cpp index a067177..2e130a9 100644 --- a/src/udpTracker.cpp +++ b/src/udpTracker.cpp @@ -23,7 +23,11 @@ #include #include #include +#include #include "multiplatform.h" +#include "logging.h" + +extern UDPT::Logger *logger; using namespace std; using namespace UDPT::Data; @@ -126,7 +130,10 @@ namespace UDPT this->isDynamic); this->isRunning = true; - cout << "Starting maintenance thread (1/" << ((int)this->thread_count) << ")" << endl; + + stringstream ss; + ss << "Starting maintenance thread (1/" << ((int)this->thread_count) << ")"; + logger->log(Logger::LL_INFO, ss.str()); // create maintainer thread. #ifdef WIN32 @@ -137,8 +144,12 @@ namespace UDPT for (i = 1;i < this->thread_count; i++) { - cout << "Starting thread (" << (i + 1) << "/" << ((int)this->thread_count) << ")" << endl; - #ifdef WIN32 + stringstream ss; + ss << "Starting thread (" << (i + 1) << "/" << ((int)this->thread_count) << ")"; + logger->log(Logger::LL_INFO, ss.str()); + ss.clear(); + + #ifdef WIN32 this->threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_thread_start, (LPVOID)this, 0, NULL); #elif defined (linux) pthread_create (&(this->threads[i]), NULL, _thread_start, (void*)this);