From 70298b63d0c99be1616c2bdff1afcdea2290c39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Sat, 3 May 2025 12:02:53 +0200 Subject: [PATCH] Print messages to stdout even if the console is open The idea behind not calling GameModel::Log in tpt.log is that that would not only print the message to stdout but also add it to GameView's list of log entries, which the console doesn't want. Sadly, this meant that messages also didn't make it to stdout when the console was open. --- src/gui/game/GameModel.cpp | 15 +++++++++++---- src/gui/game/GameModel.h | 4 ++++ src/lua/LuaMisc.cpp | 12 +----------- src/lua/LuaScriptInterface.cpp | 12 ++++++++++-- src/lua/LuaScriptInterface.h | 1 - 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/gui/game/GameModel.cpp b/src/gui/game/GameModel.cpp index 847d9c93b..2ed0c9b3c 100644 --- a/src/gui/game/GameModel.cpp +++ b/src/gui/game/GameModel.cpp @@ -1159,10 +1159,17 @@ const GameSave *GameModel::GetTransformedPlaceSave() const void GameModel::Log(String message, bool printToFile) { - consoleLog.push_front(message); - if(consoleLog.size()>100) - consoleLog.pop_back(); - notifyLogChanged(message); + if (logSink) + { + logSink(message); + } + else + { + consoleLog.push_front(message); + if(consoleLog.size()>100) + consoleLog.pop_back(); + notifyLogChanged(message); + } if (printToFile) std::cout << format::CleanString(message, false, true, false).ToUtf8() << std::endl; } diff --git a/src/gui/game/GameModel.h b/src/gui/game/GameModel.h index 085e41a4d..15e218d4c 100644 --- a/src/gui/game/GameModel.h +++ b/src/gui/game/GameModel.h @@ -8,6 +8,7 @@ #include #include #include +#include #include constexpr auto NUM_TOOLINDICES = 4; @@ -276,7 +277,10 @@ public: void SetClipboard(std::unique_ptr save); void SetPlaceSave(std::unique_ptr save); void TransformPlaceSave(Mat2 transform, Vec2 nudge); + + std::function logSink; void Log(String message, bool printToFile); + std::deque GetLog(); const GameSave *GetClipboard() const; const GameSave *GetPlaceSave() const; diff --git a/src/lua/LuaMisc.cpp b/src/lua/LuaMisc.cpp index 800c3147c..168576979 100644 --- a/src/lua/LuaMisc.cpp +++ b/src/lua/LuaMisc.cpp @@ -138,17 +138,7 @@ static int flog(lua_State *L) } lua_pop(L, 2); } - if (lsi->currentCommand) - { - auto lastError = lsi->GetLastError(); - if (lsi->luacon_hasLastError) - lastError += "; "; - lastError += text; - lsi->SetLastError(lastError); - lsi->luacon_hasLastError = true; - } - else - lsi->Log(CommandInterface::LogNotice, text); + lsi->Log(CommandInterface::LogNotice, text); return 0; } diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index f1e8a3ce2..20afbddd6 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -507,7 +507,15 @@ int CommandInterface::Command(String command) else { int level = lua_gettop(L), ret = -1; - lsi->currentCommand = true; + lsi->gameModel->logSink = [this](String text) { + auto *lsi = static_cast(this); + auto lastError = GetLastError(); + if (lsi->luacon_hasLastError) + lastError += "; "; + lastError += text; + SetLastError(lastError); + lsi->luacon_hasLastError = true; + }; if (lsi->lastCode.length()) lsi->lastCode += "\n"; lsi->lastCode += command; @@ -564,7 +572,7 @@ int CommandInterface::Command(String command) } } - lsi->currentCommand = false; + lsi->gameModel->logSink = nullptr; return ret; } } diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index 1bcf159a4..96fc4df73 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -102,7 +102,6 @@ public: bool luacon_hasLastError = false; String lastCode; - bool currentCommand = false; int textInputRefcount = 0; long unsigned int luaExecutionStart = 0;