From 51a1468cf051d876c777ce2c08fe34b71e619455 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 24 Jan 2023 17:56:48 -0500 Subject: [PATCH] Console logging --- include/blt/std/string.h | 4 +-- src/blt/std/logging.cpp | 54 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/include/blt/std/string.h b/include/blt/std/string.h index 7961dc7..0ebff9e 100644 --- a/include/blt/std/string.h +++ b/include/blt/std/string.h @@ -111,7 +111,7 @@ namespace blt::string { s.erase( s.begin(), std::find_if( s.begin(), s.end(), [](unsigned char ch) { - return !std::isspace(ch); + return !std::isblank(ch); } )); return s; @@ -122,7 +122,7 @@ namespace blt::string { s.erase( std::find_if( s.rbegin(), s.rend(), [](unsigned char ch) { - return !std::isspace(ch); + return !std::isblank(ch); } ).base(), s.end()); return s; diff --git a/src/blt/std/logging.cpp b/src/blt/std/logging.cpp index e7974b8..b0ad60b 100644 --- a/src/blt/std/logging.cpp +++ b/src/blt/std/logging.cpp @@ -7,9 +7,11 @@ #include #include #include -#include #include #include "blt/std/string.h" +#include +#include +#include // https://en.cppreference.com/w/cpp/utility/variadic // https://medium.com/swlh/variadic-functions-3419c287a0d2 @@ -19,6 +21,43 @@ namespace blt::logging { + class LogFileWriter { + private: + std::string m_path; + std::fstream* output; + int currentLines = 0; + static constexpr int MAX_LINES = 100000; + public: + explicit LogFileWriter(const std::string& path): m_path(path){ + auto currentTime = System::getTimeStringFS(); + output = new std::fstream(path + currentTime + ".log", std::ios::out | std::ios::app); + if (!output->good()){ + throw std::runtime_error("Unable to open console filestream!\n"); + } + } + + void writeLine(const std::string& line){ + if (!output->good()){ + std::cerr << "There has been an error in the logging file stream!\n"; + output->clear(); + } + *output << line; + currentLines++; + if (currentLines > MAX_LINES){ + output->flush(); + output->close(); + delete(output); + currentLines = 0; + auto currentTime = System::getTimeStringFS(); + output = new std::fstream(m_path + currentTime + ".log"); + } + } + + ~LogFileWriter() { + delete(output); + } + }; + void applyFormatting(const std::string& format, std::string& output, va_list& args){ char formattedChars[format.length()]; vsprintf(formattedChars, format.c_str(), args); @@ -45,8 +84,11 @@ namespace blt::logging { // by default everything is enabled LOG_PROPERTIES BLT_LOGGING_PROPERTIES{true, true, true, "./"}; + LogFileWriter writer{"./"}; void init(LOG_PROPERTIES properties) { + if (BLT_LOGGING_PROPERTIES.m_directory != properties.m_directory) + writer = LogFileWriter{properties.m_directory}; BLT_LOGGING_PROPERTIES = properties; } @@ -54,14 +96,18 @@ namespace blt::logging { std::string outputString = System::getTimeStringLog(); outputString += levelNames[level]; outputString += str; + + std::string fileString = outputString; if (BLT_LOGGING_PROPERTIES.m_useColor) { outputString = levelColors[level] + outputString; outputString += "\033[0m"; } - if (hasEndingLinefeed || auto_line) + if (hasEndingLinefeed || auto_line) { outputString += "\n"; + fileString += "\n"; + } if (BLT_LOGGING_PROPERTIES.m_logToConsole) { if (level > WARN) @@ -69,6 +115,10 @@ namespace blt::logging { else std::cout << outputString; } + + if (BLT_LOGGING_PROPERTIES.m_logToFile) { + writer.writeLine(fileString); + } } void log(const std::string& format, LOG_LEVEL level, int auto_line, ...) {