2013-12-22 14:04:33 +00:00
|
|
|
#include "Logs.h"
|
2015-08-12 22:33:16 +00:00
|
|
|
|
2015-09-14 17:25:54 +00:00
|
|
|
#include "Time.h"
|
|
|
|
|
2015-10-18 23:39:22 +00:00
|
|
|
#include <streambuf>
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
template <class cT, class traits = std::char_traits<cT> >
|
|
|
|
class basic_nullbuf: public std::basic_streambuf<cT, traits> {
|
|
|
|
typename traits::int_type overflow(typename traits::int_type c)
|
|
|
|
{
|
|
|
|
return traits::not_eof(c); // indicate success
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class cT, class traits = std::char_traits<cT> >
|
|
|
|
class basic_onullstream: public std::basic_ostream<cT, traits> {
|
|
|
|
public:
|
|
|
|
basic_onullstream():
|
|
|
|
std::basic_ios<cT, traits>(&m_sbuf),
|
|
|
|
std::basic_ostream<cT, traits>(&m_sbuf)
|
|
|
|
{
|
|
|
|
this->init(&m_sbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
basic_nullbuf<cT, traits> m_sbuf;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef basic_onullstream<char> onullstream;
|
|
|
|
static onullstream NULL_STREAM;
|
2015-08-12 22:33:16 +00:00
|
|
|
static bool enabled = true;
|
|
|
|
|
|
|
|
std::ostream &Logs::debug()
|
|
|
|
{
|
|
|
|
#ifdef NDEBUG
|
|
|
|
return NULL_STREAM;
|
|
|
|
#else
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
std::cout << "DEBUG - ";
|
|
|
|
return std::cout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL_STREAM;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &Logs::warning()
|
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
std::cerr << "WARN - ";
|
|
|
|
return std::cerr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL_STREAM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &Logs::error()
|
|
|
|
{
|
|
|
|
if (enabled)
|
|
|
|
{
|
|
|
|
std::cerr << "ERROR - ";
|
|
|
|
return std::cerr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL_STREAM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 17:25:54 +00:00
|
|
|
void Logs::debugTimestamp(const std::string &message)
|
|
|
|
{
|
|
|
|
debug() << Time::getRelativeTimeMs() << " - " << message << std::endl;
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:33:16 +00:00
|
|
|
void Logs::disable()
|
|
|
|
{
|
|
|
|
enabled = false;
|
|
|
|
}
|