Fixed concept stringy, improved getTime

This commit is contained in:
matthias@arch 2022-09-17 19:57:04 +02:00
parent e157591db3
commit 00e0bc0715
2 changed files with 18 additions and 13 deletions

7
src/log.cpp Normal file → Executable file
View File

@ -33,7 +33,7 @@ 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 clearLogfileOnRestart, unsigned int writeAfterLines) Log::Log(std::string logfile, bool showLog, bool storeLog, std::string&& prefix_, Color prefixColor, bool clearLogfileOnRestart, unsigned int writeAfterLines)
: showLog(showLog), storeLog(storeLog), prefixColor(prefixColor), prefix(prefix_ + ": "), prefixLength(prefix.size() + LOG_TIMESTAMP_CHAR_COUNT - 1), writeToFileAfterLines(writeAfterLines) { : iter(0), writeToFileAfterLines(writeAfterLines), showLog(showLog), storeLog(storeLog), prefixColor(prefixColor), prefix(prefix_ + ": "), prefixLength(prefix.size() + LOG_TIMESTAMP_CHAR_COUNT - 1) {
// get absolute path to the logfile // get absolute path to the logfile
fs::path logpath(logfile); fs::path logpath(logfile);
if (!logpath.is_absolute()) { if (!logpath.is_absolute()) {
@ -43,7 +43,7 @@ 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 = std::move(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(logfile)) { if (clearLogfileOnRestart and fs::is_regular_file(logfile)) {
@ -52,7 +52,7 @@ namespace gz {
} }
if (writeToFileAfterLines == 0) { writeToFileAfterLines = 1; } if (writeToFileAfterLines == 0) { writeToFileAfterLines = 1; }
logLines.resize(writeAfterLines); 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++) {
@ -74,7 +74,6 @@ namespace gz {
std::time_t t = std::time(0); std::time_t t = std::time(0);
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, sizeof(time), "%F %T: ", tmp);
} }

24
src/log.hpp Normal file → Executable file
View File

@ -29,10 +29,11 @@ namespace gz {
// //
// CONCEPTS // CONCEPTS
// //
/// is appendable to std::string /// is (similar or convertible to) std::string
template<typename T> template<typename T>
/* concept Stringy = std::same_as<T, std::string> || std::convertible_to<T, std::string_view>; */ concept Stringy = std::same_as<T, std::string> || std::convertible_to<T, std::string_view>;
concept Stringy = requires(T t, std::string s) { s += t; }; /// is appendable to std::string
/* concept Stringy = requires(T t, std::string s) { s += t; }; */
/// has .to_string() member /// has .to_string() member
template<typename T> template<typename T>
@ -57,7 +58,11 @@ namespace gz {
/// Type having printable .x and .y members /// Type having printable .x and .y members
template<typename T> template<typename T>
concept Vector2Printable = !Printable<T> && concept Vector2Printable = !Printable<T> &&
requires(T t) { { t.x } -> Printable; { t.y } -> Printable; }; requires(T t) {
{ t.x } -> Printable;
{ t.y } -> Printable;
requires sizeof(t.x) * 2 == sizeof(T);
};
/// Pair having printable elements /// Pair having printable elements
template<typename T> template<typename T>
@ -111,8 +116,7 @@ namespace gz {
* 1-7 include for example: * 1-7 include for example:
* - int, float, bool... * - int, float, bool...
* - std::vector<std::string>, std::list<unsigned int> * - std::vector<std::string>, std::list<unsigned int>
* - std::map<A, vec2<float>> if A.to_string() returns a string * - std::map<A, vec2<float>> if A.to_string() returns a string - ...
* - ...
*/ */
template<typename T> template<typename T>
concept Logable = LogableNotPointer<T> || LogableSmartPointer<T>; concept Logable = LogableNotPointer<T> || LogableSmartPointer<T>;
@ -179,7 +183,8 @@ class Log {
#ifdef LOG_MULTITHREAD #ifdef LOG_MULTITHREAD
mtx.lock(); mtx.lock();
#endif #endif
logLines[iter] = getTime(); getTime();
logLines[iter] = time;
logLines[iter] += prefix; logLines[iter] += prefix;
vlog(" ", std::forward<Args>(args)...); vlog(" ", std::forward<Args>(args)...);
logLines[iter] += "\n"; logLines[iter] += "\n";
@ -244,7 +249,7 @@ class Log {
mtx.lock(); mtx.lock();
#endif #endif
getTime(); getTime();
logLines[iter] = time; logLines[iter] = std::string(time);
logLines[iter] += prefix + ": " + type + ": "; logLines[iter] += prefix + ": " + type + ": ";
vlog(" ", std::forward<Args>(args)...); vlog(" ", std::forward<Args>(args)...);
logLines[iter] += "\n"; logLines[iter] += "\n";
@ -336,6 +341,7 @@ class Log {
for (auto it = t.begin(); it != t.end(); it++) { for (auto it = t.begin(); it != t.end(); it++) {
vlog(", ", *it); vlog(", ", *it);
} }
logLines[iter].erase(logLines[iter].size() - 2);
logLines[iter] += "]"; logLines[iter] += "]";
logLines[iter] += appendChars; logLines[iter] += appendChars;
vlog(" ", std::forward< Args>(args)...); vlog(" ", std::forward< Args>(args)...);
@ -367,7 +373,7 @@ class Log {
/// Where the lines are stored /// Where the lines are stored
std::vector<std::string> logLines; std::vector<std::string> logLines;
/// The current position in logLines /// The current position in logLines
size_t iter; size_t iter = 0;
/// When iter reaches writeToFileAfterLines, write log to file /// When iter reaches writeToFileAfterLines, write log to file
unsigned int writeToFileAfterLines; unsigned int writeToFileAfterLines;
/// Absolute path to the logfile /// Absolute path to the logfile