Skip to content

Commit

Permalink
Add a config option for standard stream redirection to log files
Browse files Browse the repository at this point in the history
Equivalent to the redirect command line option.
  • Loading branch information
LBPHacker committed Dec 19, 2024
1 parent f580a0b commit 2f2d24b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/PowderToy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ int Main(int argc, char *argv[])
prefs.Set("Fullscreen", windowFrameOps.fullscreen);
}

if (true_arg(arguments["redirect"]))
auto redirectStd = prefs.Get("RedirectStd", false);
if (true_arg(arguments["redirect"]) || redirectStd)
{
FILE *new_stdout = freopen("stdout.log", "w", stdout);
FILE *new_stderr = freopen("stderr.log", "w", stderr);
Expand Down Expand Up @@ -428,6 +429,7 @@ int Main(int argc, char *argv[])

explicitSingletons->client = std::make_unique<Client>();
Client::Ref().Initialize();
Client::Ref().SetRedirectStd(redirectStd);

explicitSingletons->saveRenderer = std::make_unique<SaveRenderer>();
explicitSingletons->favorite = std::make_unique<Favorite>();
Expand Down
11 changes: 11 additions & 0 deletions src/client/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Client: public ExplicitSingleton<Client> {
std::optional<UpdateInfo> updateInfo;

bool firstRun;
bool redirectStd = false;

std::vector<ByteString> stampIDs;
uint64_t lastStampTime = 0;
Expand Down Expand Up @@ -104,4 +105,14 @@ class Client: public ExplicitSingleton<Client> {
void Tick();

String DoMigration(ByteString fromDir, ByteString toDir);

bool GetRedirectStd()
{
return redirectStd;
}

void SetRedirectStd(bool newRedirectStd)
{
redirectStd = newRedirectStd;
}
};
5 changes: 5 additions & 0 deletions src/gui/options/OptionsController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ void OptionsController::SetMomentumScroll(bool momentumScroll)
model->SetMomentumScroll(momentumScroll);
}

void OptionsController::SetRedirectStd(bool newRedirectStd)
{
model->SetRedirectStd(newRedirectStd);
}

void OptionsController::Exit()
{
view->CloseActiveWindow();
Expand Down
1 change: 1 addition & 0 deletions src/gui/options/OptionsController.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class OptionsController
void SetIncludePressure(bool includePressure);
void SetPerfectCircle(bool perfectCircle);
void SetMomentumScroll(bool momentumScroll);
void SetRedirectStd(bool newRedirectStd);

void Exit();
OptionsView * GetView();
Expand Down
13 changes: 13 additions & 0 deletions src/gui/options/OptionsModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "common/clipboard/Clipboard.h"
#include "gui/interface/Engine.h"
#include "gui/game/GameModel.h"
#include "client/Client.h"

OptionsModel::OptionsModel(GameModel * gModel_) {
gModel = gModel_;
Expand Down Expand Up @@ -338,6 +339,18 @@ void OptionsModel::SetMomentumScroll(bool state)
notifySettingsChanged();
}

bool OptionsModel::GetRedirectStd()
{
return Client::Ref().GetRedirectStd();
}

void OptionsModel::SetRedirectStd(bool newRedirectStd)
{
GlobalPrefs::Ref().Set("RedirectStd", newRedirectStd);
Client::Ref().SetRedirectStd(newRedirectStd);
notifySettingsChanged();
}

void OptionsModel::notifySettingsChanged()
{
for (size_t i = 0; i < observers.size(); i++)
Expand Down
2 changes: 2 additions & 0 deletions src/gui/options/OptionsModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,7 @@ class OptionsModel
void SetPerfectCircle(bool perfectCircle);
bool GetMomentumScroll();
void SetMomentumScroll(bool momentumScroll);
bool GetRedirectStd();
void SetRedirectStd(bool newRedirectStd);
virtual ~OptionsModel();
};
4 changes: 4 additions & 0 deletions src/gui/options/OptionsView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ OptionsView::OptionsView() : ui::Window(ui::Point(-1, -1), ui::Point(320, 340))
}
currentY += 26;
}
redirectStd = addCheckbox(0, "Save errors and other messages to a file", "Developers may ask for this when trying to fix problems", [this] {
c->SetRedirectStd(redirectStd->GetChecked());
});

{
addSeparator();
Expand Down Expand Up @@ -511,6 +514,7 @@ void OptionsView::NotifySettingsChanged(OptionsModel * sender)
graveExitsConsole->SetChecked(sender->GetGraveExitsConsole());
threadedRendering->SetChecked(sender->GetThreadedRendering());
momentumScroll->SetChecked(sender->GetMomentumScroll());
redirectStd->SetChecked(sender->GetRedirectStd());
}

void OptionsView::AttachController(OptionsController * c_)
Expand Down
1 change: 1 addition & 0 deletions src/gui/options/OptionsView.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class OptionsView: public ui::Window
ui::Checkbox *graveExitsConsole{};
ui::Checkbox *nativeClipoard{};
ui::Checkbox *threadedRendering{};
ui::Checkbox *redirectStd{};
ui::ScrollPanel *scrollPanel{};
float customGravityX, customGravityY;
void UpdateAmbientAirTempPreview(float airTemp, bool isValid);
Expand Down

0 comments on commit 2f2d24b

Please sign in to comment.