diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2013-12-13 10:15:52 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2013-12-13 10:15:52 +0100 |
commit | 312de68e259fe03d6df09f7c7f0f7271c2350532 (patch) | |
tree | bff5e0329fb3b0ad4d3825fbf2d9687ffb3a0c86 /hugin.c | |
parent | 8ab91e5c823cfe7205330cf730ebf38b7676f685 (diff) |
Add new log-rotate feature and error handling on file log opening.20131213
Diffstat (limited to 'hugin.c')
-rw-r--r-- | hugin.c | 31 |
1 files changed, 28 insertions, 3 deletions
@@ -34,6 +34,7 @@ #include <time.h> #include <string.h> #include <unistd.h> +#include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> @@ -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() { |