Rename back to wnoconsole and move boolean check.

This commit is contained in:
Pentarctagon 2023-05-31 12:26:33 -05:00
parent 2548e3b680
commit d3f41831b7
5 changed files with 25 additions and 52 deletions

View file

@ -196,7 +196,7 @@ prevents redirecting logged output to a file. Log files are created in the logs
.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
.B --wnoconsole
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

View file

@ -254,7 +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.")
("wnoconsole", "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(bool no_con);
console_handler();
/**
* 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(bool no_con);
void enable_native_console_output();
};
console_handler::console_handler(bool no_con)
console_handler::console_handler()
: created_wincon_(false)
{
DBG_LS << "Early init message";
@ -84,7 +84,7 @@ console_handler::console_handler(bool no_con)
// 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(no_con);
enable_native_console_output();
}
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(bool no_con)
void console_handler::enable_native_console_output()
{
if(AttachConsole(ATTACH_PARENT_PROCESS)) {
LOG_LS << "Attached parent process console.";
@ -111,20 +111,18 @@ void console_handler::enable_native_console_output(bool no_con)
WRN_LS << "Cannot attach or allocate a console, continuing anyway (is this Wine?)";
}
if(!no_con) {
DBG_LS << "stderr to console";
fflush(stderr);
std::cerr.flush();
assert(freopen("CONOUT$", "wb", stderr) == stderr);
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!";
}
@ -133,10 +131,10 @@ std::unique_ptr<console_handler> lfm;
} // end anonymous namespace
void do_console_redirect(bool no_con)
void do_console_redirect()
{
if(!lfm) {
lfm.reset(new console_handler(no_con));
lfm.reset(new console_handler());
}
}

View file

@ -17,40 +17,13 @@
#include <string>
/**
* @file
* Log file control routines for Windows.
*
* During static object initialization, stdout and stderr are redirected to a
* uniquely-named log file located in the user's temporary directory as defined
* by the platform (e.g. C:/Users/username/AppData/Local/Temp/wesnoth-XXXX.log).
* Later, a request may be issued to relocate the log file to a more permanent
* and user-accessible location (such as the Wesnoth user data directory).
*
* Because Wesnoth is normally built with the GUI subsystem option, there is no
* console on startup and thus no way to see stdout/stderr output. Since
* version 1.13.1, we can allocate a console during initialization when started
* with the --wconsole option (now --no-log-to-file), but that is a somewhat clunky hack that does not
* help with post mortem debugging.
*
* SDL 1.2 used to redirect stdout and stderr to stdout.txt and stderr.txt in
* the process working directory automatically, but this approach too had its
* own shortcomings by assuming the pwd was writable by the process (or in Vista
* and later versions, requiring UAC virtualization to be enabled).
*/
namespace lg
{
/**
* Sets up the initial temporary log file.
*
* This has to be done on demand (preferably as early as possible) from a
* function rather than during static initialization, otherwise things go
* horribly wrong as soon as we try to use the logging facilities internally
* for debug messages.
* Allocates a console if needed and redirects output to CONOUT.
*/
void do_console_redirect(bool no_con);
void do_console_redirect();
/**
* Returns true if a console was allocated by the Wesnoth process.

View file

@ -1113,7 +1113,7 @@ int main(int argc, char** argv)
write_to_log_file = true;
}
if(arg == "--no-con") {
if(arg == "--wnoconsole") {
no_con = true;
}
}
@ -1124,7 +1124,9 @@ int main(int argc, char** argv)
lg::set_log_to_file();
} else {
#ifdef _WIN32
lg::do_console_redirect(no_con);
if(!no_con) {
lg::do_console_redirect();
}
#endif
}