Added logging functions

This commit is contained in:
Daniel Wolf 2017-09-09 21:40:37 +02:00
parent 360f85df42
commit 1511f0a9e0
2 changed files with 31 additions and 5 deletions

View File

@ -19,15 +19,37 @@ vector<shared_ptr<Sink>>& getSinks() {
return sinks; return sinks;
} }
void logging::addSink(shared_ptr<Sink> sink) { bool logging::addSink(shared_ptr<Sink> sink) {
lock_guard<std::mutex> lock(getLogMutex()); lock_guard<std::mutex> lock(getLogMutex());
getSinks().push_back(sink);
auto& sinks = getSinks();
if (std::find(sinks.begin(), sinks.end(), sink) == sinks.end()) {
sinks.push_back(sink);
return true;
}
return false;
} }
void logging::log(Level level, const string& message) { 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()); lock_guard<std::mutex> lock(getLogMutex());
const Entry entry = Entry(level, message);
for (auto& sink : getSinks()) { for (auto& sink : getSinks()) {
sink->receive(entry); sink->receive(entry);
} }
} }
void logging::log(Level level, const string& message) {
const Entry entry = Entry(level, message);
log(entry);
}

View File

@ -6,7 +6,11 @@
namespace logging { namespace logging {
void addSink(std::shared_ptr<Sink> sink); bool addSink(std::shared_ptr<Sink> sink);
bool removeSink(std::shared_ptr<Sink> sink);
void log(const Entry& entry);
void log(Level level, const std::string& message); void log(Level level, const std::string& message);