diff --git a/include/blt/std/logging.h b/include/blt/std/logging.h index 03fd466..fe34828 100644 --- a/include/blt/std/logging.h +++ b/include/blt/std/logging.h @@ -27,6 +27,90 @@ namespace blt::logging { explicit constexpr LOG_PROPERTIES() = default; }; + struct logger { + LOG_LEVEL level; + void logi(const std::string& str) const; + // evil hack, todo: better way + #ifdef BLT_DISABLE_LOGGING + void log(const std::string& str) { + + } + #else + void log(const std::string& str) { + logi(str); + } + #endif + }; + + static logger tlog{TRACE}; + static logger dlog{DEBUG}; + static logger ilog{INFO}; + static logger wlog{WARN}; + static logger elog{ERROR}; + static logger flog{FATAL}; + + static logger trace{TRACE}; + static logger debug{DEBUG}; + static logger info{INFO}; + static logger warn{WARN}; + static logger error{ERROR}; + static logger fatal{FATAL}; + + static inline logger& operator<<(logger& out, const std::string& str) { + out.log(str); + return out; + } + + static inline logger& operator<<(logger& out, const char chr) { + out.log(std::to_string(chr)); + return out; + } + + static inline logger& operator<<(logger& out, const unsigned char i) { + out.log(std::to_string(i)); + return out; + } + + static inline logger& operator<<(logger& out, const short i) { + out.log(std::to_string(i)); + return out; + } + + static inline logger& operator<<(logger& out, const unsigned short i) { + out.log(std::to_string(i)); + return out; + } + + static inline logger& operator<<(logger& out, const int i) { + out.log(std::to_string(i)); + return out; + } + + static inline logger& operator<<(logger& out, const unsigned int i) { + out.log(std::to_string(i)); + return out; + } + + static inline logger& operator<<(logger& out, const long i) { + out.log(std::to_string(i)); + return out; + } + + static inline logger& operator<<(logger& out, const unsigned long i) { + out.log(std::to_string(i)); + return out; + } + + static inline logger& operator<<(logger& out, const float f) { + out.log(std::to_string(f)); + return out; + } + + static inline logger& operator<<(logger& out, const double f) { + out.log(std::to_string(f)); + return out; + } + void init(LOG_PROPERTIES properties); void log(const std::string& format, LOG_LEVEL level, int auto_line, ...); void log(int i, LOG_LEVEL level, int auto_line); diff --git a/src/blt/std/logging.cpp b/src/blt/std/logging.cpp index b0ad60b..0e52299 100644 --- a/src/blt/std/logging.cpp +++ b/src/blt/std/logging.cpp @@ -10,8 +10,9 @@ #include #include "blt/std/string.h" #include -#include +#include #include +#include // https://en.cppreference.com/w/cpp/utility/variadic // https://medium.com/swlh/variadic-functions-3419c287a0d2 @@ -178,6 +179,22 @@ namespace blt::logging { log(std::to_string(f), false, level, true); } + std::unordered_map thread_local_strings; + + void logger::logi(const std::string& str) const { + auto id = std::this_thread::get_id(); + auto th_str = thread_local_strings[id]; + th_str += str; + + if (blt::string::contains(str, "\n")){ + // make sure new lines are properly formatted to prevent danging lines. Ie "[trace]: .... [debug]: ...." + bool hasEndingLinefeed = str[str.length()-1] == '\n'; + logging::log(th_str, false, level, !hasEndingLinefeed); + thread_local_strings[id] = ""; + } else { + thread_local_strings[id] = th_str; + } + } } diff --git a/src/tests/main.cpp b/src/tests/main.cpp index eb32fff..4d391e7 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -19,6 +19,11 @@ int main() { BLT_ERROR("Hello World!\n"); BLT_FATAL("Hello World!\n"); + blt::logging::tlog << "Hello! " << "Double stream insertion! " << 51 << 65 << " "; + blt::logging::tlog << "Same Line! "; + blt::logging::tlog << "Write the end!\n"; + blt::logging::tlog << "Seeee\n Super\n"; + std::string hello = "superSexyMax"; std::cout << "String starts with: " << blt::string::contains(hello, "superSexyMaxE") << "\n";