Added Logger; Partial fix to issue #5

This commit is contained in:
Naim A 2013-08-10 22:12:08 +03:00
parent 8f4732acbc
commit 74de90bade
5 changed files with 120 additions and 4 deletions

View file

@ -26,6 +26,9 @@
#include <cassert> #include <cassert>
#include <cstring> // memcpy #include <cstring> // memcpy
#include "../multiplatform.h" #include "../multiplatform.h"
#include "../logging.h"
extern UDPT::Logger *logger;
using namespace std; using namespace std;
@ -309,7 +312,16 @@ namespace UDPT
sqlite3_stmt *collectStats; sqlite3_stmt *collectStats;
sqlite3_prepare(this->db, sStr.str().c_str(), sStr.str().length(), &collectStats, NULL); 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; int seeders = 0, leechers = 0;
while (sqlite3_step(collectStats) == SQLITE_ROW) // expecting two results. while (sqlite3_step(collectStats) == SQLITE_ROW) // expecting two results.
{ {

34
src/logging.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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";
}
};

51
src/logging.h Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef LOGGING_H_
#define LOGGING_H_
#include "settings.hpp"
#include <string>
#include <iostream>
#include <queue>
#include <time.h>
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_ */

View file

@ -19,6 +19,7 @@
#include <iostream> #include <iostream>
#include "logging.h"
#include "multiplatform.h" #include "multiplatform.h"
#include "udpTracker.hpp" #include "udpTracker.hpp"
#include "settings.hpp" #include "settings.hpp"
@ -30,6 +31,8 @@ using namespace std;
using namespace UDPT; using namespace UDPT;
using namespace UDPT::Server; using namespace UDPT::Server;
Logger *logger;
static void _print_usage () static void _print_usage ()
{ {
cout << "Usage: udpt [<configuration file>]" << endl; cout << "Usage: udpt [<configuration file>]" << endl;
@ -90,6 +93,10 @@ int main(int argc, char *argv[])
{ {
_print_usage (); _print_usage ();
} }
else if (argc >= 2)
{
config_file = argv[1]; // reported in issue #5.
}
settings = new Settings (config_file); 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; cerr << "Failed to read from '" << config_file.c_str() << "'. Using default settings." << endl;
} }
logger = new Logger (settings, cout);
usi = new UDPTracker (settings); usi = new UDPTracker (settings);
HTTPServer *apiSrv = NULL; HTTPServer *apiSrv = NULL;

View file

@ -23,7 +23,11 @@
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <iostream> #include <iostream>
#include <sstream>
#include "multiplatform.h" #include "multiplatform.h"
#include "logging.h"
extern UDPT::Logger *logger;
using namespace std; using namespace std;
using namespace UDPT::Data; using namespace UDPT::Data;
@ -126,7 +130,10 @@ namespace UDPT
this->isDynamic); this->isDynamic);
this->isRunning = true; 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. // create maintainer thread.
#ifdef WIN32 #ifdef WIN32
@ -137,8 +144,12 @@ namespace UDPT
for (i = 1;i < this->thread_count; i++) for (i = 1;i < this->thread_count; i++)
{ {
cout << "Starting thread (" << (i + 1) << "/" << ((int)this->thread_count) << ")" << endl; stringstream ss;
#ifdef WIN32 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); this->threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_thread_start, (LPVOID)this, 0, NULL);
#elif defined (linux) #elif defined (linux)
pthread_create (&(this->threads[i]), NULL, _thread_start, (void*)this); pthread_create (&(this->threads[i]), NULL, _thread_start, (void*)this);