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.

53 lines
1002 B

#ifndef LOGGER_HPP
#define LOGGER_HPP
#include <ostream>
#include <ctime>
#include "Singleton_DCLP.hpp"
class Logger : public Singleton_DCLP<Logger>
{
public:
class Endl {};
Logger() : m_os(nullptr), m_print_time_stamp(false) {}
void setStream(std::ostream* os) { m_os = os; }
std::ostream* getStream() { return m_os; }
void setPrintTimeStamp(bool b = true) { m_print_time_stamp = b; }
template <class T>
Logger &operator<<(const T &x) {
decorate();
*m_os << x;
return *this;
}
Logger& operator<<(const Logger::Endl& e) {
*m_os << std::endl;
return *this;
}
private:
void decorate()
{
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 << " ";
}
}
// ostreams are not copyable, hence storing a pointer only
std::ostream* m_os;
bool m_print_time_stamp;
};
#define LOG *Logger::getInstance()
#define END Logger::Endl();
#endif // LOGGER_HPP