clean up entry::print

This commit is contained in:
Arvid Norberg 2013-12-19 08:30:17 +00:00
parent 2f43f2c428
commit df3204874d
4 changed files with 48 additions and 39 deletions

View File

@ -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<string, char>) == sizeof(map<string, entry>)
@ -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

View File

@ -33,8 +33,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/pch.hpp"
#include <algorithm>
#if (defined TORRENT_VERBOSE_LOGGING || defined TORRENT_DEBUG) && TORRENT_USE_IOSTREAM
#include <iomanip>
#if TORRENT_USE_IOSTREAM
#include <iostream>
#endif
#include <boost/bind.hpp>
@ -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 << "<uninitialized>\n";
out += "<uninitialized>\n";
}
}
#endif
}

View File

@ -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'
}

View File

@ -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;