Console logging
parent
4fbeb5be72
commit
51a1468cf0
|
@ -111,7 +111,7 @@ namespace blt::string {
|
||||||
s.erase(
|
s.erase(
|
||||||
s.begin(), std::find_if(
|
s.begin(), std::find_if(
|
||||||
s.begin(), s.end(), [](unsigned char ch) {
|
s.begin(), s.end(), [](unsigned char ch) {
|
||||||
return !std::isspace(ch);
|
return !std::isblank(ch);
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
return s;
|
return s;
|
||||||
|
@ -122,7 +122,7 @@ namespace blt::string {
|
||||||
s.erase(
|
s.erase(
|
||||||
std::find_if(
|
std::find_if(
|
||||||
s.rbegin(), s.rend(), [](unsigned char ch) {
|
s.rbegin(), s.rend(), [](unsigned char ch) {
|
||||||
return !std::isspace(ch);
|
return !std::isblank(ch);
|
||||||
}
|
}
|
||||||
).base(), s.end());
|
).base(), s.end());
|
||||||
return s;
|
return s;
|
||||||
|
|
|
@ -7,9 +7,11 @@
|
||||||
#include <blt/std/time.h>
|
#include <blt/std/time.h>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "blt/std/string.h"
|
#include "blt/std/string.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <utility>
|
||||||
|
#include <ios>
|
||||||
|
|
||||||
// https://en.cppreference.com/w/cpp/utility/variadic
|
// https://en.cppreference.com/w/cpp/utility/variadic
|
||||||
// https://medium.com/swlh/variadic-functions-3419c287a0d2
|
// https://medium.com/swlh/variadic-functions-3419c287a0d2
|
||||||
|
@ -19,6 +21,43 @@
|
||||||
|
|
||||||
namespace blt::logging {
|
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){
|
void applyFormatting(const std::string& format, std::string& output, va_list& args){
|
||||||
char formattedChars[format.length()];
|
char formattedChars[format.length()];
|
||||||
vsprintf(formattedChars, format.c_str(), args);
|
vsprintf(formattedChars, format.c_str(), args);
|
||||||
|
@ -45,8 +84,11 @@ namespace blt::logging {
|
||||||
|
|
||||||
// by default everything is enabled
|
// by default everything is enabled
|
||||||
LOG_PROPERTIES BLT_LOGGING_PROPERTIES{true, true, true, "./"};
|
LOG_PROPERTIES BLT_LOGGING_PROPERTIES{true, true, true, "./"};
|
||||||
|
LogFileWriter writer{"./"};
|
||||||
|
|
||||||
void init(LOG_PROPERTIES properties) {
|
void init(LOG_PROPERTIES properties) {
|
||||||
|
if (BLT_LOGGING_PROPERTIES.m_directory != properties.m_directory)
|
||||||
|
writer = LogFileWriter{properties.m_directory};
|
||||||
BLT_LOGGING_PROPERTIES = properties;
|
BLT_LOGGING_PROPERTIES = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,14 +96,18 @@ namespace blt::logging {
|
||||||
std::string outputString = System::getTimeStringLog();
|
std::string outputString = System::getTimeStringLog();
|
||||||
outputString += levelNames[level];
|
outputString += levelNames[level];
|
||||||
outputString += str;
|
outputString += str;
|
||||||
|
|
||||||
|
std::string fileString = outputString;
|
||||||
|
|
||||||
if (BLT_LOGGING_PROPERTIES.m_useColor) {
|
if (BLT_LOGGING_PROPERTIES.m_useColor) {
|
||||||
outputString = levelColors[level] + outputString;
|
outputString = levelColors[level] + outputString;
|
||||||
outputString += "\033[0m";
|
outputString += "\033[0m";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasEndingLinefeed || auto_line)
|
if (hasEndingLinefeed || auto_line) {
|
||||||
outputString += "\n";
|
outputString += "\n";
|
||||||
|
fileString += "\n";
|
||||||
|
}
|
||||||
|
|
||||||
if (BLT_LOGGING_PROPERTIES.m_logToConsole) {
|
if (BLT_LOGGING_PROPERTIES.m_logToConsole) {
|
||||||
if (level > WARN)
|
if (level > WARN)
|
||||||
|
@ -69,6 +115,10 @@ namespace blt::logging {
|
||||||
else
|
else
|
||||||
std::cout << outputString;
|
std::cout << outputString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (BLT_LOGGING_PROPERTIES.m_logToFile) {
|
||||||
|
writer.writeLine(fileString);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void log(const std::string& format, LOG_LEVEL level, int auto_line, ...) {
|
void log(const std::string& format, LOG_LEVEL level, int auto_line, ...) {
|
||||||
|
|
Loading…
Reference in New Issue