From 5e1deefe48d95c3ee810f15c003871ee9f83427b Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 23 Jan 2023 23:53:37 -0500 Subject: [PATCH] Add time to the log output --- include/blt/profiling/profiler.h | 8 +++++++ include/blt/std/logging.h | 5 +++-- include/blt/std/time.h | 38 +++++++++++++++++++++++++++++--- src/blt/std/logging.cpp | 36 ++++++++++++++++++------------ src/tests/main.cpp | 4 ++++ 5 files changed, 72 insertions(+), 19 deletions(-) diff --git a/include/blt/profiling/profiler.h b/include/blt/profiling/profiler.h index 31a3561..289e4eb 100644 --- a/include/blt/profiling/profiler.h +++ b/include/blt/profiling/profiler.h @@ -13,6 +13,7 @@ #include #include #include +#include namespace blt { struct CapturePoint { @@ -72,6 +73,13 @@ namespace blt { void profilerPointCyclic(const std::string_view& name) { cyclicPoints.push(CapturePoint{name, System::getCurrentTimeNanoseconds()}); } + + void print(){ + // TODO: + for (auto c : intervals){ + std::cout << c.first << " " << c.second.start << " " << c.second.end << ": " << std::to_string((c.second.end - c.second.start)/1000000) << "\n"; + } + } }; class Profiler { diff --git a/include/blt/std/logging.h b/include/blt/std/logging.h index 0a21997..dbb0673 100644 --- a/include/blt/std/logging.h +++ b/include/blt/std/logging.h @@ -17,9 +17,10 @@ namespace blt::logging { bool m_useColor = true; bool m_logToConsole = true; bool m_logToFile = true; + const char* m_directory = "./"; - explicit constexpr LOG_PROPERTIES(bool useColor, bool logToConsole, bool logToFile): - m_useColor(useColor), m_logToConsole(logToConsole), m_logToFile(logToFile) {} + explicit constexpr LOG_PROPERTIES(bool useColor, bool logToConsole, bool logToFile, const char* directory): + m_useColor(useColor), m_logToConsole(logToConsole), m_logToFile(logToFile), m_directory(directory) {} explicit constexpr LOG_PROPERTIES() = default; }; diff --git a/include/blt/std/time.h b/include/blt/std/time.h index 4610300..8d177ce 100644 --- a/include/blt/std/time.h +++ b/include/blt/std/time.h @@ -4,14 +4,27 @@ * See LICENSE file for license detail */ -#ifndef BLT_TIME_H -#define BLT_TIME_H - #include #include #include +#ifndef BLT_TIME_H +#define BLT_TIME_H + namespace blt::System { + static inline std::string ensureHasDigits(int current, int digits) { + std::string asString = std::to_string(current); + auto length = digits - asString.length(); + if (length <= 0) + return asString; + std::string zeros; + zeros.reserve(length); + for (int i = 0; i < length; i++){ + zeros += '0'; + } + return zeros + asString; + } + static inline auto getCurrentTimeNanoseconds() { return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); } @@ -39,6 +52,25 @@ namespace blt::System { timeString << now->tm_sec; return timeString.str(); } + + /** + * Standard logging time string is formatted as follows: + * Hour:Min:Second + * @return the BLT standard logging string of time.now + */ + static inline std::string getTimeStringLog() { + auto t = std::time(nullptr); + auto now = std::localtime(&t); + std::string timeString = "["; + timeString += ensureHasDigits(now->tm_hour, 2); + timeString += ":"; + timeString += ensureHasDigits(now->tm_min, 2); + timeString += ":"; + timeString += ensureHasDigits(now->tm_sec, 2); + timeString += "] "; + return timeString; + } + /** * @return the BLT standard string of time.now (See getTimeString()) that is filesystem friendly (FAT compatible). */ diff --git a/src/blt/std/logging.cpp b/src/blt/std/logging.cpp index d2fca39..a9443c2 100644 --- a/src/blt/std/logging.cpp +++ b/src/blt/std/logging.cpp @@ -4,6 +4,7 @@ * See LICENSE file for license detail */ #include +#include #include #include #include @@ -92,16 +93,16 @@ namespace blt::logging { }; const char* levelNames[6] = { - "[Trace]: ", - "[Debug]: ", - "[Info]: ", - "[Warn]: ", - "[Error]: ", - "[Fatal]: ", + "[TRACE]: ", + "[DEBUG]: ", + "[INFO]: ", + "[WARN]: ", + "[ERROR]: ", + "[FATAL]: ", }; // by default everything is enabled - LOG_PROPERTIES BLT_LOGGING_PROPERTIES{true, true, true}; + LOG_PROPERTIES BLT_LOGGING_PROPERTIES{true, true, true, "./"}; void init(LOG_PROPERTIES properties) { BLT_LOGGING_PROPERTIES = properties; @@ -111,17 +112,24 @@ namespace blt::logging { va_list args; va_start(args, format); - std::stringstream finalOutput; auto formattedString = applyFormat(format, args); + bool hasEndingLinefeed = formattedString[formattedString.length()-1] == '\n'; + + if (hasEndingLinefeed) + formattedString = formattedString.substr(0, formattedString.length()-1); + + std::string outputString = System::getTimeStringLog(); + outputString += levelNames[level]; if (BLT_LOGGING_PROPERTIES.m_useColor) - finalOutput << levelColors[level]; - finalOutput << levelNames[level]; - finalOutput << formattedString; - if (BLT_LOGGING_PROPERTIES.m_useColor) - finalOutput << "\033[0m"; + outputString = levelColors[level] + outputString; - std::string outputString = finalOutput.str(); + outputString += formattedString; + + if (BLT_LOGGING_PROPERTIES.m_useColor) + outputString += "\033[0m"; + if (hasEndingLinefeed || auto_line) + outputString += "\n"; if (BLT_LOGGING_PROPERTIES.m_logToConsole) { if (level > WARN) diff --git a/src/tests/main.cpp b/src/tests/main.cpp index c1a1db1..f76b899 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -1,7 +1,10 @@ +#include #include "binary_trees.h" #include "blt/std/string.h" +#include "blt/profiling/profiler.h" #include "blt/std/logging.h" +#include "blt/std/time.h" int main() { binaryTreeTest(); @@ -23,4 +26,5 @@ int main() { std::string hello = "superSexyMax"; std::cout << "String starts with: " << blt::string::contains(hello, "superSexyMaxE") << "\n"; + return 0; } \ No newline at end of file