From 448648d61be288fb0c4193d2937f2c9bccf6763d Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Sat, 8 Jun 2013 21:40:30 +0900 Subject: [PATCH] Simplified DebugListener class --- taglib/toolkit/tdebug.cpp | 52 +++++++++++++++++++++++++++++-- taglib/toolkit/tdebuglistener.cpp | 30 +++--------------- taglib/toolkit/tdebuglistener.h | 5 ++- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/taglib/toolkit/tdebug.cpp b/taglib/toolkit/tdebug.cpp index 95d5c55f..133aad39 100644 --- a/taglib/toolkit/tdebug.cpp +++ b/taglib/toolkit/tdebug.cpp @@ -23,18 +23,66 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ +#include "config.h" + #include "tdebug.h" #include "tstring.h" #include "tdebuglistener.h" +#include +#include +#include + using namespace TagLib; +namespace +{ + String format(const char *fmt, ...) + { + va_list args; + va_start(args, fmt); + + char buf[256]; + +#if defined(HAVE_SNPRINTF) + + vsnprintf(buf, sizeof(buf), fmt, args); + +#elif defined(HAVE_SPRINTF_S) + + vsprintf_s(buf, fmt, args); + +#else + + // Be careful. May cause a buffer overflow. + vsprintf(buf, fmt, args); + +#endif + + va_end(args); + + return String(buf); + } +} + void TagLib::debug(const String &s) { - getDebugListener()->printMessage(s); + getDebugListener()->printMessage("TagLib: " + s + "\n"); } void TagLib::debugData(const ByteVector &v) { - getDebugListener()->printData(v); + for(size_t i = 0; i < v.size(); ++i) + { + String msg + = format("*** [%d] - char '%c' - int %d, 0x%02x, 0b", i, v[i], v[i], v[i]); + + std::bitset<8> b(v[i]); + for(int j = 7; j >= 0; --j) + msg += format("%d", (b.test(j) ? 1 : 0)); + + msg += "\n"; + + getDebugListener()->printMessage(msg); + } } diff --git a/taglib/toolkit/tdebuglistener.cpp b/taglib/toolkit/tdebuglistener.cpp index bce94312..3b138e51 100644 --- a/taglib/toolkit/tdebuglistener.cpp +++ b/taglib/toolkit/tdebuglistener.cpp @@ -45,41 +45,21 @@ namespace #ifndef NDEBUG # ifdef _WIN32 - std::string s; const wstring wstr = msg.toWString(); const int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL); if(len != 0) { - s.resize(len); - WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &s[0], len, NULL, NULL); + std::vector buf(len); + WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &buf[0], len, NULL, NULL); + + std::cerr << std::string(&buf[0]); } - std::cerr << "TagLib: " << s << std::endl; # else - std::cerr << "TagLib: " << msg << std::endl; + std::cerr << msg; # endif -#endif - } - - virtual void printData(const ByteVector &v) - { -#ifndef NDEBUG - - for(size_t i = 0; i < v.size(); i++) - { - std::cout << "*** [" << i << "] - '" << char(v[i]) << "' - int " << int(v[i]) - << std::endl; - - std::bitset<8> b(v[i]); - - for(int j = 0; j < 8; j++) - std::cout << i << ":" << j << " " << b.test(j) << std::endl; - - std::cout << std::endl; - } - #endif } }; diff --git a/taglib/toolkit/tdebuglistener.h b/taglib/toolkit/tdebuglistener.h index 3dd3a734..cd1c52e5 100644 --- a/taglib/toolkit/tdebuglistener.h +++ b/taglib/toolkit/tdebuglistener.h @@ -35,8 +35,8 @@ namespace TagLib /*! * This class enables you to handle the debug messages in your preferred - * way by subclassing this class, reimplementing listen() and setting your - * reimplementation as the default with setDebugListener(). + * way by subclassing this class, reimplementing printMessage() and setting + * your reimplementation as the default with setDebugListener(). * * \see setDebugListener() */ @@ -47,7 +47,6 @@ namespace TagLib virtual ~DebugListener(); virtual void printMessage(const String &msg) = 0; - virtual void printData(const ByteVector &data) = 0; private: // Noncopyable -- 2.40.0