Simplified DebugListener class

This commit is contained in:
Tsuda Kageyu 2013-06-08 21:40:30 +09:00
parent 3b2d620671
commit 448648d61b
3 changed files with 57 additions and 30 deletions

View File

@ -23,18 +23,66 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include "config.h"
#include "tdebug.h"
#include "tstring.h"
#include "tdebuglistener.h"
#include <bitset>
#include <cstdio>
#include <cstdarg>
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);
}
}

View File

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

View File

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