Fixed bugs

This commit is contained in:
matthias@arch 2022-10-16 23:01:59 +02:00
parent ee91cae4bb
commit e9b5ff312b
2 changed files with 48 additions and 113 deletions

View File

@ -3,6 +3,7 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <ctime> #include <ctime>
#include <ios>
namespace gz { namespace gz {
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -52,55 +53,32 @@ namespace gz {
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) 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), writeToFileAfterLines(writeAfterLines), storeLog(storeLog), showLog(showLog), prefixColor(prefixColor), prefix(prefix_ + ": "), prefixLength(prefix.size() + LOG_TIMESTAMP_CHAR_COUNT - 1), timeColor(timeColor) { : iter(0),
// get absolute path to the logfile writeToFileAfterLines(writeAfterLines), clearLogfileOnRestart(clearLogfileOnRestart),
fs::path logpath(logfile); logFile(logfile), storeLog(storeLog),
if (!logpath.is_absolute()) { showLog(showLog),
logpath = fs::current_path() / logpath; prefixColor(prefixColor), prefix(prefix_ + ": "),
} showTime(showTime), timeColor(timeColor)
// create directory of logfile {
if (!fs::is_directory(logpath.parent_path())) { init();
fs::create_directory(logpath.parent_path());
}
logFile = logpath.string();
// if clearLogfileOnRestart, open the file to clear it
if (clearLogfileOnRestart and fs::is_regular_file(logfile)) {
std::ofstream file(logFile, std::ofstream::trunc);
file.close();
}
if (writeToFileAfterLines == 0) { writeToFileAfterLines = 1; }
logLines.resize(writeToFileAfterLines);
// reserve memory for strings
if (LOG_RESERVE_STRING_SIZE > 0) {
for (size_t i = 0; i < logLines.size(); i++) {
logLines[i].reserve(LOG_RESERVE_STRING_SIZE);
}
}
if (showTime) {
prefixLength = prefix.size() + LOG_TIMESTAMP_CHAR_COUNT - 1;
}
else {
prefixLength = prefix.size();
}
/* log("Initialising log with settings: logFile: " + logFile + */
/* ", showLog - " + boolToString(showLog) + ", storeLog - " + boolToString(storeLog)); */
} }
Log::Log(LogCreateInfo&& ci) Log::Log(LogCreateInfo&& ci)
: iter(0), : iter(0),
writeToFileAfterLines(ci.writeAfterLines), storeLog(ci.storeLog), writeToFileAfterLines(ci.writeAfterLines), clearLogfileOnRestart(ci.clearLogfileOnRestart),
logFile(ci.logfile), storeLog(ci.storeLog),
showLog(ci.showLog), showLog(ci.showLog),
prefixColor(ci.prefixColor), prefixColor(ci.prefixColor),
prefix(std::move(ci.prefix) + ": "), prefix(ci.prefix + ": "),
showTime(ci.showTime), timeColor(ci.timeColor) showTime(ci.showTime), timeColor(ci.timeColor)
{ {
init();
}
void Log::init() {
// get absolute path to the logfile // get absolute path to the logfile
fs::path logpath(ci.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;
} }
@ -111,7 +89,7 @@ namespace gz {
logFile = logpath.string(); logFile = logpath.string();
// if clearLogfileOnRestart, open the file to clear it // if clearLogfileOnRestart, open the file to clear it
if (ci.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();
} }
@ -125,15 +103,8 @@ namespace gz {
} }
} }
if (showTime) { // reserve memory for argsBegin
prefixLength = prefix.size() + LOG_TIMESTAMP_CHAR_COUNT - 1; argsBegin.reserve(ARG_COUNT_RESERVE_COUNT);
}
else {
prefixLength = prefix.size();
}
/* log("Initialising log with settings: logFile: " + logFile + */
/* ", showLog - " + boolToString(showLog) + ", storeLog - " + boolToString(storeLog)); */
} }
@ -152,7 +123,7 @@ namespace gz {
void Log::writeLog() { void Log::writeLog() {
std::ofstream file(logFile); 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;

View File

@ -13,9 +13,9 @@
#endif #endif
namespace gz { namespace gz {
/// Reserve a string size for each string in logArray. Set to 0 if you do not want to reserve memory for strings. /// Reserve a string size for each string in logArray. Set to 0 if you do not want to reserve memory for strings.
constexpr unsigned int LOG_RESERVE_STRING_SIZE = 100; constexpr unsigned int LOG_RESERVE_STRING_SIZE = 100;
constexpr unsigned int ARG_COUNT_RESERVE_COUNT = 6;
constexpr unsigned int LOG_TIMESTAMP_CHAR_COUNT = 22; constexpr unsigned int LOG_TIMESTAMP_CHAR_COUNT = 22;
constexpr unsigned int LOG_POSTPREFIX_CHAR_COUNT = 2; constexpr unsigned int LOG_POSTPREFIX_CHAR_COUNT = 2;
@ -153,60 +153,28 @@ class Log {
#ifdef LOG_MULTITHREAD #ifdef LOG_MULTITHREAD
mtx.lock(); mtx.lock();
#endif #endif
argsBegin.clear();
if (showTime) { if (showTime) {
getTime(); getTime();
logLines[iter] = time; logLines[iter] = time;
} }
else { else {
logLines[iter].clear(); logLines.clear();
} }
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());
if (showLog) { if (showLog) {
std::cout << COLORS[timeColor] << std::string_view(logLines[iter].c_str(), LOG_TIMESTAMP_CHAR_COUNT - 1) << // time
COLORS[prefixColor] << prefix << COLORS[RESET] << std::cout << COLORS[timeColor] << std::string_view(logLines[iter].begin(), logLines[iter].begin() + argsBegin[0])
std::string_view(logLines[iter].begin() + prefixLength, logLines[iter].end()); // prefix
} << COLORS[prefixColor] << std::string_view(logLines[iter].begin() + argsBegin[0], logLines[iter].begin() + argsBegin[1]) << COLORS[RESET]
if (++iter >= writeToFileAfterLines) { // message
iter = 0; << std::string_view(logLines[iter].begin() + argsBegin[1], logLines[iter].end());
writeLog();
}
#ifdef LOG_MULTITHREAD
mtx.unlock();
#endif
}
/**
* @brief Log a message in a certain color and with a colored type
* @details
* The message will look like this:
* <time>: <prefix>: <type>: <message>
* where time will be white, prefix in prefixColor, type in typeColor and message in messageColor
* @param args Any number of arguments that satisfy concept Logable
*/
template<Logable... Args>
void clog(Color typeColor, std::string&& type, Color messageColor, Args&&... args) {
#ifdef LOG_MULTITHREAD
mtx.lock();
#endif
if (showTime) {
getTime();
logLines[iter] = std::string(time);
}
else {
logLines[iter].clear();
}
logLines[iter] += prefix + type + ": ";
vlog(" ", std::forward<Args>(args)...);
logLines[iter] += "\n";
if (showLog) {
std::cout << COLORS[timeColor] << std::string_view(logLines[iter].c_str(), LOG_TIMESTAMP_CHAR_COUNT - 1) <<
COLORS[prefixColor] << prefix << COLORS[typeColor] <<
std::string_view(logLines[iter].begin() + prefixLength + LOG_POSTPREFIX_CHAR_COUNT, logLines[iter].begin() + prefixLength + type.size() + 2 * LOG_POSTPREFIX_CHAR_COUNT) <<
COLORS[messageColor] << std::string_view(logLines[iter].begin() + prefixLength + type.size() + 2 * LOG_POSTPREFIX_CHAR_COUNT, logLines[iter].end()) << COLORS[RESET];
} }
if (++iter >= writeToFileAfterLines) { if (++iter >= writeToFileAfterLines) {
iter = 0; iter = 0;
@ -221,7 +189,7 @@ class Log {
* @brief Log a message in a certain color * @brief Log a message in a certain color
* @details * @details
* The message will look like this: * The message will look like this:
* <time>: <prefix>: <message0>, <message1>... * <time>: <prefix>: <message0> <message1>...
* where time will be white, prefix in prefixColor, and messageI in colors[I]. * where time will be white, prefix in prefixColor, and messageI in colors[I].
* If there are less colors than message arguments, the last color is used for all remaining messages. * If there are less colors than message arguments, the last color is used for all remaining messages.
* @param args Any number of arguments that satisfy concept Logable * @param args Any number of arguments that satisfy concept Logable
@ -233,7 +201,6 @@ class Log {
mtx.lock(); mtx.lock();
#endif #endif
argsBegin.clear(); argsBegin.clear();
argsBegin.emplace_back(0);
if (showTime) { if (showTime) {
getTime(); getTime();
logLines[iter] = std::string(time); logLines[iter] = std::string(time);
@ -247,23 +214,19 @@ class Log {
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());
/* std::cout << "Log has Views:\n"; */
/* for (auto& view : argsBegin) { */
/* std::cout << "\t" << view << "\n"; */
/* } */
if (showLog) { if (showLog) {
// log prefix // time
std::cout << COLORS[timeColor] << std::cout << COLORS[timeColor] << std::string_view(logLines[iter].begin(), logLines[iter].begin() + argsBegin[0])
std::string_view(logLines[iter].begin() + argsBegin[0], logLines[iter].begin() + argsBegin[1]) << COLORS[prefixColor] << // prefix
std::string_view(logLines[iter].begin() + argsBegin[1], logLines[iter].begin() + argsBegin[2]) << 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+3 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() - 3); 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+2], logLines[iter].begin() + argsBegin[i+3]); 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() - 3 // log the rest, maxI is now <= argsBegin.size() - 2
std::cout << std::string_view(logLines[iter].begin() + argsBegin[maxI+2], 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;
@ -296,7 +259,7 @@ class Log {
*/ */
template<Logable... Args> template<Logable... Args>
void error(Args&&... args) { void error(Args&&... args) {
clog(RED, "Error", WHITE, std::forward<Args>(args)...); clog({RED, WHITE}, "Error:", std::forward<Args>(args)...);
} }
/** /**
@ -309,7 +272,7 @@ class Log {
*/ */
template<Logable... Args> template<Logable... Args>
void warning(Args&&... args) { void warning(Args&&... args) {
clog(YELLOW, "Warning", WHITE, std::forward<Args>(args)...); clog({YELLOW, WHITE}, "Warning:", std::forward<Args>(args)...);
} }
private: private:
@ -318,7 +281,7 @@ class Log {
template<util::Stringy T, Logable... Args> template<util::Stringy T, Logable... Args>
void vlog(const char* appendChars, T&& t, Args&&... args) { void vlog(const char* appendChars, T&& t, Args&&... args) {
argsBegin.emplace_back(logLines[iter].size()); argsBegin.emplace_back(logLines[iter].size());
logLines[iter] += t; logLines[iter] += std::string(t);
logLines[iter] += appendChars; logLines[iter] += appendChars;
vlog(" ", std::forward< Args>(args)...); vlog(" ", std::forward< Args>(args)...);
} }
@ -334,6 +297,7 @@ class Log {
void vlog(const char* appendChars) {}; void vlog(const char* appendChars) {};
private: private:
void init();
/// 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]
@ -346,6 +310,7 @@ class Log {
/// @{ /// @{
/// When iter reaches writeToFileAfterLines, write log to file /// When iter reaches writeToFileAfterLines, write log to file
unsigned int writeToFileAfterLines; unsigned int writeToFileAfterLines;
bool clearLogfileOnRestart;
/// Absolute path to the logfile /// Absolute path to the logfile
std::string logFile; std::string logFile;
bool storeLog; bool storeLog;
@ -355,7 +320,6 @@ class Log {
bool showLog; bool showLog;
Color prefixColor; Color prefixColor;
std::string prefix; std::string prefix;
std::string::size_type prefixLength;
/** /**
* @name Time * @name Time