Skip to content

Commit

Permalink
Kineto Logger's LoggerObservers to use set (#477)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #477

Use data structure set:
- Prevent any duplicate logger observer elements
- Elements are immutable
- Simplifies logic for adding and removing elements

Generally this list is very short, so iterating should not reduce performance.

Reviewed By: robieta

Differential Revision: D32706119

fbshipit-source-id: 21d1779035f524ae3b22aa6e49d5b6a2d9a9a7d7
  • Loading branch information
aaronenyeshi authored and facebook-github-bot committed Dec 2, 2021
1 parent ebde0dd commit 1db0936
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions libkineto/src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <mutex>
#include <set>
#include <time.h>

#include <fmt/chrono.h>
Expand All @@ -30,8 +30,8 @@ namespace KINETO_NAMESPACE {
std::atomic_int Logger::severityLevel_{VERBOSE};
std::atomic_int Logger::verboseLogLevel_{-1};
std::atomic<uint64_t> Logger::verboseLogModules_{~0ull};
static std::list<ILoggerObserver*>& LoggerObservers() {
static std::list<ILoggerObserver*> observers;
static std::set<ILoggerObserver*>& LoggerObservers() {
static std::set<ILoggerObserver*> observers;
return observers;
}
static std::mutex& mutex() {
Expand Down Expand Up @@ -85,16 +85,12 @@ void Logger::setVerboseLogModules(const std::vector<std::string>& modules) {

void Logger::addLoggerObserver(ILoggerObserver* observer) {
std::lock_guard<std::mutex> guard(mutex());
LoggerObservers().push_back(observer);
LoggerObservers().insert(observer);
}

void Logger::removeLoggerObserver(ILoggerObserver* observer) {
std::lock_guard<std::mutex> guard(mutex());
auto& LoggerObservers_ = LoggerObservers();
auto it = std::find(LoggerObservers_.begin(), LoggerObservers_.end(), observer);
if (it != LoggerObservers_.end()) {
LoggerObservers_.erase(it);
}
LoggerObservers().erase(observer);
}

} // namespace KINETO_NAMESPACE
Expand Down

0 comments on commit 1db0936

Please sign in to comment.