Console logging
parent
4fbeb5be72
commit
51a1468cf0
|
@ -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;
|
||||
|
|
|
@ -7,9 +7,11 @@
|
|||
#include <blt/std/time.h>
|
||||
#include <cstdarg>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "blt/std/string.h"
|
||||
#include <fstream>
|
||||
#include <utility>
|
||||
#include <ios>
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -55,13 +97,17 @@ namespace blt::logging {
|
|||
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, ...) {
|
||||
|
|
Loading…
Reference in New Issue