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.
This commit is contained in:
Tamás Bálint Misius
2025-05-03 12:02:53 +02:00
parent e301c7fe01
commit 70298b63d0
5 changed files with 26 additions and 18 deletions

View File

@@ -1159,10 +1159,17 @@ const GameSave *GameModel::GetTransformedPlaceSave() const
void GameModel::Log(String message, bool printToFile) void GameModel::Log(String message, bool printToFile)
{ {
consoleLog.push_front(message); if (logSink)
if(consoleLog.size()>100) {
consoleLog.pop_back(); logSink(message);
notifyLogChanged(message); }
else
{
consoleLog.push_front(message);
if(consoleLog.size()>100)
consoleLog.pop_back();
notifyLogChanged(message);
}
if (printToFile) if (printToFile)
std::cout << format::CleanString(message, false, true, false).ToUtf8() << std::endl; std::cout << format::CleanString(message, false, true, false).ToUtf8() << std::endl;
} }

View File

@@ -8,6 +8,7 @@
#include <deque> #include <deque>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <functional>
#include <array> #include <array>
constexpr auto NUM_TOOLINDICES = 4; constexpr auto NUM_TOOLINDICES = 4;
@@ -276,7 +277,10 @@ public:
void SetClipboard(std::unique_ptr<GameSave> save); void SetClipboard(std::unique_ptr<GameSave> save);
void SetPlaceSave(std::unique_ptr<GameSave> save); void SetPlaceSave(std::unique_ptr<GameSave> save);
void TransformPlaceSave(Mat2<int> transform, Vec2<int> nudge); void TransformPlaceSave(Mat2<int> transform, Vec2<int> nudge);
std::function<void (String)> logSink;
void Log(String message, bool printToFile); void Log(String message, bool printToFile);
std::deque<String> GetLog(); std::deque<String> GetLog();
const GameSave *GetClipboard() const; const GameSave *GetClipboard() const;
const GameSave *GetPlaceSave() const; const GameSave *GetPlaceSave() const;

View File

@@ -138,17 +138,7 @@ static int flog(lua_State *L)
} }
lua_pop(L, 2); lua_pop(L, 2);
} }
if (lsi->currentCommand) lsi->Log(CommandInterface::LogNotice, text);
{
auto lastError = lsi->GetLastError();
if (lsi->luacon_hasLastError)
lastError += "; ";
lastError += text;
lsi->SetLastError(lastError);
lsi->luacon_hasLastError = true;
}
else
lsi->Log(CommandInterface::LogNotice, text);
return 0; return 0;
} }

View File

@@ -507,7 +507,15 @@ int CommandInterface::Command(String command)
else else
{ {
int level = lua_gettop(L), ret = -1; int level = lua_gettop(L), ret = -1;
lsi->currentCommand = true; lsi->gameModel->logSink = [this](String text) {
auto *lsi = static_cast<LuaScriptInterface *>(this);
auto lastError = GetLastError();
if (lsi->luacon_hasLastError)
lastError += "; ";
lastError += text;
SetLastError(lastError);
lsi->luacon_hasLastError = true;
};
if (lsi->lastCode.length()) if (lsi->lastCode.length())
lsi->lastCode += "\n"; lsi->lastCode += "\n";
lsi->lastCode += command; lsi->lastCode += command;
@@ -564,7 +572,7 @@ int CommandInterface::Command(String command)
} }
} }
lsi->currentCommand = false; lsi->gameModel->logSink = nullptr;
return ret; return ret;
} }
} }

View File

@@ -102,7 +102,6 @@ public:
bool luacon_hasLastError = false; bool luacon_hasLastError = false;
String lastCode; String lastCode;
bool currentCommand = false;
int textInputRefcount = 0; int textInputRefcount = 0;
long unsigned int luaExecutionStart = 0; long unsigned int luaExecutionStart = 0;