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 | |
parent | 8ab91e5c823cfe7205330cf730ebf38b7676f685 (diff) |
Add new log-rotate feature and error handling on file log opening.20131213
-rw-r--r-- | hugin.c | 31 | ||||
-rw-r--r-- | hugin.h | 12 |
2 files changed, 39 insertions, 4 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() { @@ -131,12 +131,22 @@ 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): * * hug_status_t status; |