summaryrefslogtreecommitdiff
path: root/hugin.c
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2013-12-13 10:15:52 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2013-12-13 10:15:52 +0100
commit312de68e259fe03d6df09f7c7f0f7271c2350532 (patch)
treebff5e0329fb3b0ad4d3825fbf2d9687ffb3a0c86 /hugin.c
parent8ab91e5c823cfe7205330cf730ebf38b7676f685 (diff)
Add new log-rotate feature and error handling on file log opening.20131213
Diffstat (limited to 'hugin.c')
-rw-r--r--hugin.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/hugin.c b/hugin.c
index 9a7e7a1..cf72e5e 100644
--- a/hugin.c
+++ b/hugin.c
@@ -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()
{