-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #1207 Added log wrapper functions to be able to see logs in unit…
… tests output. Signed-off-by: Florent Peyrusse <[email protected]>
- Loading branch information
Florent Peyrusse
committed
Oct 20, 2023
1 parent
a0a176f
commit e6ddb93
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} |