Fully Polished the loggin system

This commit is contained in:
Anonymous275
2020-02-03 21:52:11 +02:00
parent 60a0d13e63
commit 54319f7cdd
4 changed files with 72 additions and 48 deletions

View File

@@ -1,6 +1,9 @@
#define ENET_IMPLEMENTATION
#include "enet.h"
#include <string>
#include <stdio.h>
#include "../logger.h"
void ParseData(size_t Length, enet_uint8* Data, char* Sender, enet_uint8 ChannelID); //Data Parser
void host_server(ENetHost *server) {
ENetEvent event;
@@ -49,14 +52,14 @@ void ServerMain(int Port, int MaxClients) {
/* create a server */
printf("starting server with a maximum of %d Clients...\n",MaxClients);
info("starting server with a maximum of " + to_string(MaxClients) + " Clients...\n");
server = enet_host_create(&address, MaxClients, 2, 0, 0);
if (server == NULL) {
printf("An error occurred while trying to create a server host.\n");
error("An error occurred while trying to create a server host.\n");
return;
}
printf("Waiting for clients on port %d...\n",Port);
info("Waiting for clients on port "+to_string(Port)+"...\n");
while (true) {
host_server(server);

View File

@@ -3,9 +3,10 @@
//
#include <fstream>
#include "logger.h"
void addToLog(basic_string<char> Data);
using namespace std;
int loggerlevel;
void setLoggerLevel(char level_string[]) {
@@ -72,23 +73,40 @@ stringstream getDate() {
return date;
}
/*
void info(char obj[]) {
if (level <= 2)
cout << getDate().str() << "\u001b[36m" << "[INFO]" << "\u001b[0m" << " " << obj << endl;
void info(basic_string<char> toPrint) {
if (loggerlevel <= 2){
cout << getDate().str() << "[INFO] " << toPrint << endl;
addToLog(getDate().str() + "[INFO] " + toPrint + "\n");
}
}
void error(char obj[]) {
if (level <= 4)
cout << getDate().str() << "\x1B[31m" << "[ERRO]" << "\u001b[0m" << " " << obj << endl;
void error(basic_string<char> toPrint) {
if (loggerlevel <= 4) {
cout << getDate().str() << "[ERROR] " << toPrint << endl;
addToLog(getDate().str() + "[ERROR] " + toPrint + "\n");
}
}
void warn(char obj[]) {
if (level <= 3)
cout << getDate().str() << "\u001b[33m" << "[WARN]" << "\u001b[0m" << " " << obj << endl;
void warn(basic_string<char> toPrint) {
if (loggerlevel <= 3) {
cout << getDate().str() << "[WARN] " << toPrint << endl;
addToLog(getDate().str() + "[WARN] " + toPrint + "\n");
}
}
void debug(char obj[]) {
if (level <= 1)
cout << getDate().str() << "\u001b[35m" << "[DBUG]" << "\u001b[0m" << " " << obj << endl;
}*/
void debug(basic_string<char> toPrint) {
if (loggerlevel <= 1) {
cout << getDate().str() << "[DEBUG] " << toPrint << endl;
addToLog(getDate().str() + "[DEBUG] " + toPrint + "\n");
}
}

View File

@@ -2,7 +2,6 @@
// Created by jojos38 on 28.01.2020.
//
#ifndef LOGGER_H
#define LOGGER_H
#include <iostream>
@@ -10,32 +9,10 @@
#include <sstream>
#include <string.h>
using namespace std;
extern int loggerlevel;
stringstream getDate();
void setLoggerLevel(char level_string[]);
template<typename T>
void info(const T& toPrint) {
if (loggerlevel <= 2)
cout << getDate().str() << "[INFO] " << toPrint << endl;
}
template<typename T>
void error(const T& toPrint) {
if (loggerlevel <= 4)
cout << getDate().str() "[ERRO] " << toPrint << endl;
}
template<typename T>
void warn(const T& toPrint) {
if (loggerlevel <= 3)
cout << getDate().str() << "[WARN] " << toPrint << endl;
}
template<typename T>
void debug(const T& toPrint) {
if (loggerlevel <= 1)
cout << getDate().str() << "[DBUG] " << toPrint << endl;
}
#endif // LOGGER_H
void info(basic_string<char> toPrint);
void warn(basic_string<char> toPrint);
void error(basic_string<char> toPrint);
void debug(basic_string<char> toPrint);

View File

@@ -5,15 +5,16 @@
#include "main.h"
#include <iostream>
#include <string>
#include <fstream>
#include "logger.h"
using namespace std; //nameSpace STD
void DebugData();
void LogInit();
void ParseConfig();
void ServerMain(int Port, int MaxClients);
bool Debug = false;
void addToLog(basic_string<char> Data);
int Port = 30814;
int MaxClients = 10;
string MapName = "levels/gridmap/level.json";
@@ -22,11 +23,11 @@ string ServerName = "BeamNG-MP FTW";
//Entry
int main() {
ParseConfig();
LogInit();
if(Debug){ //checks if debug is on
DebugData(); //Prints Debug Data
}
setLoggerLevel("ALL");
ServerMain(Port, MaxClients);
}
@@ -47,3 +48,28 @@ void SetMainValues(bool D, int P,int MP,string Name,string serverName){
MaxClients = MP;
}
void LogInit(){
ifstream InFileStream;
InFileStream.open("Server.log");
if(InFileStream.good()){
remove("Server.log");
}
InFileStream.close();
}
void addToLog(basic_string<char> Data){
basic_string<char> LogData = "";
ifstream InFileStream;
InFileStream.open("Server.log");
if(InFileStream.good()){
string line;
while (getline(InFileStream, line)) {
LogData = LogData + line + "\n";
}
}
ofstream LFS;
LFS.open ("Server.log");
LFS << LogData;
LFS << Data.c_str();
LFS.close();
}