Added sublogs, removed deprecated constructor
This commit is contained in:
parent
d3e7abc1d2
commit
4a820cb2b2
95
src/log.cpp
95
src/log.cpp
@ -4,6 +4,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <ios>
|
#include <ios>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
namespace gz {
|
namespace gz {
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
@ -50,35 +51,65 @@ namespace gz {
|
|||||||
|
|
||||||
|
|
||||||
#ifdef LOG_MULTITHREAD
|
#ifdef LOG_MULTITHREAD
|
||||||
|
// Static member initialization
|
||||||
std::mutex Log::mtx;
|
std::mutex Log::mtx;
|
||||||
#endif
|
#endif
|
||||||
Log::Log(std::string logfile, bool showLog, bool storeLog, std::string&& prefix_, Color prefixColor, bool showTime, Color timeColor, bool clearLogfileOnRestart, unsigned int writeAfterLines)
|
|
||||||
: iter(0),
|
Log::Log()
|
||||||
writeToFileAfterLines(writeAfterLines), clearLogfileOnRestart(clearLogfileOnRestart),
|
: showLog(true),
|
||||||
logFile(logfile), storeLog(storeLog),
|
prefixColor(Color::RESET),
|
||||||
showLog(showLog),
|
prefix("")
|
||||||
prefixColor(prefixColor), prefix(prefix_ + ": "),
|
|
||||||
showTime(showTime), timeColor(timeColor)
|
|
||||||
{
|
{
|
||||||
|
#ifdef LOG_SUBLOGS
|
||||||
|
resources = std::make_shared<LogResources>();
|
||||||
|
#endif
|
||||||
|
iter() = 0;
|
||||||
|
writeToFileAfterLines() = 100;
|
||||||
|
clearLogfileOnRestart() = false;
|
||||||
|
logFile() = "default.log";
|
||||||
|
storeLog() = false;
|
||||||
|
showTime() = false;
|
||||||
|
timeColor() = Color::RESET;
|
||||||
|
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Log::Log(LogCreateInfo&& ci)
|
Log::Log(LogCreateInfo&& ci)
|
||||||
: iter(0),
|
: showLog(ci.showLog),
|
||||||
writeToFileAfterLines(ci.writeAfterLines), clearLogfileOnRestart(ci.clearLogfileOnRestart),
|
|
||||||
logFile(ci.logfile), storeLog(ci.storeLog),
|
|
||||||
showLog(ci.showLog),
|
|
||||||
prefixColor(ci.prefixColor),
|
prefixColor(ci.prefixColor),
|
||||||
prefix(ci.prefix + ": "),
|
prefix(ci.prefix)
|
||||||
showTime(ci.showTime), timeColor(ci.timeColor)
|
|
||||||
{
|
{
|
||||||
|
#ifdef LOG_SUBLOGS
|
||||||
|
resources = std::make_shared<LogResources>();
|
||||||
|
#endif
|
||||||
|
iter() = 0;
|
||||||
|
writeToFileAfterLines() = ci.writeAfterLines;
|
||||||
|
clearLogfileOnRestart() = ci.clearLogfileOnRestart;
|
||||||
|
logFile() = ci.logfile;
|
||||||
|
storeLog() = ci.storeLog;
|
||||||
|
showTime() = ci.showTime;
|
||||||
|
timeColor() = ci.timeColor;
|
||||||
|
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef LOG_SUBLOGS
|
||||||
|
Log Log::createSublog(bool showLog, const std::string& prefix, Color prefixColor) {
|
||||||
|
return Log(std::shared_ptr<LogResources>(resources), showLog, prefix, prefixColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::Log(std::shared_ptr<LogResources>&& resources_, bool showLog, const std::string& prefix, Color prefixColor)
|
||||||
|
: resources(std::move(resources_)), showLog(showLog), prefixColor(prefixColor), prefix(prefix)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
void Log::init() {
|
void Log::init() {
|
||||||
// get absolute path to the logfile
|
// get absolute path to the logfile
|
||||||
fs::path logpath(std::move(logFile));
|
fs::path logpath(std::move(logFile()));
|
||||||
if (!logpath.is_absolute()) {
|
if (!logpath.is_absolute()) {
|
||||||
logpath = fs::current_path() / logpath;
|
logpath = fs::current_path() / logpath;
|
||||||
}
|
}
|
||||||
@ -86,30 +117,34 @@ namespace gz {
|
|||||||
if (!fs::is_directory(logpath.parent_path())) {
|
if (!fs::is_directory(logpath.parent_path())) {
|
||||||
fs::create_directory(logpath.parent_path());
|
fs::create_directory(logpath.parent_path());
|
||||||
}
|
}
|
||||||
logFile = logpath.string();
|
logFile() = logpath.string();
|
||||||
|
|
||||||
// if clearLogfileOnRestart, open the file to clear it
|
// if clearLogfileOnRestart, open the file to clear it
|
||||||
if (clearLogfileOnRestart and fs::is_regular_file(logpath)) {
|
if (clearLogfileOnRestart() and fs::is_regular_file(logpath)) {
|
||||||
std::ofstream file(logFile, std::ofstream::trunc);
|
std::ofstream file(logFile(), std::ofstream::trunc);
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (writeToFileAfterLines == 0) { writeToFileAfterLines = 1; }
|
if (writeToFileAfterLines() == 0) { writeToFileAfterLines() = 1; }
|
||||||
logLines.resize(writeToFileAfterLines);
|
logLines().resize(writeToFileAfterLines());
|
||||||
// reserve memory for strings
|
// reserve memory for strings
|
||||||
if (LOG_RESERVE_STRING_SIZE > 0) {
|
if (LOG_RESERVE_STRING_SIZE > 0) {
|
||||||
for (size_t i = 0; i < logLines.size(); i++) {
|
for (size_t i = 0; i < logLines().size(); i++) {
|
||||||
logLines[i].reserve(LOG_RESERVE_STRING_SIZE);
|
logLines()[i].reserve(LOG_RESERVE_STRING_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// reserve memory for argsBegin
|
// reserve memory for argsBegin
|
||||||
argsBegin.reserve(ARG_COUNT_RESERVE_COUNT);
|
argsBegin().reserve(ARG_COUNT_RESERVE_COUNT);
|
||||||
|
|
||||||
|
if (!prefix.empty()) {
|
||||||
|
prefix += ": ";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Log::~Log() {
|
Log::~Log() {
|
||||||
if (storeLog) { writeLog(); }
|
if (storeLog()) { writeLog(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -118,24 +153,24 @@ namespace gz {
|
|||||||
struct std::tm *tmp;
|
struct std::tm *tmp;
|
||||||
tmp = std::localtime(&t);
|
tmp = std::localtime(&t);
|
||||||
// stores the date and time in time: yyyy-mm-dd hh:mm:ss:
|
// stores the date and time in time: yyyy-mm-dd hh:mm:ss:
|
||||||
std::strftime(time, sizeof(time), "%F %T: ", tmp);
|
std::strftime(time(), LOG_TIMESTAMP_CHAR_COUNT, "%F %T: ", tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Log::writeLog() {
|
void Log::writeLog() {
|
||||||
std::ofstream file(logFile, std::ios_base::app);
|
std::ofstream file(logFile(), std::ios_base::app);
|
||||||
if (file.is_open()) {
|
if (file.is_open()) {
|
||||||
for (std::string message : logLines) {
|
for (std::string message : logLines()) {
|
||||||
file << message;
|
file << message;
|
||||||
}
|
}
|
||||||
getTime();
|
getTime();
|
||||||
std::string message = time;
|
std::string message = time();
|
||||||
message += "Written log to file: " + logFile + "\n";
|
message += "Written log to file: " + logFile() + "\n";
|
||||||
/* file << message; */
|
/* file << message; */
|
||||||
if (showLog) { std::cout << message; }
|
if (showLog) { std::cout << message; }
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
std::cout << COLORS[RED] << "LOG ERROR: " << COLORS[RESET] << "Could not open file '" << logFile << "'." << '\n';
|
std::cout << COLORS[RED] << "LOG ERROR: " << COLORS[RESET] << "Could not open file '" << logFile() << "'." << '\n';
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
201
src/log.hpp
201
src/log.hpp
@ -7,6 +7,14 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#ifndef LOG_NO_SUBLOGS
|
||||||
|
#define LOG_SUBLOGS
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef LOG_SUBLOGS
|
||||||
|
#include <memory>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef LOG_MULTITHREAD
|
#ifdef LOG_MULTITHREAD
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#endif
|
#endif
|
||||||
@ -59,6 +67,7 @@ namespace gz {
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
concept Logable = ConvertibleToString<T>;
|
concept Logable = ConvertibleToString<T>;
|
||||||
|
|
||||||
|
class Log;
|
||||||
/**
|
/**
|
||||||
* @brief Create info for a Log object
|
* @brief Create info for a Log object
|
||||||
*/
|
*/
|
||||||
@ -81,8 +90,37 @@ namespace gz {
|
|||||||
bool clearLogfileOnRestart = true;
|
bool clearLogfileOnRestart = true;
|
||||||
/// @brief Actually write the log to the logfile after so many lines. Must be at least 1
|
/// @brief Actually write the log to the logfile after so many lines. Must be at least 1
|
||||||
unsigned int writeAfterLines = 100;
|
unsigned int writeAfterLines = 100;
|
||||||
|
#ifdef LOG_SUBLOGS
|
||||||
|
std::shared_ptr<Log> parent;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifdef LOG_SUBLOGS
|
||||||
|
/**
|
||||||
|
* @brief Resources that are normally members of Log when not using `LOG_SUBLOGS`
|
||||||
|
*/
|
||||||
|
struct LogResources {
|
||||||
|
/// Where the lines are stored
|
||||||
|
std::vector<std::string> logLines;
|
||||||
|
/// Used during log: string views into the single substrings in logLines[currentLine]
|
||||||
|
std::vector<std::string::size_type> argsBegin;
|
||||||
|
/// The current position in logLines
|
||||||
|
size_t iter = 0;
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int writeToFileAfterLines;
|
||||||
|
bool clearLogfileOnRestart;
|
||||||
|
/// Absolute path to the logfile
|
||||||
|
std::string logFile;
|
||||||
|
bool storeLog;
|
||||||
|
|
||||||
|
bool showTime;
|
||||||
|
Color timeColor;
|
||||||
|
/// Stores the current time in yyyy-mm-dd hh:mm:ss format
|
||||||
|
char time[LOG_TIMESTAMP_CHAR_COUNT];
|
||||||
|
}; // class LogResources
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Manages printing messages to stdout and to logfiles.
|
* @brief Manages printing messages to stdout and to logfiles.
|
||||||
* @details
|
* @details
|
||||||
@ -95,6 +133,14 @@ namespace gz {
|
|||||||
* Log can use a static mutex for thread safety. To use this feature, you have to `#define LOG_MULTITHREAD` @b before including `log.hpp`.
|
* Log can use a static mutex for thread safety. To use this feature, you have to `#define LOG_MULTITHREAD` @b before including `log.hpp`.
|
||||||
* Note that log uses the default std::cout buffer, so you should make sure it is not being used while logging something.
|
* Note that log uses the default std::cout buffer, so you should make sure it is not being used while logging something.
|
||||||
*
|
*
|
||||||
|
* @subsection log_subs Sublogs
|
||||||
|
* If you want multiple log instances that share a logfile, you can @ref createSublog "create a sublog" from the parent.
|
||||||
|
* The sublog inherits all settings and resources from the parent, except for showLog and the prefix.
|
||||||
|
* This feature is not available by default, you need to `#define LOG_SUBLOGS`.
|
||||||
|
*
|
||||||
|
* Note that the sublog may outlive the parent, as they @ref LogResources "share their resources" with a shared_ptr.
|
||||||
|
* You can also create sublogs from sublogs.
|
||||||
|
*
|
||||||
* @subsection log_logfile Logfile
|
* @subsection log_logfile Logfile
|
||||||
* The logs can be written to a logfile, which can be specified in the constructor.
|
* The logs can be written to a logfile, which can be specified in the constructor.
|
||||||
* The logs are not continuously written to the logfile, since file operations are expensive.
|
* The logs are not continuously written to the logfile, since file operations are expensive.
|
||||||
@ -124,23 +170,26 @@ namespace gz {
|
|||||||
class Log {
|
class Log {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* @brief Creates a log object, which can print messages to stdout and/or write them to a log file
|
* @brief Create a log with default settings
|
||||||
* @details
|
* @details
|
||||||
* By creating multiple instances with different parameters, logs can be easily turned on/off for different usages.
|
* - `showLog = true`
|
||||||
*
|
* - `storeLog = false`
|
||||||
* The overload using LogCreateInfo might be more clear, so I recommend using that.
|
* - `prefix = ""`
|
||||||
* @note Colors will only be shown when written to stdout, not in the logfile.
|
* - `showTime = false`
|
||||||
* @deprecated Use the overload using the LogCreateInfo struct
|
|
||||||
*/
|
*/
|
||||||
Log(std::string logfile="log.log", bool showLog=true, bool storeLog=true, std::string&& prefix="", Color prefixColor=RESET, bool showTime=true, Color timeColor=RESET, bool clearLogfileOnRestart=true, unsigned int writeAfterLines=100);
|
Log();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Creates a log object, which can print messages to stdout and/or write them to a log file
|
* @brief Creates a log object, which can print messages to stdout and/or write them to a log file
|
||||||
* @details
|
* @details
|
||||||
* By creating multiple instances with different parameters, logs can be easily turned on/off for different usages.
|
* By creating multiple instances with different parameters, logs can be easily turned on/off for different usages.
|
||||||
*/
|
*/
|
||||||
Log(LogCreateInfo&& createInfo);
|
Log(LogCreateInfo&& createInfo);
|
||||||
|
#ifdef LOG_SUBLOGS
|
||||||
|
Log createSublog(bool showLog, const std::string& prefix, Color prefixColor);
|
||||||
|
private:
|
||||||
|
Log(std::shared_ptr<LogResources>&& resources, bool showLog, const std::string& prefix, Color prefixColor);
|
||||||
|
public:
|
||||||
|
#endif
|
||||||
~Log();
|
~Log();
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -275,40 +324,64 @@ class Log {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
|
#ifdef LOG_SUBLOGS
|
||||||
|
std::shared_ptr<LogResources> resources;
|
||||||
|
|
||||||
|
std::vector<std::string>& logLines() { return resources->logLines; };
|
||||||
|
std::vector<std::string::size_type>& argsBegin() { return resources->argsBegin; };
|
||||||
|
size_t& iter() { return resources->iter; };
|
||||||
|
|
||||||
|
unsigned int& writeToFileAfterLines() { return resources->writeToFileAfterLines; };
|
||||||
|
bool& clearLogfileOnRestart() { return resources->clearLogfileOnRestart; };
|
||||||
|
std::string& logFile() { return resources->logFile; };
|
||||||
|
bool& storeLog() { return resources->storeLog; };
|
||||||
|
|
||||||
|
bool& showTime() { return resources->showTime; };
|
||||||
|
Color& timeColor() { return resources->timeColor; };
|
||||||
|
char* time() { return resources->time; };
|
||||||
|
#else
|
||||||
/// Where the lines are stored
|
/// Where the lines are stored
|
||||||
std::vector<std::string> logLines;
|
std::vector<std::string> logLines_;
|
||||||
/// Used during log: string views into the single substrings in logLines[currentLine]
|
/// Used during log: string views into the single substrings in logLines[currentLine]
|
||||||
std::vector<std::string::size_type> argsBegin;
|
std::vector<std::string::size_type> argsBegin_;
|
||||||
/// The current position in logLines
|
/// The current position in logLines
|
||||||
size_t iter = 0;
|
size_t iter_ = 0;
|
||||||
/**
|
|
||||||
* @name Writing to file
|
|
||||||
*/
|
|
||||||
/// @{
|
|
||||||
/// When iter reaches writeToFileAfterLines, write log to file
|
/// When iter reaches writeToFileAfterLines, write log to file
|
||||||
unsigned int writeToFileAfterLines;
|
unsigned int writeToFileAfterLines_;
|
||||||
bool clearLogfileOnRestart;
|
bool clearLogfileOnRestart_;
|
||||||
/// Absolute path to the logfile
|
/// Absolute path to the logfile
|
||||||
std::string logFile;
|
std::string logFile_;
|
||||||
bool storeLog;
|
bool storeLog_;
|
||||||
|
|
||||||
|
bool showTime_;
|
||||||
|
Color timeColor_;
|
||||||
|
/// Stores the current time in yyyy-mm-dd hh:mm:ss format
|
||||||
|
char[LOG_TIMESTAMP_CHAR_COUNT] time_;
|
||||||
|
|
||||||
|
// getters
|
||||||
|
std::vector<std::string>& logLines() { return logLines_; };
|
||||||
|
std::vector<std::string::size_type>& argsBegin() { return argsBegin_; };
|
||||||
|
size_t& iter() { return iter_; };
|
||||||
|
|
||||||
|
unsigned int& writeToFileAfterLines() { return writeToFileAfterLines_; };
|
||||||
|
bool& clearLogfileOnRestart() { return clearLogfileOnRestart_; };
|
||||||
|
std::string& logFile() { return logFile_; };
|
||||||
|
bool& storeLog() { return storeLog_; };
|
||||||
|
|
||||||
|
bool& showTime() { return showTime_; };
|
||||||
|
Color& timeColor() { return timeColor_; };
|
||||||
|
std::string& time() { return time_; };
|
||||||
|
#endif
|
||||||
void writeLog();
|
void writeLog();
|
||||||
/// @}
|
|
||||||
|
|
||||||
bool showLog;
|
bool showLog;
|
||||||
Color prefixColor;
|
Color prefixColor;
|
||||||
std::string prefix;
|
std::string prefix;
|
||||||
|
|
||||||
/**
|
|
||||||
* @name Time
|
|
||||||
*/
|
|
||||||
/// @{
|
|
||||||
bool showTime;
|
|
||||||
Color timeColor;
|
|
||||||
/// Stores the current time in yyyy-mm-dd hh:mm:ss format
|
|
||||||
char time[LOG_TIMESTAMP_CHAR_COUNT];
|
|
||||||
/// Store the current time in yyyy-mm-dd hh:mm:ss format in time member
|
/// Store the current time in yyyy-mm-dd hh:mm:ss format in time member
|
||||||
void getTime();
|
void getTime();
|
||||||
/// @}
|
|
||||||
|
|
||||||
#ifdef LOG_MULTITHREAD
|
#ifdef LOG_MULTITHREAD
|
||||||
/// Lock for std::cout
|
/// Lock for std::cout
|
||||||
@ -325,31 +398,31 @@ class Log {
|
|||||||
#ifdef LOG_MULTITHREAD
|
#ifdef LOG_MULTITHREAD
|
||||||
mtx.lock();
|
mtx.lock();
|
||||||
#endif
|
#endif
|
||||||
argsBegin.clear();
|
argsBegin().clear();
|
||||||
if (showTime) {
|
if (showTime()) {
|
||||||
getTime();
|
getTime();
|
||||||
logLines[iter] = time;
|
logLines()[iter()] = time();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
logLines.clear();
|
logLines().clear();
|
||||||
}
|
}
|
||||||
argsBegin.emplace_back(logLines[iter].size());
|
argsBegin().emplace_back(logLines()[iter()].size());
|
||||||
logLines[iter] += prefix;
|
logLines()[iter()] += prefix;
|
||||||
|
|
||||||
vlog(" ", std::forward<Args>(args)...);
|
vlog(" ", std::forward<Args>(args)...);
|
||||||
logLines[iter] += "\n";
|
logLines()[iter()] += "\n";
|
||||||
argsBegin.emplace_back(logLines[iter].size());
|
argsBegin().emplace_back(logLines()[iter()].size());
|
||||||
|
|
||||||
if (showLog) {
|
if (showLog) {
|
||||||
// time
|
// time
|
||||||
std::cout << COLORS[timeColor] << std::string_view(logLines[iter].begin(), logLines[iter].begin() + argsBegin[0])
|
std::cout << COLORS[timeColor()] << std::string_view(logLines()[iter()].begin(), logLines()[iter()].begin() + argsBegin()[0])
|
||||||
// prefix
|
// prefix
|
||||||
<< COLORS[prefixColor] << std::string_view(logLines[iter].begin() + argsBegin[0], logLines[iter].begin() + argsBegin[1]) << COLORS[RESET]
|
<< COLORS[prefixColor] << std::string_view(logLines()[iter()].begin() + argsBegin()[0], logLines()[iter()].begin() + argsBegin()[1]) << COLORS[RESET]
|
||||||
// message
|
// message
|
||||||
<< std::string_view(logLines[iter].begin() + argsBegin[1], logLines[iter].end());
|
<< std::string_view(logLines()[iter()].begin() + argsBegin()[1], logLines()[iter()].end());
|
||||||
}
|
}
|
||||||
if (++iter >= writeToFileAfterLines) {
|
if (++iter() >= writeToFileAfterLines()) {
|
||||||
iter = 0;
|
iter() = 0;
|
||||||
writeLog();
|
writeLog();
|
||||||
}
|
}
|
||||||
#ifdef LOG_MULTITHREAD
|
#ifdef LOG_MULTITHREAD
|
||||||
@ -363,36 +436,36 @@ class Log {
|
|||||||
#ifdef LOG_MULTITHREAD
|
#ifdef LOG_MULTITHREAD
|
||||||
mtx.lock();
|
mtx.lock();
|
||||||
#endif
|
#endif
|
||||||
argsBegin.clear();
|
argsBegin().clear();
|
||||||
if (showTime) {
|
if (showTime()) {
|
||||||
getTime();
|
getTime();
|
||||||
logLines[iter] = std::string(time);
|
logLines()[iter()] = std::string(time());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
logLines.clear();
|
logLines().clear();
|
||||||
}
|
}
|
||||||
argsBegin.emplace_back(logLines[iter].size());
|
argsBegin().emplace_back(logLines()[iter()].size());
|
||||||
logLines[iter] += prefix;
|
logLines()[iter()] += prefix;
|
||||||
|
|
||||||
vlog(" ", std::forward<Args>(args)...);
|
vlog(" ", std::forward<Args>(args)...);
|
||||||
logLines[iter] += "\n";
|
logLines()[iter()] += "\n";
|
||||||
argsBegin.emplace_back(logLines[iter].size());
|
argsBegin().emplace_back(logLines()[iter()].size());
|
||||||
|
|
||||||
if (showLog) {
|
if (showLog) {
|
||||||
// time
|
// time
|
||||||
std::cout << COLORS[timeColor] << std::string_view(logLines[iter].begin(), logLines[iter].begin() + argsBegin[0])
|
std::cout << COLORS[timeColor()] << std::string_view(logLines()[iter()].begin(), logLines()[iter()].begin() + argsBegin()[0])
|
||||||
// prefix
|
// prefix
|
||||||
<< COLORS[prefixColor] << std::string_view(logLines[iter].begin() + argsBegin[0], logLines[iter].begin() + argsBegin[1]) << COLORS[RESET];
|
<< COLORS[prefixColor] << std::string_view(logLines()[iter()].begin() + argsBegin()[0], logLines()[iter()].begin() + argsBegin()[1]) << COLORS[RESET];
|
||||||
// max index where i can be used for colors and i+2 can be used for currentViews
|
// max index where i can be used for colors and i+2 can be used for currentViews
|
||||||
size_t maxI = std::min(colors.size(), argsBegin.size() - 2);
|
size_t maxI = std::min(colors.size(), argsBegin().size() - 2);
|
||||||
for (size_t i = 0; i < maxI; i++) {
|
for (size_t i = 0; i < maxI; i++) {
|
||||||
std::cout << COLORS[colors[i]] << std::string_view(logLines[iter].begin() + argsBegin[i+1], logLines[iter].begin() + argsBegin[i+2]);
|
std::cout << COLORS[colors[i]] << std::string_view(logLines()[iter()].begin() + argsBegin()[i+1], logLines()[iter()].begin() + argsBegin()[i+2]);
|
||||||
}
|
}
|
||||||
// log the rest, maxI is now <= argsBegin.size() - 2
|
// log the rest, maxI is now <= argsBegin.size() - 2
|
||||||
std::cout << std::string_view(logLines[iter].begin() + argsBegin[maxI+1], logLines[iter].end()) << COLORS[RESET];
|
std::cout << std::string_view(logLines()[iter()].begin() + argsBegin()[maxI+1], logLines()[iter()].end()) << COLORS[RESET];
|
||||||
}
|
}
|
||||||
if (++iter >= writeToFileAfterLines) {
|
if (++iter() >= writeToFileAfterLines()) {
|
||||||
iter = 0;
|
iter() = 0;
|
||||||
writeLog();
|
writeLog();
|
||||||
}
|
}
|
||||||
#ifdef LOG_MULTITHREAD
|
#ifdef LOG_MULTITHREAD
|
||||||
@ -403,17 +476,17 @@ class Log {
|
|||||||
|
|
||||||
template<util::Stringy T, Logable... Args>
|
template<util::Stringy T, Logable... Args>
|
||||||
void Log::vlog(const char* appendChars, T&& t, Args&&... args) {
|
void Log::vlog(const char* appendChars, T&& t, Args&&... args) {
|
||||||
argsBegin.emplace_back(logLines[iter].size());
|
argsBegin().emplace_back(logLines()[iter()].size());
|
||||||
logLines[iter] += std::string(t);
|
logLines()[iter()] += std::string(t);
|
||||||
logLines[iter] += appendChars;
|
logLines()[iter()] += appendChars;
|
||||||
vlog(" ", std::forward<Args>(args)...);
|
vlog(" ", std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
/// Log anything where toString exists
|
/// Log anything where toString exists
|
||||||
template<ConvertibleToString T, Logable... Args>
|
template<ConvertibleToString T, Logable... Args>
|
||||||
void Log::vlog(const char* appendChars, T&& t, Args&&... args) requires (!util::Stringy<T>) {
|
void Log::vlog(const char* appendChars, T&& t, Args&&... args) requires (!util::Stringy<T>) {
|
||||||
argsBegin.emplace_back(logLines[iter].size());
|
argsBegin().emplace_back(logLines()[iter()].size());
|
||||||
logLines[iter] += toString(t);
|
logLines()[iter()] += toString(t);
|
||||||
logLines[iter] += appendChars;
|
logLines()[iter()] += appendChars;
|
||||||
vlog(" ", std::forward<Args>(args)...);
|
vlog(" ", std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user