From b5e41242b41e7ac0b4dfd4fe4c00e0587669bd14 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 13 Aug 2012 22:19:38 +0200 Subject: [PATCH] Provide adjustable handlers for mxinfo(), mxwarn(), mxerror() --- src/common/common.cpp | 2 +- src/common/output.cpp | 60 ++++++++++++++++++++++++++++++++++--------- src/common/output.h | 9 +++++-- src/common/qt.h | 6 ----- 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/common/common.cpp b/src/common/common.cpp index 1232c23c3..64994ec42 100644 --- a/src/common/common.cpp +++ b/src/common/common.cpp @@ -137,7 +137,7 @@ mtx_common_init(std::string const &program_name) { mm_file_io_c::setup(); g_cc_local_utf8 = charset_converter_c::init(""); - init_cc_stdio(); + init_common_output(); stereo_mode_c::init(); } diff --git a/src/common/output.cpp b/src/common/output.cpp index 34b4ce3b9..d98ed29e0 100644 --- a/src/common/output.cpp +++ b/src/common/output.cpp @@ -39,6 +39,8 @@ static bool s_mm_stdio_redirected = false; charset_converter_cptr g_cc_stdio = charset_converter_cptr(new charset_converter_c); std::shared_ptr g_mm_stdio = std::shared_ptr(new mm_stdio_c); +static mxmsg_handler_t s_mxmsg_info_handler, s_mxmsg_warning_handler, s_mxmsg_error_handler; + void redirect_stdio(const mm_io_cptr &stdio) { g_mm_stdio = stdio; @@ -50,6 +52,19 @@ stdio_redirected() { return s_mm_stdio_redirected; } +void +set_mxmsg_handler(unsigned int level, + mxmsg_handler_t const &handler) { + if (MXMSG_INFO == level) + s_mxmsg_info_handler = handler; + else if (MXMSG_WARNING == level) + s_mxmsg_warning_handler = handler; + else if (MXMSG_ERROR == level) + s_mxmsg_error_handler = handler; + else + assert(false); +} + void mxmsg(unsigned int level, std::string message) { @@ -88,23 +103,30 @@ mxmsg(unsigned int level, g_mm_stdio->flush(); } -void -mxinfo(const std::string &info) { +static void +default_mxinfo(unsigned int, + std::string const &info) { mxmsg(MXMSG_INFO, info); } +void +mxinfo(std::string const &info) { + s_mxmsg_info_handler(MXMSG_INFO, info); +} + void mxinfo(const std::wstring &info) { - mxmsg(MXMSG_INFO, to_utf8(info)); + mxinfo(to_utf8(info)); } void mxinfo(const boost::wformat &info) { - mxmsg(MXMSG_INFO, to_utf8(info.str())); + mxinfo(to_utf8(info.str())); } -void -mxwarn(const std::string &warning) { +static void +default_mxwarn(unsigned int, + std::string const &warning) { if (g_suppress_warnings) return; @@ -114,22 +136,33 @@ mxwarn(const std::string &warning) { } void -mxerror(const std::string &error) { +mxwarn(std::string const &warning) { + s_mxmsg_warning_handler(MXMSG_WARNING, warning); +} + +static void +default_mxerror(unsigned int, + std::string const &error) { mxmsg(MXMSG_ERROR, error); mxexit(2); } +void +mxerror(std::string const &error) { + s_mxmsg_error_handler(MXMSG_ERROR, error); +} + void mxinfo_fn(const std::string &file_name, const std::string &info) { - mxmsg(MXMSG_INFO, (boost::format(Y("'%1%': %2%")) % file_name % info).str()); + mxinfo((boost::format(Y("'%1%': %2%")) % file_name % info).str()); } void mxinfo_tid(const std::string &file_name, int64_t track_id, const std::string &info) { - mxmsg(MXMSG_INFO, (boost::format(Y("'%1%' track %2%: %3%")) % file_name % track_id % info).str()); + mxinfo((boost::format(Y("'%1%' track %2%: %3%")) % file_name % track_id % info).str()); } void @@ -165,7 +198,7 @@ mxverb_fn(unsigned int level, if (verbose < level) return; - mxmsg(MXMSG_INFO, (boost::format(Y("'%1%': %2%")) % file_name % message).str()); + mxinfo((boost::format(Y("'%1%': %2%")) % file_name % message).str()); } void @@ -176,12 +209,15 @@ mxverb_tid(unsigned int level, if (verbose < level) return; - mxmsg(MXMSG_INFO, (boost::format(Y("'%1%' track %2%: %3%")) % file_name % track_id % message).str()); + mxinfo((boost::format(Y("'%1%' track %2%: %3%")) % file_name % track_id % message).str()); } void -init_cc_stdio() { +init_common_output() { set_cc_stdio(get_local_console_charset()); + set_mxmsg_handler(MXMSG_INFO, default_mxinfo); + set_mxmsg_handler(MXMSG_WARNING, default_mxwarn); + set_mxmsg_handler(MXMSG_ERROR, default_mxerror); } void diff --git a/src/common/output.h b/src/common/output.h index f972cee50..c7290f870 100644 --- a/src/common/output.h +++ b/src/common/output.h @@ -16,6 +16,8 @@ #include "common/os.h" +#include + #include #include "common/locale.h" @@ -23,6 +25,9 @@ using namespace libebml; +typedef std::function mxmsg_handler_t; +void set_mxmsg_handler(unsigned int level, mxmsg_handler_t const &handler); + extern bool g_suppress_info, g_suppress_warnings; extern std::string g_stdio_charset; extern charset_converter_cptr g_cc_stdio; @@ -31,7 +36,7 @@ extern std::shared_ptr g_mm_stdio; void redirect_stdio(const mm_io_cptr &new_stdio); bool stdio_redirected(); -void init_cc_stdio(); +void init_common_output(); void set_cc_stdio(const std::string &charset); void mxmsg(unsigned int level, std::string message); @@ -65,7 +70,7 @@ mxerror(const boost::format &error) { #define mxverb(level, message) \ if (verbose >= level) \ - mxmsg(MXMSG_INFO, message); + mxinfo(message); #define mxdebug(msg) \ diff --git a/src/common/qt.h b/src/common/qt.h index 70f3e5f2a..87493d04e 100644 --- a/src/common/qt.h +++ b/src/common/qt.h @@ -44,12 +44,6 @@ mxinfo(QString const &s) { mxinfo(to_utf8(s)); } -inline void -mxmsg(unsigned int level, - QString const &message) { - mxmsg(level, to_utf8(message)); -} - inline std::ostream & operator <<(std::ostream &out, QString const &string) {