Skip to content

Commit

Permalink
(maint) Extend logging tests
Browse files Browse the repository at this point in the history
Extends testing to cover `operator>>` and `get_level`, completing line
coverage of the logging module.

Fixes a mistake in `operator>>` where we couldn't round-trip values,
because it didn't handle uppercase strings. Also provides more detailed
exception messages.
  • Loading branch information
Michael Smith committed Jul 13, 2015
1 parent 9923583 commit 78eddf4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion logging/src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/log/sinks/basic_sink_backend.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/algorithm/string.hpp>

#pragma GCC diagnostic pop

Expand Down Expand Up @@ -156,6 +157,7 @@ namespace leatherman { namespace logging {
{
string value;
if (in >> value) {
boost::algorithm::to_lower(value);
if (value == "none") {
level = log_level::none;
return in;
Expand Down Expand Up @@ -185,7 +187,7 @@ namespace leatherman { namespace logging {
return in;
}
}
throw runtime_error("invalid log level: expected none, trace, debug, info, warn, error, or fatal.");
throw runtime_error((boost::format("invalid log level '%1%': expected none, trace, debug, info, warn, error, or fatal.") % level).str());
}

ostream& operator<<(ostream& strm, log_level level)
Expand Down
8 changes: 8 additions & 0 deletions logging/tests/logging_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ struct custom_log_appender :
{
void consume(boost::log::record_view const& rec, string_type const& message)
{
auto lvl = boost::log::extract<log_level>("Severity", rec);
stringstream s;
s << boost::log::extract<log_level>("Severity", rec);

log_level read_lvl;
s >> read_lvl;
REQUIRE(read_lvl == lvl);

_level = s.str();
_message = message;
}
Expand All @@ -30,6 +36,7 @@ struct logging_test_context
logging_test_context(log_level lvl = log_level::trace)
{
set_level(lvl);
REQUIRE(get_level() == lvl);
clear_error_logged_flag();

colorize(_color, lvl);
Expand All @@ -45,6 +52,7 @@ struct logging_test_context
~logging_test_context()
{
set_level(log_level::none);
REQUIRE(get_level() == log_level::none);
on_message(nullptr);
clear_error_logged_flag();

Expand Down

0 comments on commit 78eddf4

Please sign in to comment.