+ struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout
+
+ Tie(streambuf* b, ofstream& f) : buf(b), file(f) {}
+
+ int sync() { return file.rdbuf()->pubsync(), buf->pubsync(); }
+ int overflow(int c) { return log(buf->sputc((char)c), "<< "); }
+ int underflow() { return buf->sgetc(); }
+ int uflow() { return log(buf->sbumpc(), ">> "); }
+
+ int log(int c, const char* prefix) {
+
+ static int last = '\n';
+
+ if (last == '\n')
+ file.rdbuf()->sputn(prefix, 3);
+
+ return last = file.rdbuf()->sputc((char)c);
+ }
+
+ streambuf* buf;
+ ofstream& file;
+ };
+
+ ofstream file;
+ Tie in, out;
+