update// added filesystem and reformated addressbook

This commit is contained in:
Strider
2020-05-17 13:18:19 +02:00
parent 57a5df8ae7
commit 4f584ac86e
22 changed files with 615 additions and 8 deletions

11
src/Logger/LogContext.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef LOGCONTEXT_H
#define LOGCONTEXT_H
#include <string>
class LogContext
{
public:
virtual void log(std::string message) {};
};
#endif

18
src/Logger/LogCrtitical.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef LOGCRITICAL_H
#define LOGCRITICAL_H
#include "LogType.h"
#include "LogStrategy.h"
#include "LogWriter.h"
class LogCritical : public LogStrategy
{
public:
void log(std::string message)
{
LogWriter* lw = LogWriter::getInstance();
lw->write(LogType::CRITICAL, message);
}
};
#endif

18
src/Logger/LogDebug.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef LOGDEBUG_H
#define LOGDEBUG_H
#include "LogType.h"
#include "LogStrategy.h"
#include "LogWriter.h"
class LogDebug : public LogStrategy
{
public:
void log(std::string message)
{
LogWriter* lw = LogWriter::getInstance();
lw->write(LogType::DEBUG, message);
}
};
#endif

18
src/Logger/LogError.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef LOGERROR_H
#define LOGERROR_H
#include "LogType.h"
#include "LogStrategy.h"
#include "LogWriter.h"
class LogError : public LogStrategy
{
public:
void log(std::string message)
{
LogWriter* lw = LogWriter::getInstance();
lw->write(LogType::ERROR, message);
}
};
#endif

18
src/Logger/LogFatal.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef LOGFATAL_H
#define LOGFATAL_H
#include "LogType.h"
#include "LogStrategy.h"
#include "LogWriter.h"
class LogFatal : public LogStrategy
{
public:
void log(std::string message)
{
LogWriter* lw = LogWriter::getInstance();
lw->write(LogType::FATAL, message);
}
};
#endif

18
src/Logger/LogInfo.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef LOGINFO_H
#define LOGINFO_H
#include "LogType.h"
#include "LogStrategy.h"
#include "LogWriter.h"
class LogInfo : public LogStrategy
{
public:
void log(std::string message)
{
LogWriter* lw = LogWriter::getInstance();
lw->write(LogType::INFO, message);
}
};
#endif

11
src/Logger/LogStrategy.h Normal file
View File

@@ -0,0 +1,11 @@
#ifndef LOGSTRATEGY_H
#define LOGSTRATEGY_H
#include <string>
class LogStrategy
{
public:
virtual void log(std::string message) {};
};
#endif

18
src/Logger/LogSuccess.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef LOGSUCCESS_H
#define LOGSUCCESS_H
#include "LogType.h"
#include "LogStrategy.h"
#include "LogWriter.h"
class LogSuccess : public LogStrategy
{
public:
void log(std::string message)
{
LogWriter* lw = LogWriter::getInstance();
lw->write(LogType::SUCCESS, message);
}
};
#endif

47
src/Logger/LogType.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef LOGTYPE_H
#define LOGTYPE_H
#include <string>
class LogType
{
public:
enum TYPE {
INFO = 0,
DEBUG = 1,
SUCCESS = 2,
WARNING = 3,
ERROR = 4,
FATAL = 5,
CRITICAL = 6
};
static std::string enum2String(int type)
{
switch (type)
{
default:
case 0:
return "INFO";
case 1:
return "DEBUG";
case 2:
return "SUCCESS";
case 3:
return "WARNING";
case 4:
return "ERROR";
case 5:
return "FATAL";
case 6:
return "CRITICAL";
}
}
};
#endif

18
src/Logger/LogWarning.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef LOGWARNING_H
#define LOGWARNING_H
#include "LogType.h"
#include "LogStrategy.h"
#include "LogWriter.h"
class LogWarning : public LogStrategy
{
public:
void log(std::string message)
{
LogWriter* lw = LogWriter::getInstance();
lw->write(LogType::WARNING, message);
}
};
#endif

35
src/Logger/LogWriter.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include "LogWriter.h"
LogWriter* LogWriter::getInstance()
{
if(instance == nullptr)
instance = new LogWriter();
return instance;
}
void LogWriter::setLogFile(std::string file)
{
this->logfile = file;
}
void LogWriter::write(LogType::TYPE type, std::string message)
{
std::ofstream writer(this->logfile, std::ios::out | std::ios::app);
if(writer.good())
{
time_t now = time(0);
tm *ltm = localtime(&now);
std::stringstream ss;
ss << "[" << LogType::enum2String(type) << "] " <<
ltm->tm_mon << "-" <<
ltm->tm_mday << "-" <<
(1900 + ltm->tm_year) << " " <<
ltm->tm_hour << ":" <<
ltm->tm_min << ":" <<
ltm->tm_sec << " > " << message;
writer << ss.str() << "\n";
}
writer.close();
}

22
src/Logger/LogWriter.h Normal file
View File

@@ -0,0 +1,22 @@
#ifndef LOGWRITER_H
#define LOGWRITER_H
#include <string>
#include <fstream>
#include <sstream>
#include <ctime>
#include "LogType.h"
class LogWriter
{
public:
static LogWriter* getInstance();
std::string logfile = "";
void setLogFile(std::string file);
void write(LogType::TYPE t, std::string message);
private:
static LogWriter* instance;
};
#endif

25
src/Logger/Logger.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef LOGGER_H
#define LOGGER_H
#include "LogContext.h"
#include "LogStrategy.h"
#include "LogWriter.h"
class Logger : LogContext
{
private:
LogStrategy * strategy = nullptr;
public:
Logger(LogStrategy * strategy)
{
this->strategy = strategy;
}
void log(std::string message)
{
this->strategy->log(message);
}
};
LogWriter* LogWriter::instance = nullptr;
#endif

84
src/Logger/SimpleLogger.h Normal file
View File

@@ -0,0 +1,84 @@
#ifndef SIMPLELOGGER_H
#define SIMPLELOGGER_H
#include "Logger.h"
#include "LogInfo.h"
#include "LogDebug.h"
#include "LogSuccess.h"
#include "LogWarning.h"
#include "LogError.h"
#include "LogFatal.h"
#include "LogCrtitical.h"
#include "LogWriter.h"
class SimpleLogger
{
public:
SimpleLogger()
{
LogWriter::getInstance()->setLogFile("log.txt");
}
SimpleLogger(std::string logFile)
{
LogWriter::getInstance()->setLogFile(logFile);
}
void logInfo(std::string message)
{
Logger* logger = nullptr;
LogStrategy* li = new LogInfo();
logger = new Logger(li);
logger->log(message);
}
void logDebug(std::string message)
{
Logger* logger = nullptr;
LogStrategy* li = new LogDebug();
logger = new Logger(li);
logger->log(message);
}
void logSuccess(std::string message)
{
Logger* logger = nullptr;
LogStrategy* li = new LogSuccess();
logger = new Logger(li);
logger->log(message);
}
void logWarning(std::string message)
{
Logger* logger = nullptr;
LogStrategy* li = new LogWarning();
logger = new Logger(li);
logger->log(message);
}
void logError(std::string message)
{
Logger* logger = nullptr;
LogStrategy* li = new LogError();
logger = new Logger(li);
logger->log(message);
}
void logFatal(std::string message)
{
Logger* logger = nullptr;
LogStrategy* li = new LogFatal();
logger = new Logger(li);
logger->log(message);
}
void logCritical(std::string message)
{
Logger* logger = nullptr;
LogStrategy* li = new LogCritical();
logger = new Logger(li);
logger->log(message);
}
};
#endif

14
src/Logger/test.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include "SimpleLogger.h"
int main(int argc, char** argv)
{
SimpleLogger sl = SimpleLogger("/tmp/simplelog.log");
sl.logInfo("test info");
sl.logDebug("test debug");
sl.logSuccess("test success");
sl.logWarning("test warning");
sl.logError("test error");
sl.logFatal("test fatal");
sl.logCritical("test critical");
return 0;
}