Add --no-con to disable writing to CONOUT.

Fixes #7619
This commit is contained in:
Pentarctagon 2023-05-27 22:11:07 -05:00 committed by Pentarctagon
parent 5b53be5d2a
commit 10f7ef26e7
5 changed files with 34 additions and 19 deletions

View file

@ -193,6 +193,12 @@ if used) and exits
.B --no-log-to-file
prevents redirecting logged output to a file. Log files are created in the logs directory under the userdata folder.
.TP
.B --log-to-file
log output is written to a file even if it would normally go to standard output/error. Cancels the effect of --no-log-to-file whether implicit or explicit.
.TP
.B --no-con
For Windows, when used with --no-log-to-file, results in output being written to cerr/cout instead of CONOUT. Otherwise, does nothing.
.TP
.BI --max-fps \ fps
the number of frames per second the game can show, the value should be between
.B 1

View file

@ -254,6 +254,7 @@ commandline_options::commandline_options(const std::vector<std::string>& args)
("log-precise", "shows the timestamps in log output with more precision.")
("no-log-to-file", "log output is written to standard error rather than to a file.")
("log-to-file", "log output is written to a file even if it would normally go to standard output/error. Cancels the effect of --no-log-to-file whether implicit or explicit.")
("no-con", "For Windows, when used with --no-log-to-file, results in output being written to cerr/cout instead of CONOUT. Otherwise, does nothing.")
;
po::options_description multiplayer_opts("Multiplayer options");

View file

@ -57,7 +57,7 @@ public:
console_handler(const console_handler&) = delete;
console_handler& operator=(const console_handler&) = delete;
console_handler();
console_handler(bool no_con);
/**
* Returns whether we own the console we are attached to, if any.
@ -70,10 +70,10 @@ private:
/**
* Switches to using a native console.
*/
void enable_native_console_output();
void enable_native_console_output(bool no_con);
};
console_handler::console_handler()
console_handler::console_handler(bool no_con)
: created_wincon_(false)
{
DBG_LS << "Early init message";
@ -84,7 +84,7 @@ console_handler::console_handler()
// already pointing to the console.
LOG_LS << "Console already attached at startup (built with console subsystem flag?), log file disabled.";
} else {
enable_native_console_output();
enable_native_console_output(no_con);
}
DBG_LS << "Windows console init complete!";
@ -95,7 +95,7 @@ bool console_handler::owns_console() const
return created_wincon_;
}
void console_handler::enable_native_console_output()
void console_handler::enable_native_console_output(bool no_con)
{
if(AttachConsole(ATTACH_PARENT_PROCESS)) {
LOG_LS << "Attached parent process console.";
@ -111,18 +111,20 @@ void console_handler::enable_native_console_output()
WRN_LS << "Cannot attach or allocate a console, continuing anyway (is this Wine?)";
}
DBG_LS << "stderr to console";
fflush(stderr);
std::cerr.flush();
assert(freopen("CONOUT$", "wb", stderr) == stderr);
if(!no_con) {
DBG_LS << "stderr to console";
fflush(stderr);
std::cerr.flush();
assert(freopen("CONOUT$", "wb", stderr) == stderr);
DBG_LS << "stdout to console";
fflush(stdout);
std::cout.flush();
assert(freopen("CONOUT$", "wb", stdout) == stdout);
DBG_LS << "stdout to console";
fflush(stdout);
std::cout.flush();
assert(freopen("CONOUT$", "wb", stdout) == stdout);
DBG_LS << "stdin from console";
assert(freopen("CONIN$", "rb", stdin) == stdin);
DBG_LS << "stdin from console";
assert(freopen("CONIN$", "rb", stdin) == stdin);
}
LOG_LS << "Console streams handover complete!";
}
@ -131,10 +133,10 @@ std::unique_ptr<console_handler> lfm;
} // end anonymous namespace
void do_console_redirect()
void do_console_redirect(bool no_con)
{
if(!lfm) {
lfm.reset(new console_handler());
lfm.reset(new console_handler(no_con));
}
}

View file

@ -50,7 +50,7 @@ namespace lg
* horribly wrong as soon as we try to use the logging facilities internally
* for debug messages.
*/
void do_console_redirect();
void do_console_redirect(bool no_con);
/**
* Returns true if a console was allocated by the Wesnoth process.

View file

@ -1060,6 +1060,8 @@ int main(int argc, char** argv)
// if false, output will be written to the terminal
// on windows, if wesnoth was not started from a console, then it will allocate one
bool write_to_log_file = true;
[[maybe_unused]]
bool no_con = false;
// --nobanner needs to be detected before the main command-line parsing happens
// --log-to needs to be detected so the logging output location is set before any actual logging happens
@ -1110,6 +1112,10 @@ int main(int argc, char** argv)
} else if(arg == "--log-to-file") {
write_to_log_file = true;
}
if(arg == "--no-con") {
no_con = true;
}
}
// setup logging to file
@ -1118,7 +1124,7 @@ int main(int argc, char** argv)
lg::set_log_to_file();
} else {
#ifdef _WIN32
lg::do_console_redirect();
lg::do_console_redirect(no_con);
#endif
}