Invert bool logic to be error instead and check for default time point when trying to log

This commit is contained in:
wezrule 2019-02-26 09:17:50 +00:00
commit 12e86d7b36
2 changed files with 25 additions and 24 deletions

View file

@ -120,31 +120,30 @@ TEST (logger, changing_time_interval)
logging.init (path1); logging.init (path1);
logging.min_time_between_log_output = 0ms; logging.min_time_between_log_output = 0ms;
nano::logger_mt my_logger (logging.min_time_between_log_output); nano::logger_mt my_logger (logging.min_time_between_log_output);
auto success (my_logger.try_log ("logger.changing_time_interval1")); auto error (my_logger.try_log ("logger.changing_time_interval1"));
ASSERT_TRUE (success); ASSERT_FALSE (error);
logging.min_time_between_log_output = 20s; logging.min_time_between_log_output = 20s;
success = my_logger.try_log ("logger, changing_time_interval2"); error = my_logger.try_log ("logger, changing_time_interval2");
ASSERT_FALSE (success); ASSERT_TRUE (error);
} }
TEST (logger, try_log) TEST (logger, try_log)
{ {
auto path1 (nano::unique_path ()); auto path1 (nano::unique_path ());
nano::logger_mt my_logger (3ms);
std::stringstream ss; std::stringstream ss;
boost_log_cerr_redirect redirect_cerr (ss.rdbuf ()); boost_log_cerr_redirect redirect_cerr (ss.rdbuf ());
nano::logger_mt my_logger (3ms);
auto output1 = "logger.try_log1"; auto output1 = "logger.try_log1";
auto success (my_logger.try_log (output1)); auto error (my_logger.try_log (output1));
ASSERT_TRUE (success); ASSERT_FALSE (error);
auto output2 = "logger.try_log2"; auto output2 = "logger.try_log2";
success = my_logger.try_log (output2); error = my_logger.try_log (output2);
ASSERT_FALSE (success); // Fails as it is occuring too soon ASSERT_TRUE (error); // Fails as it is occuring too soon
// Sleep for a bit and then confirm // Sleep for a bit and then confirm
std::this_thread::sleep_for (3ms); std::this_thread::sleep_for (3ms);
success = my_logger.try_log (output2); error = my_logger.try_log (output2);
ASSERT_TRUE (success); ASSERT_FALSE (error);
std::string str; std::string str;
std::getline (ss, str, '\n'); std::getline (ss, str, '\n');
@ -160,13 +159,13 @@ TEST (logger, always_log)
boost_log_cerr_redirect redirect_cerr (ss.rdbuf ()); boost_log_cerr_redirect redirect_cerr (ss.rdbuf ());
nano::logger_mt my_logger (20s); // Make time interval effectively unreachable nano::logger_mt my_logger (20s); // Make time interval effectively unreachable
auto output1 = "logger.always_log1"; auto output1 = "logger.always_log1";
auto success (my_logger.try_log (output1)); auto error (my_logger.try_log (output1));
ASSERT_TRUE (success); ASSERT_FALSE (error);
// Time is too soon after, so it won't be logged // Time is too soon after, so it won't be logged
auto output2 = "logger.always_log2"; auto output2 = "logger.always_log2";
success = my_logger.try_log (output2); error = my_logger.try_log (output2);
ASSERT_FALSE (success); ASSERT_TRUE (error);
// Force it to be logged // Force it to be logged
my_logger.always_log (output2); my_logger.always_log (output2);

View file

@ -9,6 +9,8 @@
#define FATAL_LOG_PREFIX "FATAL ERROR: " #define FATAL_LOG_PREFIX "FATAL ERROR: "
using namespace std::chrono;
namespace nano namespace nano
{ {
// A wrapper around a boost logger object to allow // A wrapper around a boost logger object to allow
@ -67,20 +69,20 @@ public:
template <typename... LogItems> template <typename... LogItems>
bool try_log (LogItems &&... log_items) bool try_log (LogItems &&... log_items)
{ {
auto logged (false); auto error (true);
auto time_now = std::chrono::steady_clock::now (); auto time_now = steady_clock::now ();
if ((time_now - last_log_time) > min_log_delta) if (((time_now - last_log_time) > min_log_delta) || last_log_time == steady_clock::time_point{})
{ {
output (std::forward<LogItems> (log_items)...); output (std::forward<LogItems> (log_items)...);
last_log_time = time_now; last_log_time = time_now;
logged = true; error = false;
} }
return logged; return error;
} }
private: private:
std::chrono::milliseconds const & min_log_delta; milliseconds const & min_log_delta;
std::chrono::steady_clock::time_point last_log_time; steady_clock::time_point last_log_time;
boost::log::sources::logger_mt boost_logger_mt; boost::log::sources::logger_mt boost_logger_mt;
}; };
@ -132,7 +134,7 @@ public:
bool flush{ true }; bool flush{ true };
uintmax_t max_size{ 128 * 1024 * 1024 }; uintmax_t max_size{ 128 * 1024 * 1024 };
uintmax_t rotation_size{ 4 * 1024 * 1024 }; uintmax_t rotation_size{ 4 * 1024 * 1024 };
std::chrono::milliseconds min_time_between_log_output{ 5 }; milliseconds min_time_between_log_output{ 5 };
nano::logger_mt logger{ min_time_between_log_output }; nano::logger_mt logger{ min_time_between_log_output };
int json_version () const int json_version () const
{ {