rhubarb-lip-sync/rhubarb/src/logging/logging.cpp

58 lines
1.2 KiB
C++

#include "logging.h"
#include <mutex>
#include "Entry.h"
#include "tools/tools.h"
using namespace logging;
using std::lock_guard;
using std::shared_ptr;
using std::string;
using std::vector;
std::mutex& getLogMutex() {
static std::mutex mutex;
return mutex;
}
vector<shared_ptr<Sink>>& getSinks() {
static vector<shared_ptr<Sink>> sinks;
return sinks;
}
bool logging::addSink(shared_ptr<Sink> sink) {
lock_guard<std::mutex> lock(getLogMutex());
auto& sinks = getSinks();
if (std::find(sinks.begin(), sinks.end(), sink) == sinks.end()) {
sinks.push_back(sink);
return true;
}
return false;
}
bool logging::removeSink(std::shared_ptr<Sink> sink) {
lock_guard<std::mutex> lock(getLogMutex());
auto& sinks = getSinks();
const auto it = std::find(sinks.begin(), sinks.end(), sink);
if (it != sinks.end()) {
sinks.erase(it);
return true;
}
return false;
}
void logging::log(const Entry& entry) {
lock_guard<std::mutex> lock(getLogMutex());
for (auto& sink : getSinks()) {
sink->receive(entry);
}
}
void logging::log(Level level, const string& message) {
const Entry entry = Entry(level, message);
log(entry);
}