Skip to content

Commit

Permalink
Moved logger implementation to cpp file
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierBejMen committed Jul 30, 2024
1 parent 8473423 commit 3652912
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 82 deletions.
19 changes: 3 additions & 16 deletions src/engine/source/api/test/src/unit/kvdb/kvdb_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ const std::string valueKeyD {fmt::format("{{\"keyDA\":\"{}\",\"keyDB\":{},\"keyD
const std::string rCommand {"dummy cmd"};
const std::string rOrigin {"Dummy org module"};

void Setup()
{
logging::testInit();
}

void TearDown() {}

class KVDBApiTest : public ::testing::Test
{
protected:
Expand All @@ -58,14 +51,12 @@ class KVDBApiTest : public ::testing::Test

void SetUp() override
{
::Setup();
logging::testInit();

spMetrics = std::make_shared<MetricsManager>();
kvdbManager = std::make_shared<MockKVDBManager>();
};

void TearDown() override { ::TearDown(); };

api::wpRequest getWRequest(const bool& mustBeLoaded)
{
// create request
Expand Down Expand Up @@ -1041,9 +1032,7 @@ template<typename T>
class DumpTest : public ::testing::TestWithParam<T>
{
protected:
void SetUp() override { ::Setup(); }

void TearDown() override { ::TearDown(); };
void SetUp() override { logging::testInit(); }
};

using DumpParameters = DumpTest<std::tuple<std::string, std::string>>;
Expand Down Expand Up @@ -1118,9 +1107,7 @@ template<typename T>
class SearchTest : public ::testing::TestWithParam<T>
{
protected:
void SetUp() override { ::Setup(); }

void TearDown() override { ::TearDown(); };
void SetUp() override { logging::testInit(); }
};

using SearchTestParameters = SearchTest<std::tuple<std::string, std::string>>;
Expand Down
1 change: 1 addition & 0 deletions src/engine/source/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ add_library(base STATIC
${SRC_DIR}/expression.cpp
${SRC_DIR}/parseEvent.cpp
${SRC_DIR}/json.cpp
${SRC_DIR}/logging.cpp
)
target_include_directories(base
PUBLIC
Expand Down
76 changes: 10 additions & 66 deletions src/engine/source/base/include/base/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,92 +166,36 @@ struct LoggingConfig
const uint32_t flushInterval {DEFAULT_LOG_FLUSH_INTERVAL}; ///< Flush interval in milliseconds.
const uint32_t dedicatedThreads {DEFAULT_LOG_THREADS}; ///< Number of dedicated threads.
const uint32_t queueSize {DEFAULT_LOG_THREADS_QUEUE_SIZE}; ///< Size of the log queue for dedicated threads.
bool truncate; ///< If true, the log file will be deleted for each start of the engine.
bool truncate {false}; ///< If true, the log file will be deleted for each start of the engine.
};

/**
* @brief Retrieves the default logger.
* @return Shared pointer to the default logger.
*/
inline std::shared_ptr<spdlog::logger> getDefaultLogger()
{
auto logger = spdlog::get("default");
if (!logger)
{
throw std::runtime_error("The 'default' logger is not initialized.");
}

return logger;
}
std::shared_ptr<spdlog::logger> getDefaultLogger();

/**
* @brief Sets the log level.
* @param levelStr The log level as a string.
*/
inline void setLevel(Level level)
{
getDefaultLogger()->set_level(SEVERITY_LEVEL.at(level));

if (level <= Level::Debug)
{
getDefaultLogger()->set_pattern(LOG_DEBUG_HEADER);
}
else
{
getDefaultLogger()->set_pattern(DEFAULT_LOG_HEADER);
}
}
void setLevel(Level level);

/**
* @brief Starts logging with the given configuration.
* @param cfg Logging configuration parameters.
*/
inline void start(const LoggingConfig& cfg)
{
std::shared_ptr<spdlog::logger> logger;

if (0 < cfg.dedicatedThreads)
{
spdlog::init_thread_pool(cfg.queueSize, cfg.dedicatedThreads);
}

if (cfg.filePath == STD_ERR_PATH)
{
logger = spdlog::stderr_color_mt("default");
}
else if (cfg.filePath == STD_OUT_PATH)
{
logger = spdlog::stdout_color_mt("default");
}
else
{
logger = spdlog::basic_logger_mt("default", cfg.filePath, cfg.truncate);
}

setLevel(cfg.level);
logger->flush_on(spdlog::level::trace);
}
void start(const LoggingConfig& cfg);

/**
* @brief Stops logging.
*/
inline void stop()
{
spdlog::shutdown();
}

inline void testInit()
{
static bool initialized = false;
void stop();

if (!initialized)
{
LoggingConfig logConfig;
logConfig.level = Level::Off;
start(logConfig);
initialized = true;
}
}
/**
* @brief Initializes the logger for testing purposes.
*/
void testInit();

} // namespace logging

Expand All @@ -274,4 +218,4 @@ inline void testInit()
logging::getDefaultLogger()->log( \
spdlog::source_loc {__FILE__, __LINE__, SPDLOG_FUNCTION}, spdlog::level::critical, msg, ##__VA_ARGS__)

#endif
#endif // _H_LOGGING
85 changes: 85 additions & 0 deletions src/engine/source/base/src/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Implementation of the logging module needs to be defined in a cpp file, as spdlog uses static variables that need to
* be defined only once when included in multiple translation units. This issue occurs when the old logging.hpp is moved
* to a static library that is linked in multiple libraries, leading to multiple definitions of static variables if not
* properly managed.
*
* See: https://github.com/gabime/spdlog/issues/1658#issuecomment-681193558
*
*/

#include <base/logging.hpp>

namespace logging
{

std::shared_ptr<spdlog::logger> getDefaultLogger()
{
auto logger = spdlog::get("default");
if (!logger)
{
throw std::runtime_error("The 'default' logger is not initialized.");
}

return logger;
}

void setLevel(Level level)
{
getDefaultLogger()->set_level(SEVERITY_LEVEL.at(level));

if (level <= Level::Debug)
{
getDefaultLogger()->set_pattern(LOG_DEBUG_HEADER);
}
else
{
getDefaultLogger()->set_pattern(DEFAULT_LOG_HEADER);
}
}

void start(const LoggingConfig& cfg)
{
std::shared_ptr<spdlog::logger> logger;

if (0 < cfg.dedicatedThreads)
{
spdlog::init_thread_pool(cfg.queueSize, cfg.dedicatedThreads);
}

if (cfg.filePath == STD_ERR_PATH)
{
logger = spdlog::stderr_color_mt("default");
}
else if (cfg.filePath == STD_OUT_PATH)
{
logger = spdlog::stdout_color_mt("default");
}
else
{
logger = spdlog::basic_logger_mt("default", cfg.filePath, cfg.truncate);
}

setLevel(cfg.level);

logger->flush_on(spdlog::level::trace);
}

void stop()
{
spdlog::shutdown();
}

void testInit()
{
auto logger = spdlog::get("default");

if (!logger)
{
LoggingConfig logConfig;
logConfig.level = Level::Warn;
start(logConfig);
}
}

} // namespace logging

0 comments on commit 3652912

Please sign in to comment.