Skip to content

Commit

Permalink
Merge pull request #104 from ecmwf/feature/thread-safe-debug
Browse files Browse the repository at this point in the history
Looks good to me. Thanks @simondsmart and @geier1993
  • Loading branch information
danovaro authored Jun 13, 2024
2 parents 149b9f7 + 1f072b3 commit 3ee79af
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
7 changes: 4 additions & 3 deletions src/eckit/log/Log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ struct CreateDebugChannel : public CreateLogChannel {
};

Channel& Log::debug() {
if (!Main::ready()) {


if (!Main::ready()) {

const char* e = getenv("DEBUG");

if (e && bool(Translator<std::string, bool>()(e))) {
Expand All @@ -187,8 +188,8 @@ Channel& Log::debug() {
}

if (!Main::instance().debug_) {
static Channel empty;
return empty;
static ThreadSingleton<Channel> empty;
return empty.instance();
}


Expand Down
30 changes: 14 additions & 16 deletions src/eckit/system/Library.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
#include "eckit/system/SystemInfo.h"
#include "eckit/thread/AutoLock.h"
#include "eckit/thread/Mutex.h"
#include "eckit/thread/ThreadSingleton.h"
#include "eckit/utils/Translator.h"

namespace eckit::system {

//----------------------------------------------------------------------------------------------------------------------

Library::Library(const std::string& name) :
name_(name), prefix_(name), debug_(false) {
Library::Library(const std::string& name) : name_(name), prefix_(name), debug_(false) {

LibraryManager::enregister(name, this);

Expand Down Expand Up @@ -109,25 +109,22 @@ std::string Library::libraryPath() const {
return libraryPath_;
}

Channel& Library::debugChannel() const {
AutoLock<Mutex> lock(mutex_);

if (debugChannel_) {
return *debugChannel_;
}

std::string s = prefix_ + "_DEBUG";
Channel& Library::debugChannel() const {
static ThreadSingleton<std::map<const Library*, std::unique_ptr<Channel>>> debugChannels;

if (debug_) {
debugChannel_.reset(new Channel(new PrefixTarget(s)));
}
else {
debugChannel_.reset(new Channel());
auto it = debugChannels.instance().find(this);
if (it != debugChannels.instance().end()) {
return *it->second;
}

return *debugChannel_;
return *debugChannels.instance()
.emplace(this, debug_ ? std::make_unique<Channel>(new PrefixTarget(prefix_ + "_DEBUG")) //
: std::make_unique<Channel>()) //
.first->second.get();
}


const Configuration& Library::configuration() const {
AutoLock<Mutex> lock(mutex_);

Expand All @@ -142,7 +139,8 @@ const Configuration& Library::configuration() const {

Log::debug() << "Parsing Lib " << name_ << " config file " << cfgpath << std::endl;

eckit::Configuration* cfg = cfgpath.exists() ? new eckit::YAMLConfiguration(cfgpath) : new eckit::YAMLConfiguration(std::string(""));
eckit::Configuration* cfg
= cfgpath.exists() ? new eckit::YAMLConfiguration(cfgpath) : new eckit::YAMLConfiguration(std::string(""));

Log::debug() << "Lib " << name_ << " configuration: " << *cfg << std::endl;

Expand Down
2 changes: 0 additions & 2 deletions src/eckit/system/Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ class Library : private eckit::NonCopyable {
mutable std::string libraryPath_;
mutable std::string prefixDirectory_;

mutable std::unique_ptr<eckit::Channel> debugChannel_;

mutable std::unique_ptr<eckit::Configuration> configuration_;
};

Expand Down

0 comments on commit 3ee79af

Please sign in to comment.