2024-06-10 19:50:12 +02:00
|
|
|
#include <filesystem>
|
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
2024-06-12 19:03:41 +02:00
|
|
|
#include <renderd7/Logger.hpp>
|
2024-06-10 19:50:12 +02:00
|
|
|
#include <renderd7/Time.hpp>
|
|
|
|
#include <renderd7/renderd7.hpp>
|
|
|
|
|
|
|
|
namespace RenderD7 {
|
|
|
|
LoggerBase::~LoggerBase() {
|
|
|
|
if (_log.is_open()) _log.close();
|
|
|
|
}
|
|
|
|
void LoggerBase::Init(const std::string& name, bool fileless) {
|
|
|
|
if (!fileless) {
|
|
|
|
std::string path_base = RenderD7::GetAppDirectory() + "/logs/";
|
|
|
|
if (!std::filesystem::is_directory(path_base)) {
|
|
|
|
std::filesystem::create_directories(path_base);
|
|
|
|
}
|
|
|
|
auto ts = RenderD7::GetTimeStr();
|
|
|
|
std::string fn = name + ts + ".txt";
|
|
|
|
this->filename = name;
|
|
|
|
this->log_path = path_base + name;
|
|
|
|
if (std::filesystem::exists(this->log_path)) {
|
|
|
|
// Do nothing
|
|
|
|
} else {
|
|
|
|
_log.open(this->log_path, std::ios::out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->Write("RenderD7 Log\n\n");
|
|
|
|
}
|
|
|
|
|
2024-06-12 19:03:41 +02:00
|
|
|
void LoggerBase::Write(const std::string& debug_text, int lvl) {
|
2024-06-10 19:50:12 +02:00
|
|
|
std::string msg = "[" + RenderD7::GetTimeStr() + "]: " + debug_text;
|
2024-06-12 19:03:41 +02:00
|
|
|
if (this->_log.is_open() && lvl <= writelvl) {
|
2024-06-10 19:50:12 +02:00
|
|
|
this->_log << msg << std::endl;
|
|
|
|
}
|
2024-06-15 15:12:06 +02:00
|
|
|
/*while (msg.find_first_of('\n') != 0) {
|
2024-06-10 19:50:12 +02:00
|
|
|
lines.push_back(msg.substr(0, msg.find_first_of('\n')));
|
|
|
|
msg = msg.substr(msg.find_first_of('\n'));
|
2024-06-15 15:12:06 +02:00
|
|
|
}*/
|
2024-06-10 19:50:12 +02:00
|
|
|
lines.push_back(msg);
|
|
|
|
}
|
2024-06-12 19:03:41 +02:00
|
|
|
|
|
|
|
const std::vector<std::string>& LoggerBase::Lines() { return this->lines; }
|
2024-06-10 19:50:12 +02:00
|
|
|
} // namespace RenderD7
|