From 2be11874e3969f67de67e7b29419d699c64a84ff Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Tue, 26 Jan 2016 13:43:20 -0500 Subject: [PATCH] Improved unit test logging: Logging from the Application object in the Env is redirected to the suite::log, with the severity level set to error and above. --- src/ripple/basics/Log.h | 14 +++++-- src/ripple/basics/impl/Log.cpp | 23 ++++++++---- src/ripple/test/jtx/Env.h | 16 ++++---- src/ripple/test/jtx/impl/Env.cpp | 64 ++++++++++++++++++++++++++++++-- 4 files changed, 95 insertions(+), 22 deletions(-) diff --git a/src/ripple/basics/Log.h b/src/ripple/basics/Log.h index 0548cbd8c..8acf62f63 100644 --- a/src/ripple/basics/Log.h +++ b/src/ripple/basics/Log.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -145,7 +146,9 @@ private: }; std::mutex mutable mutex_; - std::map sinks_; + std::map , + beast::ci_less> sinks_; beast::Journal::Severity level_; File file_; bool silent_ = false; @@ -159,10 +162,10 @@ public: bool open (boost::filesystem::path const& pathToLogFile); - Sink& + beast::Journal::Sink& get (std::string const& name); - Sink& + beast::Journal::Sink& operator[] (std::string const& name); beast::Journal @@ -195,6 +198,11 @@ public: silent_ = bSilent; } + virtual + std::unique_ptr + makeSink(std::string const& partition, + beast::Journal::Severity startingLevel); + public: static LogSeverity diff --git a/src/ripple/basics/impl/Log.cpp b/src/ripple/basics/impl/Log.cpp index 426580bfd..9f84ad348 100644 --- a/src/ripple/basics/impl/Log.cpp +++ b/src/ripple/basics/impl/Log.cpp @@ -119,17 +119,16 @@ Logs::open (boost::filesystem::path const& pathToLogFile) return file_.open(pathToLogFile); } -Logs::Sink& +beast::Journal::Sink& Logs::get (std::string const& name) { std::lock_guard lock (mutex_); - auto const result (sinks_.emplace (std::piecewise_construct, - std::forward_as_tuple(name), std::forward_as_tuple (name, - level_, *this))); - return result.first->second; + auto const result = + sinks_.emplace(name, makeSink(name, level_)); + return *result.first->second; } -Logs::Sink& +beast::Journal::Sink& Logs::operator[] (std::string const& name) { return get(name); @@ -153,7 +152,7 @@ Logs::severity (beast::Journal::Severity level) std::lock_guard lock (mutex_); level_ = level; for (auto& sink : sinks_) - sink.second.severity (level); + sink.second->severity (level); } std::vector> @@ -164,7 +163,7 @@ Logs::partition_severities() const list.reserve (sinks_.size()); for (auto const& e : sinks_) list.push_back(std::make_pair(e.first, - toString(fromSeverity(e.second.severity())))); + toString(fromSeverity(e.second->severity())))); return list; } @@ -193,6 +192,14 @@ Logs::rotate() return "The log file could not be closed and reopened."; } +std::unique_ptr +Logs::makeSink(std::string const& name, + beast::Journal::Severity startingLevel) +{ + return std::make_unique( + name, startingLevel, *this); +} + LogSeverity Logs::fromSeverity (beast::Journal::Severity level) { diff --git a/src/ripple/test/jtx/Env.h b/src/ripple/test/jtx/Env.h index 99a70d361..8ff584bd3 100644 --- a/src/ripple/test/jtx/Env.h +++ b/src/ripple/test/jtx/Env.h @@ -101,8 +101,10 @@ private: std::thread thread; std::unique_ptr client; - AppBundle (std::unique_ptr config); - AppBundle (Application* app_); + AppBundle (beast::unit_test::suite& suite, + std::unique_ptr config); + AppBundle (beast::unit_test::suite& suite, + Application* app_); ~AppBundle(); }; @@ -139,11 +141,11 @@ public: // VFALCO Could wrap the suite::log in a Journal here template - Env (beast::unit_test::suite& test_, + Env (beast::unit_test::suite& suite_, std::unique_ptr config, Args&&... args) - : test (test_) - , bundle_ (std::move(config)) + : test (suite_) + , bundle_ (suite_, std::move(config)) { memoize(Account::master); Pathfinder::initPathTable(); @@ -151,9 +153,9 @@ public: } template - Env (beast::unit_test::suite& test_, + Env (beast::unit_test::suite& suite_, Args&&... args) - : Env(test_, []() + : Env(suite_, []() { auto p = std::make_unique(); setupConfigForUnitTests(*p); diff --git a/src/ripple/test/jtx/impl/Env.cpp b/src/ripple/test/jtx/impl/Env.cpp index f7aed9ece..c70f563c3 100644 --- a/src/ripple/test/jtx/impl/Env.cpp +++ b/src/ripple/test/jtx/impl/Env.cpp @@ -85,14 +85,69 @@ setupConfigForUnitTests (Config& cfg) namespace jtx { -Env::AppBundle::AppBundle(Application* app_) - : app (app_) +class SuiteSink : public beast::Journal::Sink +{ + std::string partition_; + beast::unit_test::suite& suite_; + +public: + SuiteSink(std::string const& partition, + beast::unit_test::suite& suite) + : partition_(partition + " ") + , suite_ (suite) + { + } + + void + write(beast::Journal::Severity level, + std::string const& text) override + { + std::string s; + switch(level) + { + case beast::Journal::kTrace: s = "TRC:"; break; + case beast::Journal::kDebug: s = "DBG:"; break; + case beast::Journal::kInfo: s = "INF:"; break; + case beast::Journal::kWarning: s = "WRN:"; break; + case beast::Journal::kError: s = "ERR:"; break; + default: + case beast::Journal::kFatal: s = "FTL:"; break; + } + suite_.log << s << partition_ << text; + } +}; + +class SuiteLogs : public Logs +{ + beast::unit_test::suite& suite_; + +public: + explicit + SuiteLogs(beast::unit_test::suite& suite) + : suite_(suite) + { + } + + std::unique_ptr + makeSink(std::string const& partition, + beast::Journal::Severity startingLevel) override + { + return std::make_unique(partition, suite_); + } +}; + +//------------------------------------------------------------------------------ + +Env::AppBundle::AppBundle(beast::unit_test::suite&, + Application* app_) + : app(app_) { } -Env::AppBundle::AppBundle(std::unique_ptr config) +Env::AppBundle::AppBundle(beast::unit_test::suite& suite, + std::unique_ptr config) { - auto logs = std::make_unique(); + auto logs = std::make_unique(suite); auto timeKeeper_ = std::make_unique(); timeKeeper = timeKeeper_.get(); @@ -101,6 +156,7 @@ Env::AppBundle::AppBundle(std::unique_ptr config) owned = make_Application(std::move(config), std::move(logs), std::move(timeKeeper_)); app = owned.get(); + app->logs().severity(beast::Journal::kError); app->setup(); timeKeeper->set( app->getLedgerMaster().getClosedLedger()->info().closeTime);