Made logging thread-safe
This commit is contained in:
parent
a9c4b75e46
commit
799c9c4218
@ -1,4 +1,5 @@
|
|||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
|
#include <mutex>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
@ -6,18 +7,21 @@
|
|||||||
|
|
||||||
using namespace sc;
|
using namespace sc;
|
||||||
|
|
||||||
|
std::mutex logger::_mtx;
|
||||||
|
|
||||||
logger::logger(const std::string& name, loglevel level)
|
logger::logger(const std::string& name, loglevel level)
|
||||||
: _level(level), _name(name) {}
|
: _level(level), _name(name) {}
|
||||||
|
|
||||||
void logger::msg(loglevel level, const sc::io::trgb* color,
|
void logger::msg(loglevel level, const sc::io::trgb* color,
|
||||||
const char* file, int line, const char* func) {
|
const char* file, int line, const char* func) {
|
||||||
if (level >= _level) {
|
if (level >= _level) {
|
||||||
|
std::lock_guard<std::mutex> lock {_mtx};
|
||||||
if (_topcall) {
|
if (_topcall) {
|
||||||
write_prefix(level, color, file, line, func);
|
write_prefix(level, color, file, line, func);
|
||||||
} else {
|
} else {
|
||||||
_topcall = true;
|
_topcall = true;
|
||||||
}
|
}
|
||||||
std::cerr << '\n';
|
std::cerr << std::endl;
|
||||||
if (color) {
|
if (color) {
|
||||||
std::cerr << sc::io::reset;
|
std::cerr << sc::io::reset;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <libscterm.hpp>
|
#include <libscterm.hpp>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace sc {
|
namespace sc {
|
||||||
|
|
||||||
@ -66,6 +67,7 @@ namespace sc {
|
|||||||
loglevel _level {loglevel::info};
|
loglevel _level {loglevel::info};
|
||||||
bool _topcall {true};
|
bool _topcall {true};
|
||||||
std::string _name;
|
std::string _name;
|
||||||
|
static std::mutex _mtx;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user