|
|
@@ -1,54 +1,7 @@ |
|
|
|
#include "logger.h" |
|
|
|
|
|
|
|
#include <mutex> |
|
|
|
#include <vector> |
|
|
|
#include <condition_variable> |
|
|
|
#include <thread> |
|
|
|
#include <iostream> |
|
|
|
#include <atomic> |
|
|
|
|
|
|
|
namespace logger { |
|
|
|
|
|
|
|
static std::vector<std::string> queue; |
|
|
|
static std::mutex mut; |
|
|
|
static std::condition_variable cond; |
|
|
|
static std::thread thread; |
|
|
|
static std::atomic_bool running; |
|
|
|
|
|
|
|
static void print(const std::string &msg) { |
|
|
|
std::cerr << msg << '\n'; |
|
|
|
} |
|
|
|
|
|
|
|
LogContext::~LogContext() { |
|
|
|
running.store(false); |
|
|
|
cond.notify_one(); |
|
|
|
thread.join(); |
|
|
|
} |
|
|
|
|
|
|
|
void log(std::string msg) { |
|
|
|
std::unique_lock<std::mutex> lock(mut); |
|
|
|
queue.emplace_back(std::move(msg)); |
|
|
|
lock.unlock(); |
|
|
|
cond.notify_one(); |
|
|
|
} |
|
|
|
|
|
|
|
LogContext init() { |
|
|
|
running = true; |
|
|
|
|
|
|
|
thread = std::thread([&] { |
|
|
|
std::unique_lock<std::mutex> lock(mut); |
|
|
|
while (running.load()) { |
|
|
|
cond.wait(lock, [&] { return !queue.empty() || !running.load(); }); |
|
|
|
|
|
|
|
for (std::string &msg: queue) { |
|
|
|
print(msg); |
|
|
|
} |
|
|
|
|
|
|
|
queue.clear(); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
return LogContext{}; |
|
|
|
} |
|
|
|
std::mutex mut; |
|
|
|
|
|
|
|
} |