| 1 |
#include <pbcopper/logging/Logging.h> |
| 2 |
|
| 3 |
#include <sstream> |
| 4 |
#include <string> |
| 5 |
|
| 6 |
#include <boost/algorithm/string/predicate.hpp> |
| 7 |
|
| 8 |
#include <gtest/gtest.h> |
| 9 |
|
| 10 |
#include <pbcopper/utility/StringUtils.h> |
| 11 |
|
| 12 |
using namespace PacBio; |
| 13 |
|
| 14 |
// |
| 15 |
// NOTE: We need to scope the Loggers used here and invoke the macros that take |
| 16 |
// an explicit logger (rather than the convenience, default |
| 17 |
// singleton-logger macros). Essentially, the message queue is not |
| 18 |
// guaranteed to be processed until the logger goes out of scope. That's |
| 19 |
// usually at program exit, and no problem for production applications... |
| 20 |
// but we need finer control for testing here. |
| 21 |
// |
| 22 |
|
| 23 |
namespace LoggingTests { |
| 24 |
|
| 25 |
static const std::string debugMsg{"*** Application DEBUG ***"}; |
| 26 |
static const std::string verboseMsg{"*** Application VERBOSE ***"}; |
| 27 |
static const std::string infoMsg{"*** Application INFO ***"}; |
| 28 |
static const std::string warnMsg{"*** Application WARNING ***"}; |
| 29 |
|
| 30 |
} // namespace LoggingTests |
| 31 |
|
| 32 |
TEST(Logging_LogLevel, can_be_constructed_from_string) |
| 33 |
{ |
| 34 |
const Logging::LogLevel info{"INFO"}; |
| 35 |
EXPECT_EQ(Logging::LogLevel::INFO, info); |
| 36 |
|
| 37 |
const Logging::LogLevel warn{"WARN"}; |
| 38 |
EXPECT_EQ(Logging::LogLevel::WARN, warn); |
| 39 |
|
| 40 |
EXPECT_THROW(Logging::LogLevel(""), std::invalid_argument); |
| 41 |
EXPECT_THROW(Logging::LogLevel("bad"), std::invalid_argument); |
| 42 |
} |
| 43 |
|
| 44 |
TEST(Logging_LogLevel, can_be_converted_to_string) |
| 45 |
{ |
| 46 |
const Logging::LogLevel info{Logging::LogLevel::INFO}; |
| 47 |
const Logging::LogLevel warn{Logging::LogLevel::WARN}; |
| 48 |
|
| 49 |
EXPECT_EQ("INFO", info.ToString()); |
| 50 |
EXPECT_EQ("WARN", warn.ToString()); |
| 51 |
} |
| 52 |
|
| 53 |
TEST(Logging_Logger, writes_normal_info_message_to_output_stream) |
| 54 |
{ |
| 55 |
std::ostringstream s; |
| 56 |
{ |
| 57 |
Logging::Logger logger{s, Logging::LogLevel::INFO}; |
| 58 |
PBLOGGER_INFO(logger) << LoggingTests::infoMsg; |
| 59 |
PBLOGGER_VERBOSE(logger) << LoggingTests::verboseMsg; |
| 60 |
} |
| 61 |
|
| 62 |
const std::string result = s.str(); |
| 63 |
EXPECT_NE(std::string::npos, result.find(LoggingTests::infoMsg)); |
| 64 |
EXPECT_EQ(std::string::npos, result.find(LoggingTests::verboseMsg)); // not written |
| 65 |
} |
| 66 |
|
| 67 |
TEST(Logging_Logger, writes_normal_verbose_message_to_output_stream) |
| 68 |
{ |
| 69 |
std::ostringstream s; |
| 70 |
{ |
| 71 |
Logging::Logger logger{s, Logging::LogLevel::VERBOSE}; |
| 72 |
PBLOGGER_VERBOSE(logger) << LoggingTests::verboseMsg; |
| 73 |
} |
| 74 |
|
| 75 |
const std::string result = s.str(); |
| 76 |
EXPECT_NE(std::string::npos, result.find(LoggingTests::verboseMsg)); |
| 77 |
} |
| 78 |
|
| 79 |
TEST(Logging_Logger, ignores_lower_level_messages) |
| 80 |
{ |
| 81 |
std::ostringstream s; |
| 82 |
{ |
| 83 |
Logging::Logger logger{s, Logging::LogLevel::INFO}; |
| 84 |
PBLOGGER_DEBUG(logger) << LoggingTests::debugMsg; |
| 85 |
PBLOGGER_VERBOSE(logger) << LoggingTests::verboseMsg; |
| 86 |
} |
| 87 |
EXPECT_TRUE(s.str().empty()); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(Logging_Logger, can_write_message_only_custom_field_config) |
| 91 |
{ |
| 92 |
{ |
| 93 |
std::ostringstream s; |
| 94 |
{ |
| 95 |
Logging::LogConfig config; |
| 96 |
config.Fields = Logging::LogField::NONE; |
| 97 |
|
| 98 |
Logging::Logger logger(s, config); |
| 99 |
PBLOGGER_WARN(logger) << LoggingTests::warnMsg; |
| 100 |
} |
| 101 |
EXPECT_EQ(">|> *** Application WARNING ***\n", s.str()); |
| 102 |
} |
| 103 |
{ |
| 104 |
std::ostringstream s; |
| 105 |
{ |
| 106 |
Logging::LogConfig config; |
| 107 |
config.Fields = Logging::LogField::LOG_LEVEL; |
| 108 |
|
| 109 |
Logging::Logger logger(s, config); |
| 110 |
PBLOGGER_WARN(logger) << LoggingTests::warnMsg; |
| 111 |
} |
| 112 |
EXPECT_EQ(">|> WARN -|- *** Application WARNING ***\n", s.str()); |
| 113 |
} |
| 114 |
{ |
| 115 |
std::ostringstream s; |
| 116 |
{ |
| 117 |
Logging::LogConfig config; |
| 118 |
config.Fields = Logging::LogField::LOG_LEVEL | Logging::LogField::THREAD_ID; |
| 119 |
|
| 120 |
Logging::Logger logger(s, config); |
| 121 |
PBLOGGER_ERROR(logger) << "Message"; |
| 122 |
} |
| 123 |
EXPECT_NE(std::string::npos, s.str().find("ERROR")); |
| 124 |
EXPECT_NE(std::string::npos, s.str().find("Message")); |
| 125 |
EXPECT_NE(std::string::npos, s.str().find("0x")); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
TEST(Logging_Logger, can_use_custom_delimiter) |
| 130 |
{ |
| 131 |
std::ostringstream s; |
| 132 |
{ |
| 133 |
Logging::LogConfig config; |
| 134 |
config.Fields = Logging::LogField::LOG_LEVEL; |
| 135 |
config.Delimiter = " :: "; |
| 136 |
|
| 137 |
Logging::Logger logger(s, config); |
| 138 |
PBLOGGER_WARN(logger) << LoggingTests::warnMsg; |
| 139 |
} |
| 140 |
EXPECT_EQ(">|> WARN :: *** Application WARNING ***\n", s.str()); |
| 141 |
} |
| 142 |
|
| 143 |
TEST(Logging_Logger, trace_level_is_a_noop_in_release_mode) |
| 144 |
{ |
| 145 |
std::ostringstream s; |
| 146 |
int x = 0; |
| 147 |
{ |
| 148 |
const Logging::LogConfig config{Logging::LogLevel::TRACE}; |
| 149 |
Logging::Logger logger(s, config); |
| 150 |
|
| 151 |
auto incremented = [](int& y) { |
| 152 |
++y; |
| 153 |
return std::to_string(y); // just something to feed the logger |
| 154 |
}; |
| 155 |
|
| 156 |
PBLOGGER_TRACE(logger) << incremented(x) << incremented(x); |
| 157 |
PBLOGGER_TRACE(logger) << incremented(x) << incremented(x); |
| 158 |
} |
| 159 |
|
| 160 |
#ifdef NDEBUG |
| 161 |
EXPECT_EQ(0, x); |
| 162 |
#else |
| 163 |
EXPECT_EQ(4, x); |
| 164 |
#endif // NDEBUG |
| 165 |
} |
| 166 |
|
| 167 |
TEST(Logging_Logging, can_query_active_log_level) |
| 168 |
{ |
| 169 |
{ |
| 170 |
Logging::Logger logger{std::cerr, Logging::LogLevel::INFO}; |
| 171 |
Logging::Logger::Current(&logger); |
| 172 |
|
| 173 |
EXPECT_EQ(Logging::LogLevel::INFO, Logging::CurrentLogLevel()); |
| 174 |
|
| 175 |
// reset default logging |
| 176 |
Logging::Logger::Current(&Logging::Logger::Default()); |
| 177 |
} |
| 178 |
{ |
| 179 |
Logging::Logger logger{std::cerr, Logging::LogLevel::DEBUG}; |
| 180 |
Logging::Logger::Current(&logger); |
| 181 |
|
| 182 |
EXPECT_EQ(Logging::LogLevel::DEBUG, Logging::CurrentLogLevel()); |
| 183 |
|
| 184 |
// reset default logging |
| 185 |
Logging::Logger::Current(&Logging::Logger::Default()); |
| 186 |
} |
| 187 |
} |