feat: console on new thread

This commit is contained in:
Pc
2026-01-26 22:46:33 +01:00
parent ffa5a929df
commit 233d4189d1
3 changed files with 33 additions and 25 deletions

View File

@@ -6,29 +6,27 @@
#include <condition_variable>
#include <queue>
#include <atomic>
#include <sstream> // <--- TO JEST NIEZBĘDNE
class AsyncLogger {
public:
// Singleton - jedna instancja loggera na całą grę
static AsyncLogger& getInstance() {
static AsyncLogger instance;
return instance;
}
// Dodaj wiadomość do kolejki (wywoływane z gry) - to jest super szybkie
void log(const std::string& message) {
std::lock_guard<std::mutex> lock(queueMutex);
logQueue.push(message);
cv.notify_one(); // Obudź wątek loggera
cv.notify_one();
}
// Uruchom wątek (wywołaj raz na początku programu)
void start() {
if (running) return;
running = true;
loggingThread = std::thread(&AsyncLogger::processQueue, this);
}
// Zatrzymaj wątek (wywołaj przy zamykaniu)
void stop() {
running = false;
cv.notify_one();
@@ -41,20 +39,14 @@ private:
AsyncLogger() : running(false) {}
~AsyncLogger() { stop(); }
// To jest funkcja, która działa w osobnym wątku
void processQueue() {
while (running || !logQueue.empty()) {
std::unique_lock<std::mutex> lock(queueMutex);
// Czekaj, aż coś pojawi się w kolejce lub zakończymy program
cv.wait(lock, [this] { return !logQueue.empty() || !running; });
while (!logQueue.empty()) {
std::string msg = logQueue.front();
logQueue.pop();
// Zwalniamy mutex przed wypisywaniem, żeby nie blokować głównego wątku
// gdy konsola jest "zamrożona" przez użytkownika
lock.unlock();
std::cout << msg << std::endl;
@@ -71,5 +63,10 @@ private:
std::atomic<bool> running;
};
// Makro dla wygody używania
#define LOG(msg) AsyncLogger::getInstance().log(msg)
// --- POPRAWIONE MAKRO ---
// Tworzy tymczasowy strumień (ostringstream), który "rozumie" operator <<
#define LOG(stream_args) { \
std::ostringstream ss; \
ss << stream_args; \
AsyncLogger::getInstance().log(ss.str()); \
}