+namespace {
+
+/// Version number. If Version is left empty, then compile date in the format
+/// DD-MM-YY and show in engine_info.
+const string Version = "";
+
+/// Our fancy logging facility. The trick here is to replace cin.rdbuf() and
+/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We
+/// can toggle the logging of std::cout and std:cin at runtime whilst preserving
+/// usual I/O functionality, all without changing a single line of code!
+/// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
+
+struct Tie: public streambuf { // MSVC requires split streambuf for cin and cout
+
+ Tie(streambuf* b, streambuf* l) : buf(b), logBuf(l) {}
+
+ int sync() override { return logBuf->pubsync(), buf->pubsync(); }
+ int overflow(int c) override { return log(buf->sputc((char)c), "<< "); }
+ int underflow() override { return buf->sgetc(); }
+ int uflow() override { return log(buf->sbumpc(), ">> "); }
+
+ streambuf *buf, *logBuf;
+
+ int log(int c, const char* prefix) {
+
+ static int last = '\n'; // Single log file
+
+ if (last == '\n')
+ logBuf->sputn(prefix, 3);
+
+ return last = logBuf->sputc((char)c);
+ }
+};
+
+class Logger {
+
+ Logger() : in(cin.rdbuf(), file.rdbuf()), out(cout.rdbuf(), file.rdbuf()) {}
+ ~Logger() { start(""); }
+
+ ofstream file;
+ Tie in, out;
+
+public:
+ static void start(const std::string& fname) {