Made logging thread-safe
This commit is contained in:
		@@ -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;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user