Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
}