From f1cb6f05f53ece13be2eeb3fc0d675162245e5cb Mon Sep 17 00:00:00 2001
From: Brett <brettmaster1@gmail.com>
Date: Wed, 8 Feb 2023 14:12:21 -0500
Subject: [PATCH] flush

---
 include/blt/std/logging.h |  5 +++++
 src/blt/std/logging.cpp   | 27 +++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/include/blt/std/logging.h b/include/blt/std/logging.h
index a0b6dbf..be9e5d7 100644
--- a/include/blt/std/logging.h
+++ b/include/blt/std/logging.h
@@ -41,6 +41,7 @@ namespace blt::logging {
                 logi(str);
             }
         #endif
+            void flush() const;
     };
     
     static logger tlog{TRACE};
@@ -129,6 +130,10 @@ namespace blt::logging {
     void log(unsigned short i, LOG_LEVEL level, int auto_line);
     void log(float f, LOG_LEVEL level, int auto_line);
     void log(double f, LOG_LEVEL level, int auto_line);
+    /**
+     * Will flush all buffers! This might cause issues with threads!
+     */
+    void flush();
 }
 
 #ifdef BLT_DISABLE_LOGGING
diff --git a/src/blt/std/logging.cpp b/src/blt/std/logging.cpp
index 8f7a7d4..c6e29a3 100644
--- a/src/blt/std/logging.cpp
+++ b/src/blt/std/logging.cpp
@@ -210,6 +210,33 @@ namespace blt::logging {
             thread_local_strings[id] = th_str;
         }
     }
+    
+    void logger::flush() const {
+        for (const auto& id : thread_local_strings) {
+            auto th_str = id.second;
+            bool hasEndingLinefeed = th_str[th_str.length() - 1] == '\n';
+            logging::log(th_str, false, level, !hasEndingLinefeed);
+            thread_local_strings[id.first] = "";
+        }
+    }
+    
+    void flush() {
+        // TODO: this will prevent proper level output. Please fixme
+        tlog.flush();
+        dlog.flush();
+        ilog.flush();
+        wlog.flush();
+        elog.flush();
+        flog.flush();
+        trace.flush();
+        debug.flush();
+        info.flush();
+        warn.flush();
+        error.flush();
+        fatal.flush();
+        std::cerr.flush();
+        std::cout.flush();
+    }
 }