From 312de68e259fe03d6df09f7c7f0f7271c2350532 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 13 Dec 2013 10:15:52 +0100 Subject: Add new log-rotate feature and error handling on file log opening. --- hugin.c | 31 ++++++++++++++++++++++++++++--- hugin.h | 12 +++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/hugin.c b/hugin.c index 9a7e7a1..cf72e5e 100644 --- a/hugin.c +++ b/hugin.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -119,7 +120,8 @@ struct hug_config_t { int fd; int file_fd; int stdout_no_date; - mutex_t mutex; + mutex_t mutex; + char *filename; #ifdef WITH_HUG_SYSLOG const char* syslog_host; int syslog_port; @@ -132,6 +134,7 @@ struct hug_config_t { //#ifdef WITH_HUG_MUTEX MUTEX_INIT_VALUE, // mutex; //#endif + NULL, // filename #ifdef WITH_HUG_SYSLOG NULL, // syslog_host; 541, // syslog_port; @@ -226,8 +229,13 @@ hug_status_t hug_init(unsigned int flags, ...) break; case HUG_OPTION_FILENAME: if(hug_config.flags & HUG_FLAG_OUTPUT_TO_FILE) { - const char *filename = (const char*)va_arg(vl, char*); - hug_config.file_fd = open(filename, O_CREAT | O_RDWR, 0777); + hug_config.filename = strdup((const char*)va_arg(vl, char*)); + hug_config.file_fd = open(hug_config.filename, O_CREAT | O_RDWR, 0777); + if(hug_config.file_fd == -1) { + fprintf(stderr, "Could not open logfile for writing: %s\n", + hug_config.filename); + return HUG_STATUS_ERROR; + } } break; #ifdef WITH_HUG_SYSLOG @@ -268,6 +276,7 @@ void hug_close() { if(hug_config.flags & HUG_FLAG_OUTPUT_TO_FILE) { if(hug_config.file_fd != -1) close(hug_config.file_fd); + if(hug_config.filename) free(hug_config.filename); } #ifdef WITH_HUG_SYSLOG @@ -277,6 +286,22 @@ void hug_close() hug_mutex_close(); } +hug_status_t hugin_reopen_log() +{ + if((hug_config.flags & HUG_FLAG_OUTPUT_TO_FILE) == 0) return HUG_STATUS_OK; + + close(hug_config.file_fd); + hug_config.file_fd = open(hug_config.filename, O_CREAT | O_RDWR, 0777); + + if(hug_config.file_fd == -1) { + fprintf(stderr, "Could not re-open logfile for writing: %s\n", + hug_config.filename); + return HUG_STATUS_ERROR; + } + + return HUG_STATUS_OK; +} + /* static unsigned int gettid() { diff --git a/hugin.h b/hugin.h index 8aabcf1..393ce1c 100644 --- a/hugin.h +++ b/hugin.h @@ -131,11 +131,21 @@ enum HUG_OPTION { * @param flags combination of HUG_FLAG values * @param ... list of options (type-value pairs, * terminated with HUG_OPTION_END). - * @return 0 on success, 1 on error. + * @return HUG_STATUS_OK on success, 1 on error. */ hug_status_t hug_init(unsigned int flags, ...); void hug_close(); +/** + * Use this function if hugin is logging to a file and this file has just been + * rotated. Hugin will then close the filedescriptor to the file and open + * the a new file with the same name and location. + * Be aware that logrotate might need to create this new file first with + * the correct permissions in order for hugin to write to it. + * @return 0 on success. HUG_STATUS_ERROR on error (cannot open file). + */ +hug_status_t hugin_reopen_log(); + /** * Example of usage (use mutex protected calls, send output to file): * -- cgit v1.2.3