Add stream version of the logging macros

v1
Brett 2023-01-24 23:01:34 -05:00
parent 51a1468cf0
commit bc4bf76be7
3 changed files with 107 additions and 1 deletions

View File

@ -27,6 +27,90 @@ namespace blt::logging {
explicit constexpr LOG_PROPERTIES() = default; 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 init(LOG_PROPERTIES properties);
void log(const std::string& format, LOG_LEVEL level, int auto_line, ...); void log(const std::string& format, LOG_LEVEL level, int auto_line, ...);
void log(int i, LOG_LEVEL level, int auto_line); void log(int i, LOG_LEVEL level, int auto_line);

View File

@ -10,8 +10,9 @@
#include <iostream> #include <iostream>
#include "blt/std/string.h" #include "blt/std/string.h"
#include <fstream> #include <fstream>
#include <utility> #include <unordered_map>
#include <ios> #include <ios>
#include <thread>
// https://en.cppreference.com/w/cpp/utility/variadic // https://en.cppreference.com/w/cpp/utility/variadic
// https://medium.com/swlh/variadic-functions-3419c287a0d2 // https://medium.com/swlh/variadic-functions-3419c287a0d2
@ -178,6 +179,22 @@ namespace blt::logging {
log(std::to_string(f), false, level, true); log(std::to_string(f), false, level, true);
} }
std::unordered_map<std::thread::id, std::string> 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;
}
}
} }

View File

@ -19,6 +19,11 @@ int main() {
BLT_ERROR("Hello World!\n"); BLT_ERROR("Hello World!\n");
BLT_FATAL("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::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";