Răsfoiți Sursa

cout is now duplicated into a log file

Kolja Strohm 1 an în urmă
părinte
comite
920a034a5e
2 a modificat fișierele cu 30 adăugiri și 1 ștergeri
  1. 2 0
      .gitignore
  2. 28 1
      FactoryCraft/Start.cpp

+ 2 - 0
.gitignore

@@ -364,3 +364,5 @@ FodyWeavers.xsd
 /Windows Version/saves/Test
 /Windows Version/cert.crt
 /Windows Version/cert.key
+/Windows Version/Framework.dll
+/Windows Version/Network.dll

+ 28 - 1
FactoryCraft/Start.cpp

@@ -27,6 +27,30 @@ void exit()
     }
 }
 
+class DuplicatingStreamBuf : public std::stringbuf
+{
+private:
+    std::ostream& m_os1;
+    std::ostream& m_os2;
+    Critical cs;
+
+public:
+    DuplicatingStreamBuf(std::ostream& os1, std::ostream& os2)
+        : std::stringbuf(),
+          m_os1(os1),
+          m_os2(os2)
+    {}
+
+    int sync() override
+    {
+        cs.lock();
+        m_os1 << str();
+        m_os2 << str();
+        str("");
+        cs.unlock();
+    }
+};
+
 int main()
 {
     Framework::initFramework();
@@ -52,7 +76,9 @@ int main()
     file.open(pfad->getText());
     std::streambuf* sbuf = std::cout.rdbuf();
     #ifndef _DEBUG
-    std::cout.rdbuf( file.rdbuf() );
+    std::ostream newCout(sbuf);
+    DuplicatingStreamBuf duplicator(file, newCout);
+    std::cout.rdbuf(&duplicator);
     #endif
     pfad->release();
 
@@ -99,6 +125,7 @@ int main()
         {
             std::string line;
             std::getline(std::cin, line);
+            if (!mserver) return;
             if (Text(line.c_str()) == Text("exit"))
             {
                 std::cout << "The server will be terminated and the game progress will be saved.\n";