From bda8dee41a003d6a06f1c135ecc8fb2bdc87dfab Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Sun, 8 Dec 2013 19:49:09 +0100 Subject: [PATCH] refactoring: move ebml_dumper_c to debugging.h --- src/common/debugging.cpp | 133 +++++++++++++++++++++++++++++++++++++++ src/common/debugging.h | 41 ++++++++++++ src/common/output.cpp | 129 ------------------------------------- src/common/output.h | 33 ---------- 4 files changed, 174 insertions(+), 162 deletions(-) diff --git a/src/common/debugging.cpp b/src/common/debugging.cpp index a2e76d229..b9914e313 100644 --- a/src/common/debugging.cpp +++ b/src/common/debugging.cpp @@ -15,8 +15,22 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common/debugging.h" #include "common/logger.h" #include "common/strings/editing.h" +#include "common/strings/formatting.h" + +using namespace libebml; std::unordered_map debugging_c::ms_debugging_options; bool debugging_c::ms_send_to_logger = false; @@ -141,3 +155,122 @@ debugging_option_c::invalidate_cache() { for (auto &opt : ms_registered_options) opt.m_requested = boost::logic::indeterminate; } + +// ------------------------------------------------------------ + +ebml_dumper_c::ebml_dumper_c() + : m_values{true} + , m_addresses{true} + , m_indexes{true} + , m_max_level{std::numeric_limits::max()} + , m_target_type{STDOUT} + , m_io_target{} +{ +} + +std::string +ebml_dumper_c::to_string(EbmlElement const *element) + const { + return dynamic_cast(element) ? ::to_string(static_cast(element)->GetValue()) + : dynamic_cast(element) ? ::to_string(static_cast(element)->GetValue()) + : dynamic_cast(element) ? ::to_string(static_cast(element)->GetValue(), 9) + : dynamic_cast(element) ? static_cast(element)->GetValueUTF8() + : dynamic_cast(element) ? static_cast(element)->GetValue() + : dynamic_cast(element) ? ::to_string(static_cast(element)->GetEpochDate()) + : (boost::format("(type: %1% size: %2%)") % + ( dynamic_cast(element) ? "binary" + : dynamic_cast(element) ? "master" + : dynamic_cast(element) ? "void" + : "unknown") + % element->GetSize()).str(); +} + + +ebml_dumper_c & +ebml_dumper_c::values(bool p_values) { + m_values = p_values; + return *this; +} + +ebml_dumper_c & +ebml_dumper_c::addresses(bool p_addresses) { + m_addresses = p_addresses; + return *this; +} + +ebml_dumper_c & +ebml_dumper_c::indexes(bool p_indexes) { + m_indexes = p_indexes; + return *this; +} + +ebml_dumper_c & +ebml_dumper_c::max_level(int p_max_level) { + m_max_level = p_max_level; + return *this; +} + +ebml_dumper_c & +ebml_dumper_c::target(target_type_e p_target_type, + mm_io_c *p_io_target) { + m_target_type = p_target_type; + m_io_target = p_io_target; + return *this; +} + +ebml_dumper_c & +ebml_dumper_c::dump(EbmlElement const *element) { + dump_impl(element, 0, 0); + + switch (m_target_type) { + case STDOUT: mxinfo(m_buffer.str()); break; + case LOGGER: log_it(m_buffer.str()); break; + case MM_IO: assert(!!m_io_target); m_io_target->puts(m_buffer.str()); break; + default: assert(false); + } + + m_buffer.str(""); + + return *this; +} + +void +ebml_dumper_c::dump_impl(EbmlElement const *element, + size_t level, + size_t index) { + if (level > m_max_level) + return; + + m_buffer << std::string(level, ' '); + + if (m_indexes) + m_buffer << index << " "; + + if (!element) { + m_buffer << "nullptr" << std::endl; + return; + } + + m_buffer << EBML_NAME(element); + + if (m_addresses) + m_buffer << (boost::format(" @%1%") % element); + + if (m_values) + m_buffer << " " << to_string(element); + + m_buffer << std::endl; + + auto master = dynamic_cast(element); + if (!master) + return; + + for (auto idx = 0u; master->ListSize() > idx; ++idx) + dump_impl((*master)[idx], level + 1, idx); +} + +void +dump_ebml_elements(EbmlElement *element, + bool with_values) { + ebml_dumper_c{}.values(with_values).dump(element); +} diff --git a/src/common/debugging.h b/src/common/debugging.h index 1bc0284fe..c87a87e1f 100644 --- a/src/common/debugging.h +++ b/src/common/debugging.h @@ -92,4 +92,45 @@ public: mxdebug(msg); \ } +class mm_io_c; +namespace libebml { +class EbmlElement; +}; + +class ebml_dumper_c { +public: + enum target_type_e { + STDOUT, + MM_IO, + LOGGER, + }; + +private: + bool m_values, m_addresses, m_indexes; + size_t m_max_level; + target_type_e m_target_type; + mm_io_c *m_io_target; + std::stringstream m_buffer; + +public: + ebml_dumper_c(); + + ebml_dumper_c &values(bool p_values); + ebml_dumper_c &addresses(bool p_addresses); + ebml_dumper_c &indexes(bool p_indexes); + ebml_dumper_c &max_level(int p_max_level); + ebml_dumper_c &target(target_type_e p_target_type, mm_io_c *p_io_target = nullptr); + + ebml_dumper_c &dump(libebml::EbmlElement const *element); + ebml_dumper_c &dump_if(bool do_it, libebml::EbmlElement const *element) { + return do_it ? dump(element) : *this; + } + +private: + void dump_impl(libebml::EbmlElement const *element, size_t level, size_t index); + std::string to_string(libebml::EbmlElement const *element) const; +}; + +void dump_ebml_elements(libebml::EbmlElement *element, bool with_values = false); + #endif // MTX_COMMON_DEBUGGING_H diff --git a/src/common/output.cpp b/src/common/output.cpp index 86539334b..e75dbb671 100644 --- a/src/common/output.cpp +++ b/src/common/output.cpp @@ -20,20 +20,8 @@ #include "common/locale.h" #include "common/logger.h" #include "common/mm_io.h" -#include "common/strings/formatting.h" #include "common/strings/utf8.h" -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace libebml; - bool g_suppress_info = false; bool g_suppress_warnings = false; bool g_warning_issued = false; @@ -233,123 +221,6 @@ set_cc_stdio(const std::string &charset) { g_cc_stdio = charset_converter_c::init(charset); } -ebml_dumper_c::ebml_dumper_c() - : m_values{true} - , m_addresses{true} - , m_indexes{true} - , m_max_level{std::numeric_limits::max()} - , m_target_type{STDOUT} - , m_io_target{} -{ -} - -std::string -ebml_dumper_c::to_string(EbmlElement const *element) - const { - return dynamic_cast(element) ? ::to_string(static_cast(element)->GetValue()) - : dynamic_cast(element) ? ::to_string(static_cast(element)->GetValue()) - : dynamic_cast(element) ? ::to_string(static_cast(element)->GetValue(), 9) - : dynamic_cast(element) ? static_cast(element)->GetValueUTF8() - : dynamic_cast(element) ? static_cast(element)->GetValue() - : dynamic_cast(element) ? ::to_string(static_cast(element)->GetEpochDate()) - : (boost::format("(type: %1% size: %2%)") % - ( dynamic_cast(element) ? "binary" - : dynamic_cast(element) ? "master" - : dynamic_cast(element) ? "void" - : "unknown") - % element->GetSize()).str(); -} - - -ebml_dumper_c & -ebml_dumper_c::values(bool p_values) { - m_values = p_values; - return *this; -} - -ebml_dumper_c & -ebml_dumper_c::addresses(bool p_addresses) { - m_addresses = p_addresses; - return *this; -} - -ebml_dumper_c & -ebml_dumper_c::indexes(bool p_indexes) { - m_indexes = p_indexes; - return *this; -} - -ebml_dumper_c & -ebml_dumper_c::max_level(int p_max_level) { - m_max_level = p_max_level; - return *this; -} - -ebml_dumper_c & -ebml_dumper_c::target(target_type_e p_target_type, - mm_io_c *p_io_target) { - m_target_type = p_target_type; - m_io_target = p_io_target; - return *this; -} - -ebml_dumper_c & -ebml_dumper_c::dump(EbmlElement const *element) { - dump_impl(element, 0, 0); - - switch (m_target_type) { - case STDOUT: mxinfo(m_buffer.str()); break; - case LOGGER: log_it(m_buffer.str()); break; - case MM_IO: assert(!!m_io_target); m_io_target->puts(m_buffer.str()); break; - default: assert(false); - } - - m_buffer.str(""); - - return *this; -} - -void -ebml_dumper_c::dump_impl(EbmlElement const *element, - size_t level, - size_t index) { - if (level > m_max_level) - return; - - m_buffer << std::string(level, ' '); - - if (m_indexes) - m_buffer << index << " "; - - if (!element) { - m_buffer << "nullptr" << std::endl; - return; - } - - m_buffer << EBML_NAME(element); - - if (m_addresses) - m_buffer << (boost::format(" @%1%") % element); - - if (m_values) - m_buffer << " " << to_string(element); - - m_buffer << std::endl; - - auto master = dynamic_cast(element); - if (!master) - return; - - for (auto idx = 0u; master->ListSize() > idx; ++idx) - dump_impl((*master)[idx], level + 1, idx); -} - -void -dump_ebml_elements(EbmlElement *element, - bool with_values) { - ebml_dumper_c{}.values(with_values).dump(element); -} - std::string fourcc_to_string(uint32_t fourcc) { unsigned char buffer[4], idx; diff --git a/src/common/output.h b/src/common/output.h index 784d5e48f..8527534bc 100644 --- a/src/common/output.h +++ b/src/common/output.h @@ -136,39 +136,6 @@ mxverb_tid(unsigned int level, extern const std::string empty_string; -class ebml_dumper_c { -public: - enum target_type_e { - STDOUT, - MM_IO, - LOGGER, - }; - -private: - bool m_values, m_addresses, m_indexes; - size_t m_max_level; - target_type_e m_target_type; - mm_io_c *m_io_target; - std::stringstream m_buffer; - -public: - ebml_dumper_c(); - - ebml_dumper_c &values(bool p_values); - ebml_dumper_c &addresses(bool p_addresses); - ebml_dumper_c &indexes(bool p_indexes); - ebml_dumper_c &max_level(int p_max_level); - ebml_dumper_c &target(target_type_e p_target_type, mm_io_c *p_io_target = nullptr); - - ebml_dumper_c &dump(EbmlElement const *element); - -private: - void dump_impl(EbmlElement const *element, size_t level, size_t index); - std::string to_string(EbmlElement const *element) const; -}; - -void dump_ebml_elements(EbmlElement *element, bool with_values = false); - std::string fourcc_to_string(uint32_t fourcc); #endif // MTX_COMMON_OUTPUT_H