Added Logger
This commit is contained in:
parent
addf3ea73e
commit
80b7a0109c
@ -25,6 +25,7 @@ SOURCES += \
|
|||||||
sim/WindModel.cpp \
|
sim/WindModel.cpp \
|
||||||
utils/BinMap.cpp \
|
utils/BinMap.cpp \
|
||||||
utils/CurlConnection.cpp \
|
utils/CurlConnection.cpp \
|
||||||
|
utils/Logger.cpp \
|
||||||
utils/ThrustCurveAPI.cpp \
|
utils/ThrustCurveAPI.cpp \
|
||||||
utils/math/Quaternion.cpp \
|
utils/math/Quaternion.cpp \
|
||||||
utils/math/Vector3.cpp
|
utils/math/Vector3.cpp
|
||||||
@ -47,6 +48,7 @@ HEADERS += \
|
|||||||
sim/WindModel.h \
|
sim/WindModel.h \
|
||||||
utils/BinMap.h \
|
utils/BinMap.h \
|
||||||
utils/CurlConnection.h \
|
utils/CurlConnection.h \
|
||||||
|
utils/Logger.h \
|
||||||
utils/ThrustCurveAPI.h \
|
utils/ThrustCurveAPI.h \
|
||||||
utils/math/Constants.h \
|
utils/math/Constants.h \
|
||||||
utils/math/Quaternion.h \
|
utils/math/Quaternion.h \
|
||||||
|
83
utils/Logger.cpp
Normal file
83
utils/Logger.cpp
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace utils
|
||||||
|
{
|
||||||
|
Logger* Logger::instance = nullptr;
|
||||||
|
|
||||||
|
Logger* Logger::getInstance()
|
||||||
|
{
|
||||||
|
if(!instance)
|
||||||
|
{
|
||||||
|
instance = new Logger();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::Logger()
|
||||||
|
{
|
||||||
|
outFile.open("log.txt");
|
||||||
|
/*
|
||||||
|
error = [this](std::string_view msg) { log(msg, ERROR); };
|
||||||
|
warn = [this](std::string_view msg) { log(msg, WARN); };
|
||||||
|
info = [this](std::string_view msg) { log(msg, INFO); };
|
||||||
|
debug = [this](std::string_view msg) { log(msg, DEBUG); };
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::~Logger()
|
||||||
|
{
|
||||||
|
outFile.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::log(std::string_view msg, const LogLevel& lvl)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lck(mtx);
|
||||||
|
// The fallthrough is intentional. Logging is automatically enabled for
|
||||||
|
// all levels at or lower than the current level.
|
||||||
|
switch(currentLevel)
|
||||||
|
{
|
||||||
|
case DEBUG:
|
||||||
|
if(lvl == DEBUG)
|
||||||
|
{
|
||||||
|
outFile << "[DEBUG] " << msg << std::endl;
|
||||||
|
std::cout << "[DEBUG] " << msg << std::endl;
|
||||||
|
}
|
||||||
|
[[fallthrough]];
|
||||||
|
case INFO:
|
||||||
|
if(lvl == INFO)
|
||||||
|
{
|
||||||
|
outFile << "[INFO] " << msg << std::endl;
|
||||||
|
std::cout << "[INFO] " << msg << std::endl;
|
||||||
|
}
|
||||||
|
[[fallthrough]];
|
||||||
|
case WARN:
|
||||||
|
if(lvl == WARN)
|
||||||
|
{
|
||||||
|
outFile << "[WARN] " << msg << std::endl;
|
||||||
|
std::cout << "[WARN] " << msg << std::endl;
|
||||||
|
}
|
||||||
|
[[fallthrough]];
|
||||||
|
default:
|
||||||
|
if(lvl == ERROR)
|
||||||
|
{
|
||||||
|
outFile << "[ERROR] " << msg << std::endl;
|
||||||
|
std::cout << "[ERROR] " << msg << std::endl;
|
||||||
|
std::cerr << "[ERROR] " << msg << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::setLogLevel(const LogLevel& lvl)
|
||||||
|
{
|
||||||
|
currentLevel = lvl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::log(std::ostream& o, const std::string& msg)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lck(mtx);
|
||||||
|
o << msg << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace utils
|
61
utils/Logger.h
Normal file
61
utils/Logger.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#ifndef UTILS_LOGGER_H
|
||||||
|
#define UTILS_LOGGER_H
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string_view>
|
||||||
|
//#include <functional>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo write docs
|
||||||
|
*/
|
||||||
|
namespace utils
|
||||||
|
{
|
||||||
|
|
||||||
|
class Logger
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum LogLevel
|
||||||
|
{
|
||||||
|
ERROR,
|
||||||
|
WARN,
|
||||||
|
INFO,
|
||||||
|
DEBUG
|
||||||
|
};
|
||||||
|
|
||||||
|
static Logger* getInstance();
|
||||||
|
|
||||||
|
Logger(Logger const&) = delete;
|
||||||
|
Logger(Logger&&) = delete;
|
||||||
|
Logger& operator=(Logger const&) = delete;
|
||||||
|
Logger& operator=(Logger&&) = delete;
|
||||||
|
|
||||||
|
void setLogLevel(const LogLevel& lvl);
|
||||||
|
|
||||||
|
/*
|
||||||
|
std::function<void(std::string_view)> error;
|
||||||
|
std::function<void(std::string_view)> warn;
|
||||||
|
std::function<void(std::string_view)> info;
|
||||||
|
std::function<void(std::string_view)> debug;
|
||||||
|
*/
|
||||||
|
inline void error(std::string_view msg) { log(msg, ERROR); }
|
||||||
|
inline void warn(std::string_view msg) { log(msg, WARN); }
|
||||||
|
inline void info(std::string_view msg) { log(msg, INFO); }
|
||||||
|
inline void debug(std::string_view msg) { log(msg, DEBUG); }
|
||||||
|
|
||||||
|
void log(std::ostream& o, const std::string& msg);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void log(std::string_view msg, const LogLevel& lvl);
|
||||||
|
|
||||||
|
LogLevel currentLevel;
|
||||||
|
std::ofstream outFile;
|
||||||
|
static Logger* instance;
|
||||||
|
std::mutex mtx;
|
||||||
|
Logger();
|
||||||
|
~Logger();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace utils
|
||||||
|
#endif // UTILS_LOGGER_H
|
Loading…
x
Reference in New Issue
Block a user