summaryrefslogtreecommitdiff
path: root/src/dedicated.cpp
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2020-05-17 23:32:10 +0200
committerMichael Lutz <michi@icosahedron.de>2020-05-21 20:02:34 +0200
commitc972a63c8cbee7fa8d6d5af2cbbecb8c75ee561a (patch)
tree70d5b6c477666170f07469ba769adc4ec629a91c /src/dedicated.cpp
parent37bc2f806462b3c2a84891b3aad6db00e935da86 (diff)
downloadopenttd-c972a63c8cbee7fa8d6d5af2cbbecb8c75ee561a.tar.xz
Codechange: Store info about the dedicated server log file in globals with automatic destruction to simplify control flow in openttd_main.
Diffstat (limited to 'src/dedicated.cpp')
-rw-r--r--src/dedicated.cpp14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/dedicated.cpp b/src/dedicated.cpp
index 8ab035e61..fe3cd7cfb 100644
--- a/src/dedicated.cpp
+++ b/src/dedicated.cpp
@@ -8,9 +8,11 @@
/** @file dedicated.cpp Forking support for dedicated servers. */
#include "stdafx.h"
+#include "fileio_func.h"
+#include <string>
-char *_log_file = nullptr; ///< File to reroute output of a forked OpenTTD to
-FILE *_log_fd = nullptr; ///< File to reroute output of a forked OpenTTD to
+std::string _log_file; ///< 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)
@@ -38,17 +40,17 @@ void DedicatedFork()
case 0: { // We're the child
/* Open the log-file to log all stuff too */
- _log_fd = fopen(_log_file, "a");
- if (_log_fd == nullptr) {
+ _log_fd.reset(fopen(_log_file.c_str(), "a"));
+ if (!_log_fd) {
perror("Unable to open logfile");
exit(1);
}
/* 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");
exit(1);
}
- if (dup2(fileno(_log_fd), fileno(stderr)) == -1) {
+ if (dup2(fileno(_log_fd.get()), fileno(stderr)) == -1) {
perror("Rerouting stderr");
exit(1);
}