You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.0 KiB

5 years ago
#ifndef LOGGER_HPP
#define LOGGER_HPP
5 years ago
#include <ctime>
5 years ago
#include <ostream>
#include <sstream>
5 years ago
#include "Singleton_DCLP.hpp"
class Logger : public Singleton_DCLP<Logger>
{
public:
5 years ago
class Endl {};
Logger() : m_os(nullptr), m_buffer(), m_print_time_stamp(false) {}
5 years ago
5 years ago
void setStream(std::ostream* os) { m_os = os; }
5 years ago
void setPrintTimeStamp(bool b = true) { m_print_time_stamp = b; }
template <class T>
Logger &operator<<(const T &x) {
m_buffer << x;
5 years ago
return *this;
}
Logger& operator<<(const Logger::Endl& e) {
decorate();
*m_os << m_buffer.str();
5 years ago
*m_os << std::endl;
m_buffer.str("");
5 years ago
return *this;
}
private:
void decorate()
5 years ago
{
if (m_print_time_stamp) {
const std::time_t timenow = std::time(nullptr);
std::string s(std::ctime(&timenow));
s.pop_back(); // removing trailing \n
*m_os << s << " ";
}
}
5 years ago
// ostreams are not copyable, hence storing a pointer only
std::ostream* m_os;
std::ostringstream m_buffer;
5 years ago
bool m_print_time_stamp;
5 years ago
};
5 years ago
#define LOG *Logger::getInstance()
#define END Logger::Endl();
5 years ago
#endif // LOGGER_HPP