Skip to content

Commit

Permalink
refs #1207 Added log wrapper functions to be able to see logs in unit…
Browse files Browse the repository at this point in the history
… tests output.

Signed-off-by: Florent Peyrusse <[email protected]>
  • Loading branch information
Florent Peyrusse committed Oct 20, 2023
1 parent a0a176f commit e6ddb93
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
67 changes: 67 additions & 0 deletions C/common/include/log_utils.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <logger.h>

namespace LogUtils {
/*
* Log helper functions that will log both in the Fledge syslog file and in stdout for unit tests
*/

template<class... Args>
void log_debug(const std::string& format, Args&&... args) {
#ifdef UNIT_TEST
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
fflush(stdout);
#endif
Logger::getLogger()->debug(format.c_str(), std::forward<Args>(args)...);
}

template<class... Args>
void log_info(const std::string& format, Args&&... args) {
#ifdef UNIT_TEST
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
fflush(stdout);
#endif
Logger::getLogger()->info(format.c_str(), std::forward<Args>(args)...);
}

template<class... Args>
void log_warn(const std::string& format, Args&&... args) {
#ifdef UNIT_TEST
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
fflush(stdout);
#endif
Logger::getLogger()->warn(format.c_str(), std::forward<Args>(args)...);
}

template<class... Args>
void log_error(const std::string& format, Args&&... args) {
#ifdef UNIT_TEST
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
fflush(stdout);
#endif
Logger::getLogger()->error(format.c_str(), std::forward<Args>(args)...);
}

template<class... Args>
void log_fatal(const std::string& format, Args&&... args) {
#ifdef UNIT_TEST
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
fflush(stdout);
#endif
Logger::getLogger()->fatal(format.c_str(), std::forward<Args>(args)...);
}
}

#endif /* _LOG_UTILS_H */
1 change: 1 addition & 0 deletions tests/unit/C/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ setup_target_for_coverage_gcovr_xml(
DEPENDENCIES ${PROJECT_NAME}
)

target_compile_definitions(${PROJECT_NAME} PRIVATE UNIT_TEST)
13 changes: 13 additions & 0 deletions tests/unit/C/common/test_log_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <gtest/gtest.h>

#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"));
}

0 comments on commit e6ddb93

Please sign in to comment.