diff options
| author | William Jon McCann <mccann@jhu.edu> | 2007-10-22 16:57:47 -0400 | 
|---|---|---|
| committer | William Jon McCann <mccann@jhu.edu> | 2007-10-22 16:57:47 -0400 | 
| commit | 252f542201036d36aa01ade9bd381624391cfb34 (patch) | |
| tree | 202a99a54102f511b34d7aeec59d38f2d19137f4 | |
| parent | 4c842f1e108a0e9a412ac599cc4cfc954d801e62 (diff) | |
try to handle log rotation
| -rw-r--r-- | src/ck-event-logger.c | 53 | 
1 files changed, 45 insertions, 8 deletions
diff --git a/src/ck-event-logger.c b/src/ck-event-logger.c index b14bec0..7cc3d69 100644 --- a/src/ck-event-logger.c +++ b/src/ck-event-logger.c @@ -146,15 +146,14 @@ retry:          fchown (fd, 0, 0); -        event_logger->priv->fd = fd;          event_logger->priv->file = fdopen (fd, "a"); -          if (event_logger->priv->file == NULL) {                  g_warning ("Error setting up log descriptor (%s)",                             g_strerror (errno));                  close (fd);                  return FALSE;          } +        event_logger->priv->fd = fd;          /* Set it to line buffering */          setlinebuf (event_logger->priv->file); @@ -162,6 +161,48 @@ retry:          return TRUE;  } +static void +reopen_file_stream (CkEventLogger *event_logger) +{ +        close (event_logger->priv->fd); +        fclose (event_logger->priv->file); + +        /* FIXME: retries */ +        open_log_file (event_logger); +} + +static void +check_file_stream (CkEventLogger *event_logger) +{ +        int         old_fd; +        struct stat old_stats; +        int         new_fd; +        struct stat new_stats; + +        old_fd = event_logger->priv->fd; +        if (fstat (old_fd, &old_stats) != 0) { +                g_warning ("Unable to stat file: %s", +                           g_strerror (errno)); +                reopen_file_stream (event_logger); +                return; +        } + +        new_fd = g_open (event_logger->priv->log_filename, O_RDONLY | O_NONBLOCK, 0600); +        if (new_fd == -1 || fstat (new_fd, &new_stats) < 0) { +                close (new_fd); +                g_debug ("Unable to open or stat %s - will try to reopen", event_logger->priv->log_filename); +                reopen_file_stream (event_logger); +                return; +        } +        close (new_fd); + +        if (old_stats.st_ino != new_stats.st_ino || old_stats.st_dev != new_stats.st_dev) { +                g_debug ("File %s has been replaced; writing to end of new file", event_logger->priv->log_filename); +                reopen_file_stream (event_logger); +                return; +        } +} +  static gboolean  write_log_for_event (CkEventLogger *event_logger,                       CkLogEvent    *event) @@ -173,14 +214,10 @@ write_log_for_event (CkEventLogger *event_logger,          ck_log_event_to_string (event, str);          g_debug ("Writing log for event: %s", str->str); +        check_file_stream (event_logger);          if (event_logger->priv->file != NULL) { -                int rc; - -                g_debug ("fd:%d err:%d tell:%ld", -                         fileno (event_logger->priv->file), -                         ferror (event_logger->priv->file), -                         ftell (event_logger->priv->file)); +                int         rc;                  rc = fprintf (event_logger->priv->file, "%s\n", str->str);                  if (rc <= 0) {  | 
