aboutsummaryrefslogtreecommitdiff
path: root/src/log.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-12-28 19:48:53 +0100
committerMax Kellermann <max@duempel.org>2008-12-28 19:48:53 +0100
commit59cf77bbc81175eb77d5bc01a1ffa7a10c2d61dc (patch)
tree18c9ddf46afe329bf7de93263dafa671758b52e7 /src/log.c
parent8fe03b8bcef9f5a3a822055a3c4ab65cfe1baedb (diff)
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().
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c29
1 files changed, 15 insertions, 14 deletions
diff --git a/src/log.c b/src/log.c
index 9797abe7..fd0685ff 100644
--- a/src/log.c
+++ b/src/log.c
@@ -49,15 +49,15 @@ static GLogLevelFlags log_threshold = G_LOG_LEVEL_MESSAGE;
static const char *log_charset;
static bool stdout_mode = true;
-static int out_fd = -1;
+static int out_fd;
static const char *out_filename;
-static void redirect_logs(void)
+static void redirect_logs(int fd)
{
- assert(out_fd >= 0);
- if (dup2(out_fd, STDOUT_FILENO) < 0)
+ assert(fd >= 0);
+ if (dup2(fd, STDOUT_FILENO) < 0)
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));
}
@@ -247,8 +247,11 @@ void setup_log_output(bool use_stdout)
{
fflush(NULL);
if (!use_stdout) {
- if (out_filename != NULL)
- redirect_logs();
+ if (out_filename != NULL) {
+ redirect_logs(out_fd);
+ close(out_fd);
+ }
+
stdout_mode = false;
log_charset = NULL;
}
@@ -284,6 +287,8 @@ G_GNUC_PRINTF(1, 2) G_GNUC_NORETURN void FATAL(const char *fmt, ...)
int cycle_log_files(void)
{
+ int fd;
+
if (stdout_mode || out_filename == NULL)
return 0;
assert(out_filename);
@@ -291,13 +296,13 @@ int cycle_log_files(void)
DEBUG("Cycling log files...\n");
close_log_files();
- out_fd = open_log_file();
- if (out_fd < 0) {
+ fd = open_log_file();
+ if (fd < 0) {
ERROR("error re-opening log file: %s\n", out_filename);
return -1;
}
- redirect_logs();
+ redirect_logs(fd);
DEBUG("Done cycling log files\n");
return 0;
}
@@ -309,9 +314,5 @@ void close_log_files(void)
if (out_filename == NULL)
closelog();
- else {
- assert(out_fd >= 0);
- close(out_fd);
- }
}