mirror of
https://github.com/taglib/taglib.git
synced 2025-06-04 01:28:21 -04:00
Simplified DebugListener class
This commit is contained in:
parent
3b2d620671
commit
448648d61b
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
};
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user