// MT4Q5.cpp - Logging Facility #include #include #include #include #include #include #include #include "../Utilities/Utilities.h" std::string getTimeDate() { //http://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c time_t now = time(0); struct tm tstruct; char buf[80]; errno_t err = localtime_s(&tstruct, &now); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct); return buf; } template class Message { public: Message(const std::string& timeDate, const std::string& test, const T& t); void show(); std::string toString(); private: std::string timeDate_; std::string text_; T t_; }; template Message::Message(const std::string& timeDate, const std::string& text, const T& t) : timeDate_(timeDate), text_(text), t_(t) {} template void Message::show() { std::cout << "\n " << timeDate << " " << text_ << " " << t_.toString(); } template std::string Message::toString() { std::string ret = timeDate_ + " " + text_ + " " + t_.toString(); return ret; } template struct ILog { virtual ~ILog() {} virtual void message(Message& msg) = 0; virtual void show() = 0; virtual bool save() = 0; }; template struct factory { ILog* create(); }; template class Log : public ILog { public: void message(Message& msg); void show(); bool save(); private: static std::vector> log_; }; template std::vector> Log::log_; template ILog* factory::create() { return new Log(); } template void Log::message(Message& msg) { log_.push_back(msg); } template void Log::show() { std::cout << "\n Message Log"; std::cout << "\n -------------"; for (auto msg : log_) { std::cout << "\n " << msg.toString(); } std::cout << "\n"; } template bool Log::save() { std::ofstream out("logfile.txt", std::ios::app); if (out.good()) { for (auto msg : log_) { out << "\n " << msg.toString(); } out.close(); log_.clear(); return true; } return false; } class StringWrapper { public: StringWrapper(const std::string& name) : name_(name) {} std::string toString() { return name_; } private: std::string name_; }; int main() { title("MT1Q2 and MT1Q4 - Message Logger"); Log log; Message msg1(getTimeDate(), " first message", StringWrapper("Jim")); Message msg2(getTimeDate(), "second message", StringWrapper("Jim")); Message msg3(getTimeDate(), " third message", StringWrapper("Jim")); Message msg4(getTimeDate(), "fourth message", StringWrapper("Jim")); log.message(msg1); log.message(msg2); log.message(msg3); log.message(msg4); log.show(); log.save(); log.show(); std::cout << "\n"; factory fact; ILog* pLog = fact.create(); Message msg5(getTimeDate(), " fifth message", StringWrapper("Taron")); Message msg6(getTimeDate(), " sixth message", StringWrapper("Rakesh")); Message msg7(getTimeDate(), "seventh message", StringWrapper("Zhichen")); Message msg8(getTimeDate(), " eigth message", StringWrapper("Xing")); Message msg9(getTimeDate(), " ninth message", StringWrapper("Vishal")); Message msg10(getTimeDate(), " tenth message", StringWrapper("Elie")); pLog->message(msg5); pLog->message(msg6); pLog->message(msg7); pLog->message(msg8); pLog->message(msg9); pLog->message(msg10); pLog->show(); std::cout << "\n\n"; }