From e6ddb932ff5563e6278c1068b5c1829bea5caf44 Mon Sep 17 00:00:00 2001 From: Florent Peyrusse Date: Fri, 20 Oct 2023 13:56:53 +0200 Subject: [PATCH] refs #1207 Added log wrapper functions to be able to see logs in unit tests output. Signed-off-by: Florent Peyrusse --- C/common/include/log_utils.h | 67 ++++++++++++++++++++++++++ tests/unit/C/common/CMakeLists.txt | 1 + tests/unit/C/common/test_log_utils.cpp | 13 +++++ 3 files changed, 81 insertions(+) create mode 100644 C/common/include/log_utils.h create mode 100644 tests/unit/C/common/test_log_utils.cpp diff --git a/C/common/include/log_utils.h b/C/common/include/log_utils.h new file mode 100644 index 0000000000..cb8c350b0c --- /dev/null +++ b/C/common/include/log_utils.h @@ -0,0 +1,67 @@ +#ifndef _LOG_UTILS_H +#define _LOG_UTILS_H +/* + * Log utility functions + * + * Copyright (c) 2017-2018 OSisoft, LLC + * + * Released under the Apache 2.0 Licence + * + * Author: Mark Riddoch, Massimiliano Pinto + */ + +#include +#include + +namespace LogUtils { + /* + * Log helper functions that will log both in the Fledge syslog file and in stdout for unit tests + */ + + template + void log_debug(const std::string& format, Args&&... args) { + #ifdef UNIT_TEST + printf(std::string(format).append("\n").c_str(), std::forward(args)...); + fflush(stdout); + #endif + Logger::getLogger()->debug(format.c_str(), std::forward(args)...); + } + + template + void log_info(const std::string& format, Args&&... args) { + #ifdef UNIT_TEST + printf(std::string(format).append("\n").c_str(), std::forward(args)...); + fflush(stdout); + #endif + Logger::getLogger()->info(format.c_str(), std::forward(args)...); + } + + template + void log_warn(const std::string& format, Args&&... args) { + #ifdef UNIT_TEST + printf(std::string(format).append("\n").c_str(), std::forward(args)...); + fflush(stdout); + #endif + Logger::getLogger()->warn(format.c_str(), std::forward(args)...); + } + + template + void log_error(const std::string& format, Args&&... args) { + #ifdef UNIT_TEST + printf(std::string(format).append("\n").c_str(), std::forward(args)...); + fflush(stdout); + #endif + Logger::getLogger()->error(format.c_str(), std::forward(args)...); + } + + template + void log_fatal(const std::string& format, Args&&... args) { + #ifdef UNIT_TEST + printf(std::string(format).append("\n").c_str(), std::forward(args)...); + fflush(stdout); + #endif + Logger::getLogger()->fatal(format.c_str(), std::forward(args)...); + } +} + +#endif /* _LOG_UTILS_H */ diff --git a/tests/unit/C/common/CMakeLists.txt b/tests/unit/C/common/CMakeLists.txt index 3285063627..118a241898 100644 --- a/tests/unit/C/common/CMakeLists.txt +++ b/tests/unit/C/common/CMakeLists.txt @@ -129,3 +129,4 @@ setup_target_for_coverage_gcovr_xml( DEPENDENCIES ${PROJECT_NAME} ) +target_compile_definitions(${PROJECT_NAME} PRIVATE UNIT_TEST) \ No newline at end of file diff --git a/tests/unit/C/common/test_log_utils.cpp b/tests/unit/C/common/test_log_utils.cpp new file mode 100644 index 0000000000..ea965cfb44 --- /dev/null +++ b/tests/unit/C/common/test_log_utils.cpp @@ -0,0 +1,13 @@ +#include + +#include "log_utils.h" + +TEST(LogUtilsTest, LogWrappers) +{ + std::string text("This message is at level %s"); + ASSERT_NO_THROW(LogUtils::log_debug(text.c_str(), "debug")); + ASSERT_NO_THROW(LogUtils::log_info(text.c_str(), "info")); + ASSERT_NO_THROW(LogUtils::log_warn(text.c_str(), "warning")); + ASSERT_NO_THROW(LogUtils::log_error(text.c_str(), "error")); + ASSERT_NO_THROW(LogUtils::log_fatal(text.c_str(), "fatal")); +} \ No newline at end of file