Skip to content

Commit

Permalink
Allow static libs
Browse files Browse the repository at this point in the history
- Remove SHARED option from add_library to allow static libs,
  CMake then respects the standard BUILD_SHARED_LIBS option.
- Use singleton LoggerImpl to avoid problems with static lib
  startup order.
  • Loading branch information
hefroy committed Oct 11, 2023
1 parent 89720d3 commit 46b983a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ link_directories(

file(GLOB CAPI_SRCS "src/CommonAPI/*.cpp")
list(SORT CAPI_SRCS)
add_library(CommonAPI SHARED ${CAPI_SRCS})
add_library(CommonAPI ${CAPI_SRCS})
target_link_libraries(CommonAPI PRIVATE ${DL_LIBRARY} ${DLT_LIBRARIES})
set_target_properties(CommonAPI PROPERTIES VERSION ${LIBCOMMONAPI_MAJOR_VERSION}.${LIBCOMMONAPI_MINOR_VERSION}.${LIBCOMMONAPI_PATCH_VERSION} SOVERSION ${LIBCOMMONAPI_MAJOR_VERSION}.${LIBCOMMONAPI_MINOR_VERSION}.${LIBCOMMONAPI_PATCH_VERSION} LINKER_LANGUAGE C)
set_target_properties (CommonAPI PROPERTIES INTERFACE_LINK_LIBRARY "")
Expand Down
2 changes: 1 addition & 1 deletion include/CommonAPI/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Logger {

private:
class LoggerImpl;
static std::unique_ptr<LoggerImpl> loggerImpl_;
static LoggerImpl &getInstance();

COMMONAPI_EXPORT static bool isLogged(Level _level);
COMMONAPI_EXPORT static void doLog(Level _level, const std::string& _message);
Expand Down
13 changes: 8 additions & 5 deletions src/CommonAPI/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,26 @@ class Logger::LoggerImpl {
#endif
};

std::unique_ptr<Logger::LoggerImpl> Logger::loggerImpl_ =
std::unique_ptr<Logger::LoggerImpl>(new Logger::LoggerImpl());
Logger::LoggerImpl &Logger::getInstance()
{
static LoggerImpl loggerImpl;
return loggerImpl;
}

Logger::Logger() = default;
Logger::~Logger() = default;

void Logger::init(bool _useConsole, const std::string &_fileName, bool _useDlt,
const std::string& _level) {
loggerImpl_->init(_useConsole, _fileName, _useDlt, _level);
getInstance().init(_useConsole, _fileName, _useDlt, _level);
}

bool Logger::isLogged(Level _level) {
return loggerImpl_->isLogged(_level);
return getInstance().isLogged(_level);
}

void Logger::doLog(Level _level, const std::string& _message) {
loggerImpl_->doLog(_level, _message);
getInstance().doLog(_level, _message);
}

} //namespace CommonAPI

0 comments on commit 46b983a

Please sign in to comment.