From c0b68e9c33c9d5e10b20c0a0d6be27241d305458 Mon Sep 17 00:00:00 2001 From: Nicholas Chan Date: Mon, 9 Mar 2020 11:58:21 -0400 Subject: [PATCH] Logger changes --- bfs/include/Logger.hpp | 33 +++++++++++++++++++++++++++++++++ bfs/src/logging/Logger.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 bfs/include/Logger.hpp create mode 100644 bfs/src/logging/Logger.cpp diff --git a/bfs/include/Logger.hpp b/bfs/include/Logger.hpp new file mode 100644 index 0000000..60ea5b7 --- /dev/null +++ b/bfs/include/Logger.hpp @@ -0,0 +1,33 @@ +#ifndef LOGGER_HPP +#define LOGGER_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + + +extern class Logger { +public: + ofstream logFile; + string filename; + string filePath; + Logger(int l_Level); + ~Logger(); + void writeLog(string msg, int msg_level); + static Logger* getLogger(Logger* logger); +private: + int logging_Level; + //auto now; + +}; + + +#endif diff --git a/bfs/src/logging/Logger.cpp b/bfs/src/logging/Logger.cpp new file mode 100644 index 0000000..ad4e261 --- /dev/null +++ b/bfs/src/logging/Logger.cpp @@ -0,0 +1,32 @@ +#include "Logger.hpp" + +Logger::Logger(int l_Level) { + this->logging_Level = l_Level; + time_t t = time(0); // get time now + struct tm * now = localtime( & t ); + char buffer [80]; + strftime (buffer,80,"%Y-%m-%d %H:%M:%S",now); + std::string str(buffer); + this->filename = str + ".log"; + //cout << filename << "\n"; + //this->filename = "log.txt"; +} + +void Logger::writeLog(string msg, int msg_level) { + string line; + this->logFile.open(this->filename, ios::in); + if (this->logging_Level <= msg_level) { + auto now = chrono::system_clock::to_time_t(chrono::system_clock::now()); + this->logFile << ctime(&now) << " " << msg << endl; + + } + this->logFile.close(); +} + +Logger* Logger::getLogger(Logger* logger) { + if (logger == NULL) { + logger = new Logger(1); + + } + return logger; +} \ No newline at end of file