1
0
Fork 0

Codechange: Store info about the dedicated server log file in globals with automatic destruction to simplify control flow in openttd_main.

pull/8170/head
Michael Lutz 2020-05-17 23:32:10 +02:00
parent 37bc2f8064
commit c972a63c8c
4 changed files with 26 additions and 30 deletions

View File

@ -8,9 +8,11 @@
/** @file dedicated.cpp Forking support for dedicated servers. */ /** @file dedicated.cpp Forking support for dedicated servers. */
#include "stdafx.h" #include "stdafx.h"
#include "fileio_func.h"
#include <string>
char *_log_file = nullptr; ///< File to reroute output of a forked OpenTTD to std::string _log_file; ///< File to reroute output of a forked OpenTTD to
FILE *_log_fd = nullptr; ///< File to reroute output of a forked OpenTTD to std::unique_ptr<FILE, FileDeleter> _log_fd; ///< File to reroute output of a forked OpenTTD to
#if defined(UNIX) #if defined(UNIX)
@ -38,17 +40,17 @@ void DedicatedFork()
case 0: { // We're the child case 0: { // We're the child
/* Open the log-file to log all stuff too */ /* Open the log-file to log all stuff too */
_log_fd = fopen(_log_file, "a"); _log_fd.reset(fopen(_log_file.c_str(), "a"));
if (_log_fd == nullptr) { if (!_log_fd) {
perror("Unable to open logfile"); perror("Unable to open logfile");
exit(1); exit(1);
} }
/* Redirect stdout and stderr to log-file */ /* Redirect stdout and stderr to log-file */
if (dup2(fileno(_log_fd), fileno(stdout)) == -1) { if (dup2(fileno(_log_fd.get()), fileno(stdout)) == -1) {
perror("Rerouting stdout"); perror("Rerouting stdout");
exit(1); exit(1);
} }
if (dup2(fileno(_log_fd), fileno(stderr)) == -1) { if (dup2(fileno(_log_fd.get()), fileno(stderr)) == -1) {
perror("Rerouting stderr"); perror("Rerouting stderr");
exit(1); exit(1);
} }

View File

@ -1245,8 +1245,9 @@ void DeterminePaths(const char *exe)
free(tmp); free(tmp);
} }
extern char *_log_file; extern std::string _log_file;
_log_file = str_fmt("%sopenttd.log", _personal_dir); _log_file = _personal_dir;
_log_file += "openttd.log";
} }
/** /**

View File

@ -159,4 +159,12 @@ public:
} }
}; };
/** Helper to manage a FILE with a \c std::unique_ptr. */
struct FileDeleter {
void operator()(FILE *f)
{
if (f) fclose(f);
}
};
#endif /* FILEIO_FUNC_H */ #endif /* FILEIO_FUNC_H */

View File

@ -543,7 +543,7 @@ int openttd_main(int argc, char *argv[])
Dimension resolution = {0, 0}; Dimension resolution = {0, 0};
/* AfterNewGRFScan sets save_config to true after scanning completed. */ /* AfterNewGRFScan sets save_config to true after scanning completed. */
bool save_config = false; bool save_config = false;
AfterNewGRFScan *scanner = new AfterNewGRFScan(&save_config); std::unique_ptr<AfterNewGRFScan> scanner(new AfterNewGRFScan(&save_config));
bool dedicated = false; bool dedicated = false;
char *debuglog_conn = nullptr; char *debuglog_conn = nullptr;
@ -637,7 +637,7 @@ int openttd_main(int argc, char *argv[])
DeterminePaths(argv[0]); DeterminePaths(argv[0]);
if (StrEmpty(mgo.opt)) { if (StrEmpty(mgo.opt)) {
ret = 1; ret = 1;
goto exit_noshutdown; return ret;
} }
char title[80]; char title[80];
@ -654,12 +654,11 @@ int openttd_main(int argc, char *argv[])
GetString(buf, _load_check_data.error, lastof(buf)); GetString(buf, _load_check_data.error, lastof(buf));
fprintf(stderr, "%s\n", buf); fprintf(stderr, "%s\n", buf);
} }
goto exit_noshutdown; return ret;
} }
WriteSavegameInfo(title); WriteSavegameInfo(title);
return ret;
goto exit_noshutdown;
} }
case 'G': scanner->generation_seed = strtoul(mgo.opt, nullptr, 10); break; case 'G': scanner->generation_seed = strtoul(mgo.opt, nullptr, 10); break;
case 'c': free(_config_file); _config_file = stredup(mgo.opt); break; case 'c': free(_config_file); _config_file = stredup(mgo.opt); break;
@ -683,8 +682,7 @@ int openttd_main(int argc, char *argv[])
BaseSounds::FindSets(); BaseSounds::FindSets();
BaseMusic::FindSets(); BaseMusic::FindSets();
ShowHelp(); ShowHelp();
return ret;
goto exit_noshutdown;
} }
DeterminePaths(argv[0]); DeterminePaths(argv[0]);
@ -785,8 +783,7 @@ int openttd_main(int argc, char *argv[])
if (!HandleBootstrap()) { if (!HandleBootstrap()) {
ShutdownGame(); ShutdownGame();
return ret;
goto exit_bootstrap;
} }
VideoDriver::GetInstance()->ClaimMousePointer(); VideoDriver::GetInstance()->ClaimMousePointer();
@ -842,8 +839,7 @@ int openttd_main(int argc, char *argv[])
CheckForMissingGlyphs(); CheckForMissingGlyphs();
/* ScanNewGRFFiles now has control over the scanner. */ /* ScanNewGRFFiles now has control over the scanner. */
ScanNewGRFFiles(scanner); ScanNewGRFFiles(scanner.release());
scanner = nullptr;
VideoDriver::GetInstance()->MainLoop(); VideoDriver::GetInstance()->MainLoop();
@ -860,17 +856,6 @@ int openttd_main(int argc, char *argv[])
/* Reset windowing system, stop drivers, free used memory, ... */ /* Reset windowing system, stop drivers, free used memory, ... */
ShutdownGame(); ShutdownGame();
exit_noshutdown:
exit_bootstrap:
delete scanner;
extern FILE *_log_fd;
if (_log_fd != nullptr) {
fclose(_log_fd);
}
return ret; return ret;
} }