From 3e6fe0dde74a63ac3509acd9ed25cc36fba0fa2a Mon Sep 17 00:00:00 2001 From: kanoi Date: Mon, 4 Aug 2014 17:54:31 +1000 Subject: [PATCH] libckpool - make the rotating filename a separate function --- src/libckpool.c | 31 +++++++++++++++++++++---------- src/libckpool.h | 1 + 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/libckpool.c b/src/libckpool.c index c20d6ce2..fbc8e91c 100644 --- a/src/libckpool.c +++ b/src/libckpool.c @@ -831,40 +831,51 @@ json_t *json_object_dup(json_t *val, const char *entry) return json_copy(json_object_get(val, entry)); } +char *rotating_filename(const char *path, time_t when) +{ + char *filename; + struct tm *tm; + + tm = localtime(&when); + ASPRINTF(&filename, "%s%04d%02d%02d%02d.log", path, tm->tm_year + 1900, tm->tm_mon + 1, + tm->tm_mday, tm->tm_hour); + return filename; +} + /* Creates a logfile entry which changes filename hourly with exclusive access */ bool rotating_log(const char *path, const char *msg) { mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; char *filename; - struct tm *tm; - time_t now; FILE *fp; int fd; + bool ok = false; - now = time(NULL); - tm = localtime(&now); - ASPRINTF(&filename, "%s%04d%02d%02d%02d.log", path, tm->tm_year + 1900, tm->tm_mon + 1, - tm->tm_mday, tm->tm_hour); + filename = rotating_filename(path, time(NULL)); fd = open(filename, O_CREAT|O_RDWR, mode); if (unlikely(fd == -1)) { LOGERR("Failed to open %s in rotating_log!", filename); - return false; + goto stageleft; } fp = fdopen(fd, "a"); if (unlikely(!fp)) { close(fd); LOGERR("Failed to fopen %s in rotating_log!", filename); - return false; + goto stageleft; } if (unlikely(flock(fd, LOCK_EX))) { fclose(fp); LOGERR("Failed to flock %s in rotating_log!", filename); - return false; + goto stageleft; } fprintf(fp, "%s\n", msg); fclose(fp); + ok = true; - return true; +stageleft: + free(filename); + + return ok; } /* Align a size_t to 4 byte boundaries for fussy arches */ diff --git a/src/libckpool.h b/src/libckpool.h index 1433bdcc..62407f4f 100644 --- a/src/libckpool.h +++ b/src/libckpool.h @@ -396,6 +396,7 @@ const char *__json_array_string(json_t *val, unsigned int entry); char *json_array_string(json_t *val, unsigned int entry); json_t *json_object_dup(json_t *val, const char *entry); +char *rotating_filename(const char *path, time_t when); bool rotating_log(const char *path, const char *msg); void align_len(size_t *len);