paysages3d/src/system/Logs.cpp

68 lines
1.5 KiB
C++
Raw Normal View History

#include "Logs.h"
#include "Timing.h"
2015-09-14 17:25:54 +00:00
#include <ostream>
2016-07-23 20:58:32 +00:00
#include <streambuf>
2015-12-10 23:36:50 +00:00
template <class cT, class traits = char_traits<cT>> class basic_nullbuf : public basic_streambuf<cT, traits> {
typename traits::int_type overflow(typename traits::int_type c) {
return traits::not_eof(c); // indicate success
}
};
2015-12-10 23:36:50 +00:00
template <class cT, class traits = char_traits<cT>> class basic_onullstream : public basic_ostream<cT, traits> {
public:
2015-12-10 23:36:50 +00:00
basic_onullstream() : basic_ios<cT, traits>(&m_sbuf), 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;
static bool enabled = true;
#ifdef NDEBUG
ostream &Logs::debug(const string &) {
return NULL_STREAM;
}
#else
ostream &Logs::debug(const string &logger) {
if (enabled) {
2015-12-13 19:08:38 +00:00
cout << "DEBUG [" << logger << "] ";
2015-12-10 23:36:50 +00:00
return cout;
} else {
return NULL_STREAM;
}
}
#endif
2015-12-13 19:08:38 +00:00
ostream &Logs::warning(const string &logger) {
if (enabled) {
2015-12-13 19:08:38 +00:00
cerr << "WARN [" << logger << "] ";
2015-12-10 23:36:50 +00:00
return cerr;
} else {
return NULL_STREAM;
}
}
2015-12-13 19:08:38 +00:00
ostream &Logs::error(const string &logger) {
if (enabled) {
2015-12-13 19:08:38 +00:00
cerr << "ERROR [" << logger << "] ";
2015-12-10 23:36:50 +00:00
return cerr;
} else {
return NULL_STREAM;
}
}
2015-12-13 19:08:38 +00:00
void Logs::debugTimestamp(const string &logger, const string &message) {
debug(logger) << Timing::getRelativeTimeMs() << " - " << message << endl;
2015-09-14 17:25:54 +00:00
}
void Logs::disable() {
enabled = false;
}