|
|
|
#ifndef LOGGER_HPP
|
|
|
|
#define LOGGER_HPP
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "Singleton_DCLP.hpp"
|
|
|
|
|
|
|
|
class Logger : public Singleton_DCLP<Logger>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class Endl {};
|
|
|
|
|
|
|
|
Logger() :
|
|
|
|
m_os(nullptr),
|
|
|
|
m_buffer(),
|
|
|
|
m_print_time_stamp(false),
|
|
|
|
m_print_user_name(false) {}
|
|
|
|
|
|
|
|
void setStream(std::ostream* os) { m_os = os; }
|
|
|
|
|
|
|
|
void setPrintTimeStamp(bool b = true) { m_print_time_stamp = b; }
|
|
|
|
void setPrintUserName(bool b = true) { m_print_user_name = b; }
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
Logger &operator<<(const T &x) {
|
|
|
|
m_buffer << x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger& operator<<(const Logger::Endl& e) {
|
|
|
|
decorate();
|
|
|
|
*m_os << m_buffer.str();
|
|
|
|
*m_os << std::endl;
|
|
|
|
m_buffer.str("");
|
|
|
|
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 << " ";
|
|
|
|
}
|
|
|
|
if (m_print_user_name) {
|
|
|
|
#ifdef linux
|
|
|
|
const uid_t uid = getuid();
|
|
|
|
const struct passwd* p = getpwuid(uid);
|
|
|
|
*m_os << p->pw_name << " ";
|
|
|
|
#elif _win32
|
|
|
|
*m_os << "n00b ";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ostreams are not copyable, hence storing a pointer only
|
|
|
|
std::ostream* m_os;
|
|
|
|
std::ostringstream m_buffer;
|
|
|
|
bool m_print_time_stamp;
|
|
|
|
bool m_print_user_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LOG *Logger::getInstance()
|
|
|
|
#define END Logger::Endl();
|
|
|
|
|
|
|
|
#endif // LOGGER_HPP
|