///////////////////////////////////////////////////// // MT2Q1 - Trace is a std::string // // // // Jim Fawcett, CSE687 - OOD Midterm, Spring 2013 // ///////////////////////////////////////////////////// #include #include #include #include #include "../MTutility/MTutility.h" class Tracer : public std::string { public: Tracer(const std::string& name) : _name(name) {} void log(const std::string& msg); void add(const std::string& msg); private: std::string _name; }; std::string getTime() { using namespace std::chrono; std::time_t tt = system_clock::to_time_t (system_clock::now()); return ctime(&tt); } void Tracer::log(const std::string& msg) { *this += _name + " : " + std::string(getTime()) + msg + "\n"; } void Tracer::add(const std::string& msg) { *this += msg + "\n"; } int main() { Title("MT2Q1 - Tracer as std::string"); std::cout << "\n"; Tracer trace("log"); trace.log("first piece of information"); trace.add("second piece of information"); ::_sleep(2000); trace.log("still more information"); // trace is a string so we can do this: trace += "external insertion : " + getTime() + "outside information"; std::cout << trace << "\n\n"; return 0; }