Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions debian/daliserver.logrotate
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/var/log/daliserver.log
{
rotate 7
daily
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
/usr/bin/killall -HUP daliserver
endscript
}
2 changes: 1 addition & 1 deletion lib/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ DispatchStatus dispatch_run(DispatchPtr table, int timeout) {
if (errno != EINTR) {
log_error("Error waiting for I/O events");
}
return 0;
return 1;
} else if (ready == 0) {
// timeout
log_debug("poll() timeout");
Expand Down
34 changes: 21 additions & 13 deletions lib/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
static unsigned int loglevel = LOG_LEVEL_DEFAULT;
static unsigned int loglevel_file = LOG_LEVEL_DEFAULT;
static unsigned int loglevel_syslog = LOG_LEVEL_ERROR;
static char *logfile = NULL;
static FILE *fp_logfile = NULL;
static int enabled_syslog = 0;

Expand Down Expand Up @@ -155,27 +156,34 @@ unsigned int log_get_level() {
return loglevel;
}

int log_set_logfile(const char *logfile) {
int log_reopen_file() {
if (fp_logfile) {
fclose(fp_logfile);
fp_logfile = NULL;
}
if (logfile) {
FILE *fp = fopen(logfile, "a");
if (!fp) {
fp_logfile = fopen(logfile, "a");
if (!fp_logfile) {
log_error("Error opening log file %s: %s", logfile, strerror(errno));
return -1;
} else {
if (fp_logfile) {
fclose(fp_logfile);
}
fp_logfile = fp;
}
} else {
if (fp_logfile) {
fclose(fp_logfile);
fp_logfile = NULL;
}
}
return 0;
}

int log_set_logfile(const char *logfile_path) {
free(logfile);
logfile = NULL;
if (logfile_path != NULL) {
logfile = strdup(logfile_path);
if (!logfile) {
log_error("Error setting log file %s: %s", logfile_path, strerror(errno));
return -1;
}
}
return log_reopen_file();
}

void log_set_logfile_level(unsigned int level) {
if (level > LOG_LEVEL_MAX) {
loglevel_file = LOG_LEVEL_MAX;
Expand Down
6 changes: 5 additions & 1 deletion lib/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ void log_printf(unsigned int level, const char *format, ...);
void log_set_level(unsigned int level);
// Gets the current log level
unsigned int log_get_level();
// Reopen the configured log file (if any), e.g. after logrotate
// Returns 0 upon success, -1 if the file could not be opened
// (also generates a warning to the other logging channels in this case)
int log_reopen_file();
// Enables/disables logging to a log file
// Pass NULL for logfile to disable
// Returns 0 upon success, -1 if the file could not be opened
// (also generates a warning to the other logging channels in this case)
int log_set_logfile(const char *logfile);
int log_set_logfile(const char *logfile_path);
// Set the log level for the logfile
// The default is the same as for console logging
void log_set_logfile_level(unsigned int level);
Expand Down
6 changes: 6 additions & 0 deletions src/daliserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ int main(int argc, char *const argv[]) {
running = 1;
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGHUP, signal_handler);
while (running && dispatch_run(dispatch, usbdali_get_timeout(usb)));

log_info("Shutting daliserver down");
Expand All @@ -201,6 +202,11 @@ int main(int argc, char *const argv[]) {
}

static void signal_handler(int sig) {
if (sig == SIGHUP) {
log_info("Signal received, reopening log file");
log_reopen_file();
return;
}
if (running) {
log_info("Signal received, shutting down");
running = 0;
Expand Down