wip
This commit is contained in:
1 parent
0dfda6f481
commit
9fac304771
9 files changed
+116
-51
No files matched your search
+4
-4
@@ -2,12 +2,12 @@ cmake_minimum_required(VERSION 3.16)
|
|||||||
|
|
||||||
project(distrins)
|
project(distrins)
|
||||||
|
|
||||||
add_library(asio INTERFACE)
|
add_library(Asio INTERFACE IMPORTED GLOBAL)
|
||||||
target_compile_options(asio INTERFACE -D ASIO_STANDALONE)
|
target_compile_options(Asio INTERFACE -D ASIO_STANDALONE)
|
||||||
target_include_directories(asio INTERFACE lib/asio/include)
|
target_include_directories(Asio INTERFACE lib/asio/include)
|
||||||
|
|
||||||
find_package (Threads REQUIRED)
|
find_package (Threads REQUIRED)
|
||||||
target_link_libraries(asio INTERFACE Threads::Threads)
|
target_link_libraries(Asio INTERFACE Threads::Threads)
|
||||||
|
|
||||||
add_subdirectory(src)
|
add_subdirectory(src)
|
||||||
|
|
||||||
@@ -1,18 +1,14 @@
|
|||||||
#include "EventBus.hpp"
|
#include "EventBus.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace Plains {
|
namespace Plains {
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
EventBus::EventBus() {};
|
EventBus::EventBus(Logger& logger) : logger(logger) {};
|
||||||
|
|
||||||
void EventBus::test() {
|
void EventBus::test() {
|
||||||
std::cout << "Testing from within EventBus!" << std::endl;
|
logger.debug << "Testing from within EventBus!" << Logger::commit;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EventBus::run() {
|
|
||||||
std::cout << "Run event queue.... oops done! Exiting..." << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
}}
|
}}
|
||||||
+70
-18
@@ -9,32 +9,41 @@ using std::cout;
|
|||||||
using std::cerr;
|
using std::cerr;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
|
|
||||||
//static string getTimeStamp() { return ctime ( time ( 0 ) ); }
|
Logger::Logger(enum Level logLevel) :
|
||||||
|
|
||||||
Logger::Logger(enum Level logLevel, EventBus& eventBus) :
|
|
||||||
logLevel(logLevel),
|
logLevel(logLevel),
|
||||||
eventBus(eventBus),
|
|
||||||
debug(DEBUG, *this),
|
debug(DEBUG, *this),
|
||||||
info(INFO, *this),
|
info(INFO, *this),
|
||||||
warn(WARN, *this),
|
warn(WARN, *this),
|
||||||
fatal(FATAL, *this)
|
fatal(FATAL, *this)
|
||||||
{
|
{
|
||||||
//info(this);
|
output = new ofstream("plains.log");
|
||||||
logFile = ofstream("plains.log");
|
|
||||||
|
|
||||||
if (!logFile) {
|
if (!output) {
|
||||||
cerr << "Unable to open Logfile" << endl;
|
cerr << "Unable to open Logfile!" << endl;
|
||||||
//logFile = cout.fstream;
|
//std::ostream osteam();
|
||||||
|
//aoutput = cout;
|
||||||
|
//output->rdbuf(cout.rdbuf());
|
||||||
|
//output.rdbuf(NULL);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
cout << "Log file opened" << endl;
|
debug << "Logfile successfully opened!" << commit;
|
||||||
//oldStdOut = cout.rdbuf();
|
|
||||||
//cout.rdbuf(logFile.rdbuf());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::~Logger() {
|
Logger::~Logger() {
|
||||||
logFile.close();
|
output->close();
|
||||||
|
delete output;
|
||||||
|
}
|
||||||
|
|
||||||
|
string Logger::genTimeStamp() {
|
||||||
|
time_t rawtime;
|
||||||
|
struct tm * timeinfo;
|
||||||
|
char buffer[80];
|
||||||
|
time (&rawtime);
|
||||||
|
timeinfo = localtime(&rawtime);
|
||||||
|
strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
|
||||||
|
|
||||||
|
return string(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::setLogLevel(Level level) {
|
void Logger::setLogLevel(Level level) {
|
||||||
@@ -51,26 +60,69 @@ void Logger::log(Level lvl, const char* msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::Sink::Sink (const Level level, Logger& parent) : level(level), parent(parent) {}
|
Logger::Sink::Sink (const Level level, Logger& parent) :
|
||||||
|
level(level),
|
||||||
|
parent(parent),
|
||||||
|
lineIsPrefixed(false) {}
|
||||||
|
|
||||||
void Logger::Sink::ln ( const string& msg ) {
|
void Logger::Sink::ln ( const string& msg ) {
|
||||||
if ( level >= parent.logLevel ) parent.logFile << "DATESTAMP [" << level << "] " << msg << endl;
|
ln(msg.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::Sink::ln ( const char* msg ) {
|
void Logger::Sink::ln ( const char* msg ) {
|
||||||
if ( level >= parent.logLevel ) parent.logFile << "DATESTAMP [" << level << "] " << msg << endl;
|
if ( level >= parent.logLevel ) {
|
||||||
|
// If line has already been started, abort it and start new one:
|
||||||
|
if(lineIsPrefixed) *parent.output << endl;
|
||||||
|
prefixLine(true);
|
||||||
|
*parent.output << msg << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::Sink::prefixLine(bool resetToFalse) {
|
||||||
|
if (!lineIsPrefixed) {
|
||||||
|
*parent.output << Logger::genTimeStamp() << " ";
|
||||||
|
lineIsPrefixed = resetToFalse ? false : true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::Sink& Logger::Sink::operator<<(const string& msg) {
|
Logger::Sink& Logger::Sink::operator<<(const string& msg) {
|
||||||
if ( level >= parent.logLevel ) parent.logFile << "DATESTAMP [" << level << "] " << msg;
|
if ( level >= parent.logLevel ) {
|
||||||
|
prefixLine();
|
||||||
|
*parent.output << msg;
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::Sink& Logger::Sink::operator<<(const char* msg) {
|
Logger::Sink& Logger::Sink::operator<<(const char* msg) {
|
||||||
if ( level >= parent.logLevel ) parent.logFile << "DATESTAMP [" << level << "] " << msg;
|
if ( level >= parent.logLevel ) {
|
||||||
|
prefixLine();
|
||||||
|
*parent.output << msg;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::Sink& Logger::Sink::operator<<(const unsigned long msg) {
|
||||||
|
if ( level >= parent.logLevel ) {
|
||||||
|
prefixLine();
|
||||||
|
*parent.output << msg;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::Sink& Logger::Sink::operator<<(const char character) {
|
||||||
|
if ( level >= parent.logLevel ) {
|
||||||
|
switch(character) {
|
||||||
|
// COMMIT - End message line
|
||||||
|
case 0:
|
||||||
|
*parent.output << endl;
|
||||||
|
lineIsPrefixed = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}}
|
}}
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
#ifndef EVENT_BUS_HPP
|
#ifndef EVENT_BUS_HPP
|
||||||
#define EVENT_BUS_HPP
|
#define EVENT_BUS_HPP
|
||||||
|
#include "Logger.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace Plains {
|
namespace Plains {
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
class EventBus {
|
class EventBus {
|
||||||
|
private:
|
||||||
|
Logger& logger;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EventBus();
|
EventBus(Logger& logger);
|
||||||
void test();
|
void test();
|
||||||
void run(); // Run the event queue worker
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -3,35 +3,39 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include "EventBus.hpp"
|
|
||||||
|
|
||||||
namespace Plains {
|
namespace Plains {
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::ofstream;
|
using std::ofstream;
|
||||||
using std::ostream;
|
//using std::ostream;
|
||||||
using std::streambuf;
|
//using std::streambuf;
|
||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
public:
|
public:
|
||||||
enum Level { DEBUG, INFO, WARN, FATAL };
|
enum Level { DEBUG, INFO, WARN, FATAL };
|
||||||
Logger(Level logLevel, EventBus& eventBus);
|
Logger(Level logLevel);
|
||||||
~Logger();
|
~Logger();
|
||||||
void setLogLevel(Level level);
|
void setLogLevel(Level level);
|
||||||
void log(Level lvl, const char* msg);
|
void log(Level lvl, const char* msg);
|
||||||
void log(Level lvl, const string& msg);
|
void log(Level lvl, const string& msg);
|
||||||
|
static const char commit = 0;
|
||||||
|
|
||||||
class Sink {
|
class Sink {
|
||||||
private:
|
private:
|
||||||
|
bool lineIsPrefixed;
|
||||||
Logger& parent;
|
Logger& parent;
|
||||||
const Logger::Level level;
|
const Logger::Level level;
|
||||||
|
void prefixLine(bool resetToFalse = false);
|
||||||
public:
|
public:
|
||||||
Sink(const Level level, Logger& parent);
|
Sink(const Level level, Logger& parent);
|
||||||
void ln (const string& msg);
|
void ln (const string& msg);
|
||||||
void ln (const char* msg);
|
void ln (const char* msg);
|
||||||
|
Sink& operator<<(const char character);
|
||||||
Sink& operator<<(const string& msg);
|
Sink& operator<<(const string& msg);
|
||||||
Sink& operator<<(const char* msg);
|
Sink& operator<<(const char* msg);
|
||||||
|
Sink& operator<<(const unsigned long msg);
|
||||||
};
|
};
|
||||||
|
|
||||||
Sink debug;
|
Sink debug;
|
||||||
@@ -40,10 +44,9 @@ class Logger {
|
|||||||
Sink fatal;
|
Sink fatal;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EventBus& eventBus;
|
|
||||||
enum Level logLevel;
|
enum Level logLevel;
|
||||||
ofstream logFile;
|
ofstream* output;
|
||||||
streambuf* oldStdOut;
|
static string genTimeStamp();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+4
-6
@@ -16,18 +16,16 @@ int main(int args, char **argv) {
|
|||||||
arguments.push_back (argv[index]);
|
arguments.push_back (argv[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
EventBus eventBus;
|
Logger logger(Logger::DEBUG);
|
||||||
Logger logger(Logger::DEBUG, eventBus);
|
EventBus eventBus(logger);
|
||||||
ConfigManager cfg(logger, arguments);
|
ConfigManager cfg(logger, arguments);
|
||||||
Resolver::Server resolver(logger, eventBus);
|
Resolver::Server resolver(logger, eventBus);
|
||||||
|
|
||||||
//Record testrec = ARecord();
|
//Record testrec = ARecord();
|
||||||
|
|
||||||
logger.info << "Testing overloading!" << " with chaining!\n";
|
logger.info << "Testing overloading!" << " with chaining!" << Logger::commit;
|
||||||
logger.debug.ln ("Booting complete.");
|
logger.debug.ln ("PlaiNS successfully started.");
|
||||||
|
|
||||||
//Run eventBus on main thread.
|
|
||||||
eventBus.run();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3,4 +3,4 @@ add_library(Resolver OBJECT Server.cpp)
|
|||||||
|
|
||||||
target_include_directories(Resolver PUBLIC include)
|
target_include_directories(Resolver PUBLIC include)
|
||||||
|
|
||||||
target_link_libraries(Resolver PRIVATE asio Core)
|
target_link_libraries(Resolver PRIVATE Core PUBLIC Asio)
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
#include "Server.hpp"
|
#include "Server.hpp"
|
||||||
#include <asio.hpp>
|
|
||||||
#include <asio/ts/buffer.hpp>
|
|
||||||
#include <asio/ts/internet.hpp>
|
|
||||||
|
|
||||||
//Testing:
|
//Testing:
|
||||||
#include "Message.hpp"
|
#include "Message.hpp"
|
||||||
@@ -10,8 +7,8 @@ namespace Plains {
|
|||||||
namespace Resolver {
|
namespace Resolver {
|
||||||
|
|
||||||
Server::Server(Logger& logger, EventBus& eventBus) : logger(logger), eventBus(eventBus) {
|
Server::Server(Logger& logger, EventBus& eventBus) : logger(logger), eventBus(eventBus) {
|
||||||
// logger.debug << "Testing some stuff..." << endl;
|
logger.debug << "Testing some stuff..." << Logger::commit;
|
||||||
// logger.debug << "Message class overall size:" << sizeof(Message) << endl;
|
logger.debug << "Message class overall size:" << sizeof(Message) << Logger::commit;
|
||||||
// logger.debug << "Message::Header size:" << sizeof(Message::Header) << endl;
|
// logger.debug << "Message::Header size:" << sizeof(Message::Header) << endl;
|
||||||
// logger.debug << "Message::QR:RESPONSE Enum:" << Message::QR::RESPONSE << endl;
|
// logger.debug << "Message::QR:RESPONSE Enum:" << Message::QR::RESPONSE << endl;
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,38 @@
|
|||||||
#ifndef SERVER_HPP
|
#ifndef SERVER_HPP
|
||||||
#define SERVER_HPP
|
#define SERVER_HPP
|
||||||
|
#include "asio.hpp"
|
||||||
#include "Core.hpp"
|
#include "Core.hpp"
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
|
||||||
namespace Plains {
|
namespace Plains {
|
||||||
namespace Resolver {
|
namespace Resolver {
|
||||||
|
|
||||||
using Core::EventBus;
|
using Core::EventBus;
|
||||||
using Core::Logger;
|
using Core::Logger;
|
||||||
|
using asio::ip::udp;
|
||||||
|
|
||||||
|
|
||||||
class Server
|
class Server
|
||||||
{
|
{
|
||||||
const EventBus& eventBus;
|
private:
|
||||||
const Logger& logger;
|
EventBus& eventBus;
|
||||||
|
Logger& logger;
|
||||||
|
asio::io_context io_context;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Server(Logger& logger, EventBus& eventBus);
|
Server(Logger& logger, EventBus& eventBus);
|
||||||
~Server();
|
~Server();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}}
|
}}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user