#include #include "logger.h" using SSMD::Logger; bool Logger::setLogFile(const std::string & fileName) { fout.open(fileName.c_str(), std::ios::app); logFile = fileName; return !(consoleLog = !fout.is_open()); } std::ostream & Logger::operator<<(const std::string & str) { _logDate(); if (consoleLog) { return std::cout << str; } else { fout.close(); fout.open(logFile.c_str(), std::ios::app); if (fout) { fout << str; } return fout; } } inline void Logger::_logDate() { time_t t = time(NULL); struct tm *ts = localtime(&t); if (consoleLog) { std::cout << "[" << (ts->tm_year + 1900) << "-" << (ts->tm_mon < 9 ? "0" : "") << (ts->tm_mon + 1) << "-" << (ts->tm_mday < 10 ? "0" : "") << ts->tm_mday << " " << (ts->tm_hour < 10 ? "0" : "") << ts->tm_hour << ":" << (ts->tm_min < 10 ? "0" : "") << ts->tm_min << ":" << (ts->tm_sec < 10 ? "0" : "") << ts->tm_sec << "] "; } else { fout.close(); fout.open(logFile.c_str(), std::ios::app); if (fout) { fout << "[" << (ts->tm_year + 1900) << "-" << (ts->tm_mon < 9 ? "0" : "") << (ts->tm_mon + 1) << "-" << (ts->tm_mday < 10 ? "0" : "") << ts->tm_mday << " " << (ts->tm_hour < 10 ? "0" : "") << ts->tm_hour << ":" << (ts->tm_min < 10 ? "0" : "") << ts->tm_min << ":" << (ts->tm_sec < 10 ? "0" : "") << ts->tm_sec << "] "; } } }