Add time to the log output

v1
Brett 2023-01-23 23:53:37 -05:00
parent d025b0595c
commit 5e1deefe48
5 changed files with 72 additions and 19 deletions

View File

@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include <blt/std/time.h> #include <blt/std/time.h>
#include <blt/std/queue.h> #include <blt/std/queue.h>
#include <iostream>
namespace blt { namespace blt {
struct CapturePoint { struct CapturePoint {
@ -72,6 +73,13 @@ namespace blt {
void profilerPointCyclic(const std::string_view& name) { void profilerPointCyclic(const std::string_view& name) {
cyclicPoints.push(CapturePoint{name, System::getCurrentTimeNanoseconds()}); 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 { class Profiler {

View File

@ -17,9 +17,10 @@ namespace blt::logging {
bool m_useColor = true; bool m_useColor = true;
bool m_logToConsole = true; bool m_logToConsole = true;
bool m_logToFile = true; bool m_logToFile = true;
const char* m_directory = "./";
explicit constexpr LOG_PROPERTIES(bool useColor, bool logToConsole, bool logToFile): explicit constexpr LOG_PROPERTIES(bool useColor, bool logToConsole, bool logToFile, const char* directory):
m_useColor(useColor), m_logToConsole(logToConsole), m_logToFile(logToFile) {} m_useColor(useColor), m_logToConsole(logToConsole), m_logToFile(logToFile), m_directory(directory) {}
explicit constexpr LOG_PROPERTIES() = default; explicit constexpr LOG_PROPERTIES() = default;
}; };

View File

@ -4,14 +4,27 @@
* See LICENSE file for license detail * See LICENSE file for license detail
*/ */
#ifndef BLT_TIME_H
#define BLT_TIME_H
#include <chrono> #include <chrono>
#include <ctime> #include <ctime>
#include <sstream> #include <sstream>
#ifndef BLT_TIME_H
#define BLT_TIME_H
namespace blt::System { 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() { static inline auto getCurrentTimeNanoseconds() {
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
} }
@ -39,6 +52,25 @@ namespace blt::System {
timeString << now->tm_sec; timeString << now->tm_sec;
return timeString.str(); 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). * @return the BLT standard string of time.now (See getTimeString()) that is filesystem friendly (FAT compatible).
*/ */

View File

@ -4,6 +4,7 @@
* See LICENSE file for license detail * See LICENSE file for license detail
*/ */
#include <blt/std/logging.h> #include <blt/std/logging.h>
#include <blt/std/time.h>
#include <cstdarg> #include <cstdarg>
#include <string> #include <string>
#include <sstream> #include <sstream>
@ -92,16 +93,16 @@ namespace blt::logging {
}; };
const char* levelNames[6] = { const char* levelNames[6] = {
"[Trace]: ", "[TRACE]: ",
"[Debug]: ", "[DEBUG]: ",
"[Info]: ", "[INFO]: ",
"[Warn]: ", "[WARN]: ",
"[Error]: ", "[ERROR]: ",
"[Fatal]: ", "[FATAL]: ",
}; };
// 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, "./"};
void init(LOG_PROPERTIES properties) { void init(LOG_PROPERTIES properties) {
BLT_LOGGING_PROPERTIES = properties; BLT_LOGGING_PROPERTIES = properties;
@ -111,17 +112,24 @@ namespace blt::logging {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
std::stringstream finalOutput;
auto formattedString = applyFormat(format, args); 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) if (BLT_LOGGING_PROPERTIES.m_useColor)
finalOutput << levelColors[level]; outputString = levelColors[level] + outputString;
finalOutput << levelNames[level];
finalOutput << formattedString;
if (BLT_LOGGING_PROPERTIES.m_useColor)
finalOutput << "\033[0m";
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 (BLT_LOGGING_PROPERTIES.m_logToConsole) {
if (level > WARN) if (level > WARN)

View File

@ -1,7 +1,10 @@
#include <unordered_map>
#include "binary_trees.h" #include "binary_trees.h"
#include "blt/std/string.h" #include "blt/std/string.h"
#include "blt/profiling/profiler.h"
#include "blt/std/logging.h" #include "blt/std/logging.h"
#include "blt/std/time.h"
int main() { int main() {
binaryTreeTest(); binaryTreeTest();
@ -23,4 +26,5 @@ int main() {
std::string hello = "superSexyMax"; std::string hello = "superSexyMax";
std::cout << "String starts with: " << blt::string::contains(hello, "superSexyMaxE") << "\n"; std::cout << "String starts with: " << blt::string::contains(hello, "superSexyMaxE") << "\n";
return 0;
} }