Enabled users to define custom debug message listeners

This commit is contained in:
Tsuda Kageyu
2013-06-08 09:59:36 +09:00
parent e18546560e
commit 3b2d620671
4 changed files with 64 additions and 9 deletions

View File

@ -31,5 +31,10 @@ using namespace TagLib;
void TagLib::debug(const String &s)
{
getDebugListener()->listen(s);
getDebugListener()->printMessage(s);
}
void TagLib::debugData(const ByteVector &v)
{
getDebugListener()->printData(v);
}

View File

@ -29,6 +29,7 @@
namespace TagLib {
class String;
class ByteVector;
#ifndef DO_NOT_DOCUMENT
@ -44,6 +45,17 @@ namespace TagLib {
* \internal
*/
void debug(const String &s);
/*!
* For debugging binary data.
*
* \warning Do not use this outside of TagLib, it could lead to undefined
* symbols in your build if TagLib is built with NDEBUG defined and your
* application is not.
*
* \internal
*/
void debugData(const ByteVector &v);
}
#endif

View File

@ -27,6 +27,7 @@
#ifndef NDEBUG
# include <iostream>
# include <bitset>
# ifdef _WIN32
# include <windows.h>
# endif
@ -39,7 +40,7 @@ namespace
class DefaultListener : public DebugListener
{
public:
virtual void listen(const String &msg)
virtual void printMessage(const String &msg)
{
#ifndef NDEBUG
# ifdef _WIN32
@ -59,23 +60,51 @@ namespace
std::cerr << "TagLib: " << msg << std::endl;
# 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
}
};
DefaultListener defaultListener;
DebugListener *traceListener = &defaultListener;
DebugListener *debugListener = &defaultListener;
}
TagLib::DebugListener::DebugListener()
{
}
TagLib::DebugListener::~DebugListener()
{
}
void TagLib::setDebugListener(DebugListener *listener)
{
if(listener)
traceListener = listener;
debugListener = listener;
else
traceListener = &defaultListener;
debugListener = &defaultListener;
}
DebugListener *TagLib::getDebugListener()
{
return traceListener;
return debugListener;
}

View File

@ -43,7 +43,16 @@ namespace TagLib
class TAGLIB_EXPORT DebugListener
{
public:
virtual void listen(const String &msg) = 0;
DebugListener();
virtual ~DebugListener();
virtual void printMessage(const String &msg) = 0;
virtual void printData(const ByteVector &data) = 0;
private:
// Noncopyable
DebugListener(const DebugListener &);
DebugListener &operator=(const DebugListener &);
};
/*!
@ -56,14 +65,14 @@ namespace TagLib
*
* \see DebugListener
*/
void setDebugListener(DebugListener *listener);
TAGLIB_EXPORT void setDebugListener(DebugListener *listener);
#ifndef DO_NOT_DOCUMENT
/*!
* \internal
*/
DebugListener *getDebugListener();
TAGLIB_EXPORT DebugListener *getDebugListener();
#endif
}