Files
grafikaKBT/Logger.hpp
2026-02-02 00:33:08 +01:00

42 lines
1.1 KiB
C++

#pragma once
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <atomic>
#include <sstream> // Niezbędne dla makra
class AsyncLogger {
public:
// Tylko deklaracje funkcji
static AsyncLogger& getInstance();
void log(const std::string& message);
void start();
void stop();
// Usuwamy copy constructor i operator przypisania (Singleton)
AsyncLogger(const AsyncLogger&) = delete;
void operator=(const AsyncLogger&) = delete;
private:
AsyncLogger(); // Prywatny konstruktor
~AsyncLogger(); // Prywatny destruktor
void processQueue();
std::thread loggingThread;
std::mutex queueMutex;
std::condition_variable cv;
std::queue<std::string> logQueue;
std::atomic<bool> running;
};
// Zmiana nazwy makra na GAME_LOG, aby uniknąć konfliktów (np. z bibliotekami matematycznymi)
// Używamy pętli do...while(0), aby makro było bezpieczne w instrukcjach if/else
#define GAME_LOG(stream_args) { \
std::ostringstream ss; \
ss << stream_args; \
AsyncLogger::getInstance().log(ss.str()); \
}