|
| 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 */ |
0 commit comments