-#if defined(_MSC_VER)
- struct _timeb t;
- _ftime(&t);
- return int(t.time * 1000 + t.millitm);
-#else
- struct timeval t;
- gettimeofday(&t, NULL);
- return t.tv_sec * 1000 + t.tv_usec / 1000;
-#endif
+ if (last == '\n')
+ file->rdbuf()->sputn(prefix, 3);
+
+ return last = file->rdbuf()->sputc((char)c);
+ }
+};
+
+class Logger {
+
+ Logger() : in(cin.rdbuf(), &file), out(cout.rdbuf(), &file) {}
+ ~Logger() { start(false); }
+
+ ofstream file;
+ Tie in, out;
+
+public:
+ static void start(bool b) {
+
+ static Logger l;
+
+ if (b && !l.file.is_open())
+ {
+ l.file.open("io_log.txt", ifstream::out | ifstream::app);
+ cin.rdbuf(&l.in);
+ cout.rdbuf(&l.out);
+ }
+ else if (!b && l.file.is_open())
+ {
+ cout.rdbuf(l.out.buf);
+ cin.rdbuf(l.in.buf);
+ l.file.close();
+ }
+ }
+};
+
+
+/// Used to serialize access to std::cout to avoid multiple threads to write at
+/// the same time.
+
+std::ostream& operator<<(std::ostream& os, SyncCout sc) {
+
+ static Mutex m;
+
+ if (sc == io_lock)
+ m.lock();
+
+ if (sc == io_unlock)
+ m.unlock();
+
+ return os;