Skip to content

Commit cb16d35

Browse files
author
Florent Peyrusse
committed
refs #1207 Added log wrapper functions to be able to see logs in unit tests output.
1 parent a0a176f commit cb16d35

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

C/common/include/log_utils.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#ifndef _LOG_UTILS_H
2+
#define _LOG_UTILS_H
3+
/*
4+
* Log utility functions
5+
*
6+
* Copyright (c) 2017-2018 OSisoft, LLC
7+
*
8+
* Released under the Apache 2.0 Licence
9+
*
10+
* Author: Mark Riddoch, Massimiliano Pinto
11+
*/
12+
13+
#include <string>
14+
#include <logger.h>
15+
16+
namespace LogUtils {
17+
/*
18+
* Log helper functions that will log both in the Fledge syslog file and in stdout for unit tests
19+
*/
20+
21+
template<class... Args>
22+
void log_debug(const std::string& format, Args&&... args) {
23+
#ifdef UNIT_TEST
24+
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
25+
fflush(stdout);
26+
#endif
27+
Logger::getLogger()->debug(format.c_str(), std::forward<Args>(args)...);
28+
}
29+
30+
template<class... Args>
31+
void log_info(const std::string& format, Args&&... args) {
32+
#ifdef UNIT_TEST
33+
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
34+
fflush(stdout);
35+
#endif
36+
Logger::getLogger()->info(format.c_str(), std::forward<Args>(args)...);
37+
}
38+
39+
template<class... Args>
40+
void log_warn(const std::string& format, Args&&... args) {
41+
#ifdef UNIT_TEST
42+
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
43+
fflush(stdout);
44+
#endif
45+
Logger::getLogger()->warn(format.c_str(), std::forward<Args>(args)...);
46+
}
47+
48+
template<class... Args>
49+
void log_error(const std::string& format, Args&&... args) {
50+
#ifdef UNIT_TEST
51+
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
52+
fflush(stdout);
53+
#endif
54+
Logger::getLogger()->error(format.c_str(), std::forward<Args>(args)...);
55+
}
56+
57+
template<class... Args>
58+
void log_fatal(const std::string& format, Args&&... args) {
59+
#ifdef UNIT_TEST
60+
printf(std::string(format).append("\n").c_str(), std::forward<Args>(args)...);
61+
fflush(stdout);
62+
#endif
63+
Logger::getLogger()->fatal(format.c_str(), std::forward<Args>(args)...);
64+
}
65+
}
66+
67+
#endif /* _LOG_UTILS_H */

tests/unit/C/common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ setup_target_for_coverage_gcovr_xml(
129129
DEPENDENCIES ${PROJECT_NAME}
130130
)
131131
132+
target_compile_definitions(${PROJECT_NAME} PRIVATE UNIT_TEST)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <gtest/gtest.h>
2+
3+
#include "log_utils.h"
4+
5+
TEST(LogUtilsTest, LogWrappers)
6+
{
7+
std::string text("This message is at level %s");
8+
ASSERT_NO_THROW(LogUtils::log_debug(text.c_str(), "debug"));
9+
ASSERT_NO_THROW(LogUtils::log_info(text.c_str(), "info"));
10+
ASSERT_NO_THROW(LogUtils::log_warn(text.c_str(), "warning"));
11+
ASSERT_NO_THROW(LogUtils::log_error(text.c_str(), "error"));
12+
ASSERT_NO_THROW(LogUtils::log_fatal(text.c_str(), "fatal"));
13+
}

0 commit comments

Comments
 (0)