2013-12-22 14:04:33 +00:00
|
|
|
#include "Logs.h"
|
2015-08-12 22:33:16 +00:00
|
|
|
|
2015-12-30 19:20:27 +00:00
|
|
|
#include "Timing.h"
|
2015-09-14 17:25:54 +00:00
|
|
|
|
2015-10-18 23:39:22 +00:00
|
|
|
#include <ostream>
|
2016-07-23 20:58:32 +00:00
|
|
|
#include <streambuf>
|
2015-10-18 23:39:22 +00:00
|
|
|
|
2015-12-10 23:36:50 +00:00
|
|
|
template <class cT, class traits = char_traits<cT>> class basic_nullbuf : public basic_streambuf<cT, traits> {
|
2015-11-09 21:30:46 +00:00
|
|
|
typename traits::int_type overflow(typename traits::int_type c) {
|
2015-10-18 23:39:22 +00:00
|
|
|
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> {
|
2015-11-09 21:30:46 +00:00
|
|
|
public:
|
2015-12-10 23:36:50 +00:00
|
|
|
basic_onullstream() : basic_ios<cT, traits>(&m_sbuf), basic_ostream<cT, traits>(&m_sbuf) {
|
2015-11-09 21:30:46 +00:00
|
|
|
this->init(&m_sbuf);
|
|
|
|
}
|
2015-10-18 23:39:22 +00:00
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
private:
|
|
|
|
basic_nullbuf<cT, traits> m_sbuf;
|
2015-10-18 23:39:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef basic_onullstream<char> onullstream;
|
|
|
|
static onullstream NULL_STREAM;
|
2015-08-12 22:33:16 +00:00
|
|
|
static bool enabled = true;
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
2015-12-29 23:43:35 +00:00
|
|
|
ostream &Logs::debug(const string &) {
|
2015-08-12 22:33:16 +00:00
|
|
|
return NULL_STREAM;
|
2015-12-29 23:43:35 +00:00
|
|
|
}
|
2015-08-12 22:33:16 +00:00
|
|
|
#else
|
2015-12-29 23:43:35 +00:00
|
|
|
ostream &Logs::debug(const string &logger) {
|
2015-11-09 21:30:46 +00:00
|
|
|
if (enabled) {
|
2015-12-13 19:08:38 +00:00
|
|
|
cout << "DEBUG [" << logger << "] ";
|
2015-12-10 23:36:50 +00:00
|
|
|
return cout;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-08-12 22:33:16 +00:00
|
|
|
return NULL_STREAM;
|
|
|
|
}
|
|
|
|
}
|
2015-12-29 23:43:35 +00:00
|
|
|
#endif
|
2015-08-12 22:33:16 +00:00
|
|
|
|
2015-12-13 19:08:38 +00:00
|
|
|
ostream &Logs::warning(const string &logger) {
|
2015-11-09 21:30:46 +00:00
|
|
|
if (enabled) {
|
2015-12-13 19:08:38 +00:00
|
|
|
cerr << "WARN [" << logger << "] ";
|
2015-12-10 23:36:50 +00:00
|
|
|
return cerr;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-08-12 22:33:16 +00:00
|
|
|
return NULL_STREAM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 19:08:38 +00:00
|
|
|
ostream &Logs::error(const string &logger) {
|
2015-11-09 21:30:46 +00:00
|
|
|
if (enabled) {
|
2015-12-13 19:08:38 +00:00
|
|
|
cerr << "ERROR [" << logger << "] ";
|
2015-12-10 23:36:50 +00:00
|
|
|
return cerr;
|
2015-11-09 21:30:46 +00:00
|
|
|
} else {
|
2015-08-12 22:33:16 +00:00
|
|
|
return NULL_STREAM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 19:08:38 +00:00
|
|
|
void Logs::debugTimestamp(const string &logger, const string &message) {
|
2015-12-30 19:20:27 +00:00
|
|
|
debug(logger) << Timing::getRelativeTimeMs() << " - " << message << endl;
|
2015-09-14 17:25:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 21:30:46 +00:00
|
|
|
void Logs::disable() {
|
2015-08-12 22:33:16 +00:00
|
|
|
enabled = false;
|
|
|
|
}
|