Commit 59cf77bb authored by Max Kellermann's avatar Max Kellermann

log: don't keep log file open

The log file is duped to STDOUT_FILENO and STDERR_FILENO. No need to keep another copy of it in out_fd all the time. We only need it once once in setup_log_output().
parent 8fe03b8b
...@@ -49,15 +49,15 @@ static GLogLevelFlags log_threshold = G_LOG_LEVEL_MESSAGE; ...@@ -49,15 +49,15 @@ static GLogLevelFlags log_threshold = G_LOG_LEVEL_MESSAGE;
static const char *log_charset; static const char *log_charset;
static bool stdout_mode = true; static bool stdout_mode = true;
static int out_fd = -1; static int out_fd;
static const char *out_filename; static const char *out_filename;
static void redirect_logs(void) static void redirect_logs(int fd)
{ {
assert(out_fd >= 0); assert(fd >= 0);
if (dup2(out_fd, STDOUT_FILENO) < 0) if (dup2(fd, STDOUT_FILENO) < 0)
FATAL("problems dup2 stdout : %s\n", strerror(errno)); FATAL("problems dup2 stdout : %s\n", strerror(errno));
if (dup2(out_fd, STDERR_FILENO) < 0) if (dup2(fd, STDERR_FILENO) < 0)
FATAL("problems dup2 stderr : %s\n", strerror(errno)); FATAL("problems dup2 stderr : %s\n", strerror(errno));
} }
...@@ -247,8 +247,11 @@ void setup_log_output(bool use_stdout) ...@@ -247,8 +247,11 @@ void setup_log_output(bool use_stdout)
{ {
fflush(NULL); fflush(NULL);
if (!use_stdout) { if (!use_stdout) {
if (out_filename != NULL) if (out_filename != NULL) {
redirect_logs(); redirect_logs(out_fd);
close(out_fd);
}
stdout_mode = false; stdout_mode = false;
log_charset = NULL; log_charset = NULL;
} }
...@@ -284,6 +287,8 @@ G_GNUC_PRINTF(1, 2) G_GNUC_NORETURN void FATAL(const char *fmt, ...) ...@@ -284,6 +287,8 @@ G_GNUC_PRINTF(1, 2) G_GNUC_NORETURN void FATAL(const char *fmt, ...)
int cycle_log_files(void) int cycle_log_files(void)
{ {
int fd;
if (stdout_mode || out_filename == NULL) if (stdout_mode || out_filename == NULL)
return 0; return 0;
assert(out_filename); assert(out_filename);
...@@ -291,13 +296,13 @@ int cycle_log_files(void) ...@@ -291,13 +296,13 @@ int cycle_log_files(void)
DEBUG("Cycling log files...\n"); DEBUG("Cycling log files...\n");
close_log_files(); close_log_files();
out_fd = open_log_file(); fd = open_log_file();
if (out_fd < 0) { if (fd < 0) {
ERROR("error re-opening log file: %s\n", out_filename); ERROR("error re-opening log file: %s\n", out_filename);
return -1; return -1;
} }
redirect_logs(); redirect_logs(fd);
DEBUG("Done cycling log files\n"); DEBUG("Done cycling log files\n");
return 0; return 0;
} }
...@@ -309,9 +314,5 @@ void close_log_files(void) ...@@ -309,9 +314,5 @@ void close_log_files(void)
if (out_filename == NULL) if (out_filename == NULL)
closelog(); closelog();
else {
assert(out_fd >= 0);
close(out_fd);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment