Provide adjustable handlers for mxinfo(), mxwarn(), mxerror()

This commit is contained in:
Moritz Bunkus 2012-08-13 22:19:38 +02:00
parent dc225ba8d6
commit b5e41242b4
4 changed files with 56 additions and 21 deletions

View File

@ -137,7 +137,7 @@ mtx_common_init(std::string const &program_name) {
mm_file_io_c::setup(); mm_file_io_c::setup();
g_cc_local_utf8 = charset_converter_c::init(""); g_cc_local_utf8 = charset_converter_c::init("");
init_cc_stdio(); init_common_output();
stereo_mode_c::init(); stereo_mode_c::init();
} }

View File

@ -39,6 +39,8 @@ static bool s_mm_stdio_redirected = false;
charset_converter_cptr g_cc_stdio = charset_converter_cptr(new charset_converter_c); charset_converter_cptr g_cc_stdio = charset_converter_cptr(new charset_converter_c);
std::shared_ptr<mm_io_c> g_mm_stdio = std::shared_ptr<mm_io_c>(new mm_stdio_c); std::shared_ptr<mm_io_c> g_mm_stdio = std::shared_ptr<mm_io_c>(new mm_stdio_c);
static mxmsg_handler_t s_mxmsg_info_handler, s_mxmsg_warning_handler, s_mxmsg_error_handler;
void void
redirect_stdio(const mm_io_cptr &stdio) { redirect_stdio(const mm_io_cptr &stdio) {
g_mm_stdio = stdio; g_mm_stdio = stdio;
@ -50,6 +52,19 @@ stdio_redirected() {
return s_mm_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 void
mxmsg(unsigned int level, mxmsg(unsigned int level,
std::string message) { std::string message) {
@ -88,23 +103,30 @@ mxmsg(unsigned int level,
g_mm_stdio->flush(); g_mm_stdio->flush();
} }
void static void
mxinfo(const std::string &info) { default_mxinfo(unsigned int,
std::string const &info) {
mxmsg(MXMSG_INFO, info); mxmsg(MXMSG_INFO, info);
} }
void
mxinfo(std::string const &info) {
s_mxmsg_info_handler(MXMSG_INFO, info);
}
void void
mxinfo(const std::wstring &info) { mxinfo(const std::wstring &info) {
mxmsg(MXMSG_INFO, to_utf8(info)); mxinfo(to_utf8(info));
} }
void void
mxinfo(const boost::wformat &info) { mxinfo(const boost::wformat &info) {
mxmsg(MXMSG_INFO, to_utf8(info.str())); mxinfo(to_utf8(info.str()));
} }
void static void
mxwarn(const std::string &warning) { default_mxwarn(unsigned int,
std::string const &warning) {
if (g_suppress_warnings) if (g_suppress_warnings)
return; return;
@ -114,22 +136,33 @@ mxwarn(const std::string &warning) {
} }
void 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); mxmsg(MXMSG_ERROR, error);
mxexit(2); mxexit(2);
} }
void
mxerror(std::string const &error) {
s_mxmsg_error_handler(MXMSG_ERROR, error);
}
void void
mxinfo_fn(const std::string &file_name, mxinfo_fn(const std::string &file_name,
const std::string &info) { 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 void
mxinfo_tid(const std::string &file_name, mxinfo_tid(const std::string &file_name,
int64_t track_id, int64_t track_id,
const std::string &info) { 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 void
@ -165,7 +198,7 @@ mxverb_fn(unsigned int level,
if (verbose < level) if (verbose < level)
return; return;
mxmsg(MXMSG_INFO, (boost::format(Y("'%1%': %2%")) % file_name % message).str()); mxinfo((boost::format(Y("'%1%': %2%")) % file_name % message).str());
} }
void void
@ -176,12 +209,15 @@ mxverb_tid(unsigned int level,
if (verbose < level) if (verbose < level)
return; 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 void
init_cc_stdio() { init_common_output() {
set_cc_stdio(get_local_console_charset()); 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 void

View File

@ -16,6 +16,8 @@
#include "common/os.h" #include "common/os.h"
#include <functional>
#include <ebml/EbmlElement.h> #include <ebml/EbmlElement.h>
#include "common/locale.h" #include "common/locale.h"
@ -23,6 +25,9 @@
using namespace libebml; using namespace libebml;
typedef std::function<void(unsigned int level, std::string const &)> mxmsg_handler_t;
void set_mxmsg_handler(unsigned int level, mxmsg_handler_t const &handler);
extern bool g_suppress_info, g_suppress_warnings; extern bool g_suppress_info, g_suppress_warnings;
extern std::string g_stdio_charset; extern std::string g_stdio_charset;
extern charset_converter_cptr g_cc_stdio; extern charset_converter_cptr g_cc_stdio;
@ -31,7 +36,7 @@ extern std::shared_ptr<mm_io_c> g_mm_stdio;
void redirect_stdio(const mm_io_cptr &new_stdio); void redirect_stdio(const mm_io_cptr &new_stdio);
bool stdio_redirected(); bool stdio_redirected();
void init_cc_stdio(); void init_common_output();
void set_cc_stdio(const std::string &charset); void set_cc_stdio(const std::string &charset);
void mxmsg(unsigned int level, std::string message); void mxmsg(unsigned int level, std::string message);
@ -65,7 +70,7 @@ mxerror(const boost::format &error) {
#define mxverb(level, message) \ #define mxverb(level, message) \
if (verbose >= level) \ if (verbose >= level) \
mxmsg(MXMSG_INFO, message); mxinfo(message);
#define mxdebug(msg) \ #define mxdebug(msg) \

View File

@ -44,12 +44,6 @@ mxinfo(QString const &s) {
mxinfo(to_utf8(s)); mxinfo(to_utf8(s));
} }
inline void
mxmsg(unsigned int level,
QString const &message) {
mxmsg(level, to_utf8(message));
}
inline std::ostream & inline std::ostream &
operator <<(std::ostream &out, operator <<(std::ostream &out,
QString const &string) { QString const &string) {