chore: main cleanup in new files
This commit is contained in:
60
Logger.hpp
60
Logger.hpp
@@ -1,60 +1,30 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <queue>
|
||||
#include <atomic>
|
||||
#include <sstream> // <--- TO JEST NIEZBĘDNE
|
||||
#include <sstream> // Niezbędne dla makra
|
||||
|
||||
class AsyncLogger {
|
||||
public:
|
||||
static AsyncLogger& getInstance() {
|
||||
static AsyncLogger instance;
|
||||
return instance;
|
||||
}
|
||||
// Tylko deklaracje funkcji
|
||||
static AsyncLogger& getInstance();
|
||||
|
||||
void log(const std::string& message) {
|
||||
std::lock_guard<std::mutex> lock(queueMutex);
|
||||
logQueue.push(message);
|
||||
cv.notify_one();
|
||||
}
|
||||
void log(const std::string& message);
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
void start() {
|
||||
if (running) return;
|
||||
running = true;
|
||||
loggingThread = std::thread(&AsyncLogger::processQueue, this);
|
||||
}
|
||||
|
||||
void stop() {
|
||||
running = false;
|
||||
cv.notify_one();
|
||||
if (loggingThread.joinable()) {
|
||||
loggingThread.join();
|
||||
}
|
||||
}
|
||||
// Usuwamy copy constructor i operator przypisania (Singleton)
|
||||
AsyncLogger(const AsyncLogger&) = delete;
|
||||
void operator=(const AsyncLogger&) = delete;
|
||||
|
||||
private:
|
||||
AsyncLogger() : running(false) {}
|
||||
~AsyncLogger() { stop(); }
|
||||
AsyncLogger(); // Prywatny konstruktor
|
||||
~AsyncLogger(); // Prywatny destruktor
|
||||
|
||||
void processQueue() {
|
||||
while (running || !logQueue.empty()) {
|
||||
std::unique_lock<std::mutex> lock(queueMutex);
|
||||
cv.wait(lock, [this] { return !logQueue.empty() || !running; });
|
||||
|
||||
while (!logQueue.empty()) {
|
||||
std::string msg = logQueue.front();
|
||||
logQueue.pop();
|
||||
lock.unlock();
|
||||
|
||||
std::cout << msg << std::endl;
|
||||
|
||||
lock.lock();
|
||||
}
|
||||
}
|
||||
}
|
||||
void processQueue();
|
||||
|
||||
std::thread loggingThread;
|
||||
std::mutex queueMutex;
|
||||
@@ -63,9 +33,9 @@ private:
|
||||
std::atomic<bool> running;
|
||||
};
|
||||
|
||||
// --- POPRAWIONE MAKRO ---
|
||||
// Tworzy tymczasowy strumień (ostringstream), który "rozumie" operator <<
|
||||
#define LOG(stream_args) { \
|
||||
// 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()); \
|
||||
|
||||
Reference in New Issue
Block a user