From df3204874d113781136ff15469e8450f8b38bd20 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 19 Dec 2013 08:30:17 +0000 Subject: [PATCH] clean up entry::print --- include/libtorrent/entry.hpp | 11 ++++--- src/entry.cpp | 56 ++++++++++++++++++++++-------------- test/test_rss.cpp | 4 +-- test/test_storage.cpp | 16 +++++------ 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index f85718082..a3a9c0eb3 100644 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -228,10 +228,7 @@ namespace libtorrent entry* find_key(std::string const& key); entry const* find_key(std::string const& key) const; - // TODO: could this be removed? -#if (defined TORRENT_VERBOSE_LOGGING || defined TORRENT_DEBUG) && TORRENT_USE_IOSTREAM - void print(std::ostream& os, int indent = 0) const; -#endif + std::string to_string() const; protected: @@ -241,6 +238,8 @@ namespace libtorrent private: + void to_string_impl(std::string& out, int indent) const; + #if (defined(_MSC_VER) && _MSC_VER < 1310) || TORRENT_COMPLETE_TYPES_REQUIRED // workaround for msvc-bug. // assumes sizeof(map) == sizeof(map) @@ -279,10 +278,10 @@ namespace libtorrent mutable boost::uint8_t m_type_queried:1; }; -#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM +#if TORRENT_USE_IOSTREAM inline std::ostream& operator<<(std::ostream& os, const entry& e) { - e.print(os, 0); + os << e.to_string(); return os; } #endif diff --git a/src/entry.cpp b/src/entry.cpp index b47cadcab..00a935478 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -33,8 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#if (defined TORRENT_VERBOSE_LOGGING || defined TORRENT_DEBUG) && TORRENT_USE_IOSTREAM -#include +#if TORRENT_USE_IOSTREAM #include #endif #include @@ -42,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" #include "libtorrent/escape_string.hpp" #include "libtorrent/lazy_entry.hpp" +#include "libtorrent/escape_string.hpp" #if defined(_MSC_VER) #define for if (false) {} else for @@ -495,15 +495,22 @@ namespace libtorrent TORRENT_ASSERT(false); } -#if (defined TORRENT_VERBOSE_LOGGING || defined TORRENT_DEBUG) && TORRENT_USE_IOSTREAM - void entry::print(std::ostream& os, int indent) const + std::string entry::to_string() const + { + std::string ret; + to_string_impl(ret, 0); + return ret; + } + + void entry::to_string_impl(std::string& out, int indent) const { TORRENT_ASSERT(indent >= 0); - for (int i = 0; i < indent; ++i) os << " "; + for (int i = 0; i < indent; ++i) out += " "; switch (m_type) { case int_t: - os << integer() << "\n"; + out += libtorrent::to_string(integer()).elems; + out += "\n"; break; case string_t: { @@ -516,20 +523,28 @@ namespace libtorrent break; } } - if (binary_string) os << to_hex(string()) << "\n"; - else os << string() << "\n"; + if (binary_string) + { + out += to_hex(string()); + out += "\n"; + } + else + { + out += string(); + out += "\n"; + } } break; case list_t: { - os << "list\n"; + out += "list\n"; for (list_type::const_iterator i = list().begin(); i != list().end(); ++i) { - i->print(os, indent+1); + i->to_string_impl(out, indent+1); } } break; case dictionary_t: { - os << "dictionary\n"; + out += "dictionary\n"; for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i) { bool binary_string = false; @@ -541,23 +556,22 @@ namespace libtorrent break; } } - for (int j = 0; j < indent+1; ++j) os << " "; - os << "["; - if (binary_string) os << to_hex(i->first); - else os << i->first; - os << "]"; + for (int j = 0; j < indent+1; ++j) out += " "; + out += "["; + if (binary_string) out += to_hex(i->first); + else out += i->first; + out += "]"; if (i->second.type() != entry::string_t && i->second.type() != entry::int_t) - os << "\n"; - else os << " "; - i->second.print(os, indent+2); + out += "\n"; + else out += " "; + i->second.to_string_impl(out, indent+2); } } break; default: - os << "\n"; + out += "\n"; } } -#endif } diff --git a/test/test_rss.cpp b/test/test_rss.cpp index 43eb1ecf5..20ed242eb 100644 --- a/test/test_rss.cpp +++ b/test/test_rss.cpp @@ -125,9 +125,7 @@ void test_feed(std::string const& filename, rss_expect const& expect) f->save_state(state); fprintf(stderr, "feed_state:\n"); -#ifdef TORRENT_DEBUG - state.print(std::cerr); -#endif + std::cerr << state.to_string() << "\n"; // TODO: verify some key state is saved in 'state' } diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 4129c1d99..7995e7b0e 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -979,9 +979,8 @@ void test_fastresume(std::string const& test_path) TEST_CHECK(!exists(combine_path(test_path, combine_path("tmp1", "temporary")))); if (exists(combine_path(test_path, combine_path("tmp1", "temporary")))) return; -#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM - resume.print(std::cout); -#endif + + std::cerr << resume.to_string() << "\n"; // make sure the fast resume check fails! since we removed the file { @@ -1071,9 +1070,8 @@ void test_rename_file_in_fastresume(std::string const& test_path) TEST_CHECK(!exists(combine_path(test_path, "tmp2/temporary"))); TEST_CHECK(exists(combine_path(test_path, "tmp2/testing_renamed_files"))); TEST_CHECK(resume.dict().find("mapped_files") != resume.dict().end()); -#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM - resume.print(std::cout); -#endif + + std::cerr << resume.to_string() << "\n"; // make sure the fast resume check succeeds, even though we renamed the file { @@ -1105,9 +1103,9 @@ void test_rename_file_in_fastresume(std::string const& test_path) ses.remove_torrent(h); } TEST_CHECK(resume.dict().find("mapped_files") != resume.dict().end()); -#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM - resume.print(std::cout); -#endif + + std::cerr << resume.to_string() << "\n"; + remove_all(combine_path(test_path, "tmp2"), ec); if (ec) std::cerr << "remove_all '" << combine_path(test_path, "tmp2") << "': " << ec.message() << std::endl;