Skip to content

Commit

Permalink
Rename LoggerSingleton to LoggerImpl
Browse files Browse the repository at this point in the history
ref #4847
  • Loading branch information
kbenne authored and jmarrec committed Mar 22, 2024
1 parent aad08ee commit 69c474b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/utilities/core/LogSink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class UTILITIES_API LogSink
void setFormatter(const boost::log::formatter& fmter);

protected:
friend class LoggerSingleton;
friend class LoggerImpl;

// does not register in the global logger
LogSink();
Expand Down
20 changes: 10 additions & 10 deletions src/utilities/core/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace keywords = boost::log::keywords;

namespace openstudio {

std::shared_ptr<LoggerSingleton> Logger::obj = nullptr;
std::shared_ptr<LoggerImpl> Logger::obj = nullptr;

/// convenience function for SWIG, prefer macros in C++
void logFree(LogLevel level, const std::string& channel, const std::string& message) {
BOOST_LOG_SEV(openstudio::Logger::instance().loggerFromChannel(channel), level) << message;
}

LoggerSingleton::LoggerSingleton() {
LoggerImpl::LoggerImpl() {
// Make current thread id attribute available to logging
boost::log::core::get()->add_global_attribute("ThreadId", boost::log::attributes::make_function(&std::this_thread::get_id));

Expand All @@ -40,24 +40,24 @@ LoggerSingleton::LoggerSingleton() {
//this->addSink(m_standardErrLogger.sink());
}

LoggerSingleton::~LoggerSingleton() {
LoggerImpl::~LoggerImpl() {
// unregister Qt message handler
//qInstallMsgHandler(consoleLogQtMessage);
}

LogSink LoggerSingleton::standardOutLogger() const {
LogSink LoggerImpl::standardOutLogger() const {
std::shared_lock l{m_mutex};

return m_standardOutLogger;
}

LogSink LoggerSingleton::standardErrLogger() const {
LogSink LoggerImpl::standardErrLogger() const {
std::shared_lock l{m_mutex};

return m_standardErrLogger;
}

LoggerType& LoggerSingleton::loggerFromChannel(const LogChannel& logChannel) {
LoggerType& LoggerImpl::loggerFromChannel(const LogChannel& logChannel) {
std::shared_lock l{m_mutex};

auto it = m_loggerMap.find(logChannel);
Expand All @@ -80,15 +80,15 @@ LoggerType& LoggerSingleton::loggerFromChannel(const LogChannel& logChannel) {
return it->second;
}

bool LoggerSingleton::findSink(boost::shared_ptr<LogSinkBackend> sink) {
bool LoggerImpl::findSink(boost::shared_ptr<LogSinkBackend> sink) {
std::unique_lock l{m_mutex};

auto it = m_sinks.find(sink);

return (it != m_sinks.end());
}

void LoggerSingleton::addSink(boost::shared_ptr<LogSinkBackend> sink) {
void LoggerImpl::addSink(boost::shared_ptr<LogSinkBackend> sink) {
std::shared_lock l{m_mutex};

auto it = m_sinks.find(sink);
Expand All @@ -106,7 +106,7 @@ void LoggerSingleton::addSink(boost::shared_ptr<LogSinkBackend> sink) {
}
}

void LoggerSingleton::removeSink(boost::shared_ptr<LogSinkBackend> sink) {
void LoggerImpl::removeSink(boost::shared_ptr<LogSinkBackend> sink) {
std::shared_lock l{m_mutex};

auto it = m_sinks.find(sink);
Expand All @@ -124,7 +124,7 @@ void LoggerSingleton::removeSink(boost::shared_ptr<LogSinkBackend> sink) {
}
}

void LoggerSingleton::addTimeStampToLogger() {
void LoggerImpl::addTimeStampToLogger() {
std::unique_lock l{m_mutex};

// Add a TimeStamp attribute, same as boost::log::add_common_attributes() would do
Expand Down
31 changes: 9 additions & 22 deletions src/utilities/core/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
#include <shared_mutex>

/// defines method logChannel() to get a logger for a class
#define REGISTER_LOGGER(__logChannel__) \
static openstudio::LogChannel logChannel() { \
return __logChannel__; \
}
#define REGISTER_LOGGER(__logChannel__) \
static openstudio::LogChannel logChannel() { return __logChannel__; }

/// log a message from within a registered class
#define LOG(__level__, __message__) LOG_FREE(__level__, logChannel(), __message__);
Expand Down Expand Up @@ -61,13 +59,13 @@ class Logger;

/** Primary logging class.
*/
class UTILITIES_API LoggerSingleton
class UTILITIES_API LoggerImpl
{
friend class Logger;

public:
/// destructor, cleans up, writes xml file footers, etc
~LoggerSingleton();
~LoggerImpl();

/// get logger for standard out
LogSink standardOutLogger() const;
Expand Down Expand Up @@ -97,7 +95,7 @@ class UTILITIES_API LoggerSingleton

private:
/// private constructor
LoggerSingleton();
LoggerImpl();

mutable std::shared_mutex m_mutex;

Expand All @@ -116,31 +114,20 @@ class UTILITIES_API LoggerSingleton
SinkSetType m_sinks;
};

/** Surprisingly, LoggerSingleton is not a singleton.
* Historically, Logger
* was a template instantiation of openstudio::Singleton<LoggerSingleton>,
* but the template was found to result in a singleton that was not global
* across the ScriptEngine DLL boundary (bug on Windows platforms). Logger is a non-template version
* that is equivalent to openstudio::Singleton<LoggerSingleton>, but this version
* is global across the ScriptEngine interface. For compatibility reasons,
* Logger and LoggerSingleton are functionally equivalent to their original
* implementations, despite the somewhat surprising names.
*/
class UTILITIES_API Logger
{
public:
Logger() = delete;

static LoggerSingleton& instance() {
if (! obj) {
obj = std::shared_ptr<LoggerSingleton>(new LoggerSingleton());
static LoggerImpl& instance() {
if (!obj) {
obj = std::shared_ptr<LoggerImpl>(new LoggerImpl());
}
return *obj;
}

private:

static std::shared_ptr<LoggerSingleton> obj;
static std::shared_ptr<LoggerImpl> obj;
};

} // namespace openstudio
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/core/Logger.i
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

%ignore std::vector<openstudio::LogMessage>::vector(size_type);
%ignore std::vector<openstudio::LogMessage>::resize(size_type);
%ignore openstudio::LoggerSingleton::loggerFromChannel;
%ignore openstudio::LoggerImpl::loggerFromChannel;
%ignore openstudio::LogSink::setFormatter;

%template(LogMessageVector) std::vector<openstudio::LogMessage>;
Expand Down

0 comments on commit 69c474b

Please sign in to comment.