#include "Logger.h" #include "Common.h" #include #include #include #include using namespace std; const char* Months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"}; namespace Logger { string _logFile; void setLogfile(std::string file) { _logFile = file; } void logMessage(string msg) { Common* cmn = Common::instance(); time_t t = time(0); tm now; localtime_r(&t, &now); ostringstream out; out << Months[ now.tm_mon ] << " " << setw(2) << setfill('0') << now.tm_mday; out << " " << setw(2) << setfill('0') << now.tm_hour; out << ":" << setw(2) << setfill('0') << now.tm_min; out << ":" << setw(2) << setfill('0') << now.tm_sec; out << " " << msg << endl; if (cmn->isDaemon && _logFile != "" && cmn->daemonized) { seteuid(0); ofstream of( _logFile.c_str(), ios_base::out | ios_base::app | ios_base::ate);//append mode if (of) { of << out.str(); of.flush(); of.close(); } seteuid( cmn->uid); } else { cout << out.str(); cout.flush(); } } }