Skip to content

Commit

Permalink
Don't redirect stdout for standalone exe invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
cgutman committed Sep 7, 2020
1 parent 851eac0 commit f7de095
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions GSv6Fwd/GSv6Fwd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,25 +833,28 @@ void UpdatePcpPinholes()
free(addresses);
}

void ResetLogFile()
void ResetLogFile(bool standaloneExe)
{
char oldLogFilePath[MAX_PATH + 1];
char currentLogFilePath[MAX_PATH + 1];
char timeString[MAX_PATH + 1] = {};
SYSTEMTIME time;

ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-old.log", oldLogFilePath, sizeof(oldLogFilePath));
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-current.log", currentLogFilePath, sizeof(currentLogFilePath));
if (!standaloneExe) {
char oldLogFilePath[MAX_PATH + 1];
char currentLogFilePath[MAX_PATH + 1];

// Close the existing stdout handle. This is important because otherwise
// it may still be open as stdout when we try to MoveFileEx below.
fclose(stdout);
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-old.log", oldLogFilePath, sizeof(oldLogFilePath));
ExpandEnvironmentStringsA("%ProgramData%\\MISS\\GSv6Fwd-current.log", currentLogFilePath, sizeof(currentLogFilePath));

// Rotate the current to the old log file
MoveFileExA(currentLogFilePath, oldLogFilePath, MOVEFILE_REPLACE_EXISTING);
// Close the existing stdout handle. This is important because otherwise
// it may still be open as stdout when we try to MoveFileEx below.
fclose(stdout);

// Redirect stdout to this new file
freopen(currentLogFilePath, "w", stdout);
// Rotate the current to the old log file
MoveFileExA(currentLogFilePath, oldLogFilePath, MOVEFILE_REPLACE_EXISTING);

// Redirect stdout to this new file
freopen(currentLogFilePath, "w", stdout);
}

// Print a log header
printf("IPv6 Forwarder for GameStream v" VER_VERSION_STR "\n");
Expand All @@ -862,12 +865,12 @@ void ResetLogFile()
printf("The current UTC time is: %s\n", timeString);
}

int Run(void)
int Run(bool standaloneExe)
{
int err;
WSADATA data;

ResetLogFile();
ResetLogFile(standaloneExe);

HANDLE ifaceChangeEvent = CreateEvent(nullptr, true, false, nullptr);

Expand Down Expand Up @@ -912,7 +915,7 @@ int Run(void)
break;
}

ResetLogFile();
ResetLogFile(standaloneExe);
}

return 0;
Expand Down Expand Up @@ -964,7 +967,7 @@ ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
SetServiceStatus(ServiceStatusHandle, &ServiceStatus);

// Start the relay
err = Run();
err = Run(false);
if (err != 0) {
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = err;
Expand All @@ -982,7 +985,7 @@ static const SERVICE_TABLE_ENTRY ServiceTable[] = {
int main(int argc, char* argv[])
{
if (argc == 2 && !strcmp(argv[1], "exe")) {
Run();
Run(true);
return 0;
}

Expand Down

0 comments on commit f7de095

Please sign in to comment.