Win32: add the --win32log option to log stdout and stderr to files

Adding --win32log as the first command line option on Windows
will now log all stdout and stderr output to the files
subsurface_err.log and subsurface_out.log in the working directory.

This change required a new argument 'bool logfile' to be added to:
subsurface_console_init() which is defined in all platform files
(linux.c, macos.c, etc.)

Example usage:
subsurface.exe --win32log -v -v -v

Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
This commit is contained in:
Lubomir I. Ivanov 2017-02-02 21:50:47 +02:00 committed by Dirk Hohndel
parent 7c8461a328
commit 4a894e0713
7 changed files with 29 additions and 12 deletions

View file

@ -187,8 +187,10 @@ int subsurface_zip_close(struct zip *zip)
} }
/* win32 console */ /* win32 console */
void subsurface_console_init(bool dedicated) void subsurface_console_init(bool dedicated, bool logfile)
{ {
(void)dedicated;
(void)logifle;
/* NOP */ /* NOP */
} }

View file

@ -730,7 +730,7 @@ extern void *subsurface_opendir(const char *path);
extern int subsurface_access(const char *path, int mode); extern int subsurface_access(const char *path, int mode);
extern struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp); extern struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp);
extern int subsurface_zip_close(struct zip *zip); extern int subsurface_zip_close(struct zip *zip);
extern void subsurface_console_init(bool dedicated); extern void subsurface_console_init(bool dedicated, bool logfile);
extern void subsurface_console_exit(void); extern void subsurface_console_exit(void);
extern bool subsurface_user_is_root(void); extern bool subsurface_user_is_root(void);

View file

@ -215,9 +215,10 @@ int subsurface_zip_close(struct zip *zip)
} }
/* win32 console */ /* win32 console */
void subsurface_console_init(bool dedicated) void subsurface_console_init(bool dedicated, bool logfile)
{ {
(void)dedicated; (void)dedicated;
(void)logifle;
/* NOP */ /* NOP */
} }

View file

@ -201,9 +201,10 @@ int subsurface_zip_close(struct zip *zip)
} }
/* win32 console */ /* win32 console */
void subsurface_console_init(bool dedicated) void subsurface_console_init(bool dedicated, bool logfile)
{ {
(void) dedicated; (void)dedicated;
(void)logifle;
/* NOP */ /* NOP */
} }

View file

@ -187,7 +187,8 @@ static void print_help()
printf("\n --survey Offer to submit a user survey"); printf("\n --survey Offer to submit a user survey");
printf("\n --user=<test> Choose configuration space for user <test>"); printf("\n --user=<test> Choose configuration space for user <test>");
printf("\n --cloud-timeout=<nr> Set timeout for cloud connection (0 < timeout < 60)"); printf("\n --cloud-timeout=<nr> Set timeout for cloud connection (0 < timeout < 60)");
printf("\n --win32console Create a dedicated console if needed (Windows only). Add option before everything else\n\n"); printf("\n --win32console Create a dedicated console if needed (Windows only). Add option before everything else");
printf("\n --win32log Write the program output to subsurface.log (Windows only). Add option before everything else\n\n");
} }
void parse_argument(const char *arg) void parse_argument(const char *arg)
@ -245,6 +246,8 @@ void parse_argument(const char *arg)
} }
if (strcmp(arg, "--win32console") == 0) if (strcmp(arg, "--win32console") == 0)
return; return;
if (strcmp(arg, "--win32log") == 0)
return;
/* fallthrough */ /* fallthrough */
case 'p': case 'p':
/* ignore process serial number argument when run as native macosx app */ /* ignore process serial number argument when run as native macosx app */

View file

@ -377,11 +377,12 @@ static struct {
FILE *out, *err; FILE *out, *err;
} console_desc; } console_desc;
void subsurface_console_init(bool dedicated) void subsurface_console_init(bool dedicated, bool logfile)
{ {
(void)console_desc; (void)console_desc;
/* if this is a console app already, do nothing */ /* if this is a console app already, do nothing */
#ifndef WIN32_CONSOLE_APP #ifndef WIN32_CONSOLE_APP
/* just in case of multiple calls */ /* just in case of multiple calls */
memset((void *)&console_desc, 0, sizeof(console_desc)); memset((void *)&console_desc, 0, sizeof(console_desc));
/* the AttachConsole(..) call can be used to determine if the parent process /* the AttachConsole(..) call can be used to determine if the parent process
@ -421,9 +422,12 @@ void subsurface_console_init(bool dedicated)
SetConsoleCtrlHandler(NULL, TRUE); /* disable the CTRL handler */ SetConsoleCtrlHandler(NULL, TRUE); /* disable the CTRL handler */
} }
const char *location_out = logfile ? "subsurface_out.log" : "CON";
const char *location_err = logfile ? "subsurface_err.log" : "CON";
/* redirect; on win32, CON is a reserved pipe target, like NUL */ /* redirect; on win32, CON is a reserved pipe target, like NUL */
console_desc.out = freopen("CON", "w", stdout); console_desc.out = freopen(location_out, "w", stdout);
console_desc.err = freopen("CON", "w", stderr); console_desc.err = freopen(location_err, "w", stderr);
if (!dedicated) if (!dedicated)
puts(""); /* add an empty line */ puts(""); /* add an empty line */
#endif #endif

View file

@ -34,9 +34,15 @@ int main(int argc, char **argv)
QStringList importedFiles; QStringList importedFiles;
QStringList arguments = QCoreApplication::arguments(); QStringList arguments = QCoreApplication::arguments();
bool dedicated_console = arguments.length() > 1 && bool win32_log = arguments.length() > 1 &&
(arguments.at(1) == QString("--win32console")); (arguments.at(1) == QString("--win32log"));
subsurface_console_init(dedicated_console); if (win32_log) {
subsurface_console_init(true, true);
} else {
bool dedicated_console = arguments.length() > 1 &&
(arguments.at(1) == QString("--win32console"));
subsurface_console_init(dedicated_console, false);
}
const char *default_directory = system_default_directory(); const char *default_directory = system_default_directory();
const char *default_filename = system_default_filename(); const char *default_filename = system_default_filename();