Made logging thread-safe

This commit is contained in:
Bob Polis 2024-01-31 09:57:18 +01:00
parent a9c4b75e46
commit 799c9c4218
2 changed files with 7 additions and 1 deletions

View File

@ -1,4 +1,5 @@
#include "logger.hpp"
#include <mutex>
#include <unistd.h>
#include <sys/time.h>
#include <ctime>
@ -6,18 +7,21 @@
using namespace sc;
std::mutex logger::_mtx;
logger::logger(const std::string& name, loglevel level)
: _level(level), _name(name) {}
void logger::msg(loglevel level, const sc::io::trgb* color,
const char* file, int line, const char* func) {
if (level >= _level) {
std::lock_guard<std::mutex> lock {_mtx};
if (_topcall) {
write_prefix(level, color, file, line, func);
} else {
_topcall = true;
}
std::cerr << '\n';
std::cerr << std::endl;
if (color) {
std::cerr << sc::io::reset;
}

View File

@ -4,6 +4,7 @@
#include <iostream>
#include <string>
#include <libscterm.hpp>
#include <mutex>
namespace sc {
@ -66,6 +67,7 @@ namespace sc {
loglevel _level {loglevel::info};
bool _topcall {true};
std::string _name;
static std::mutex _mtx;
};
}