diff --git a/ChangeLog b/ChangeLog
index e7da020..4285d53 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2020-01-25 Mikey Austin
+
+ * Improved file logging module
+ * Updated logger to always write to stderr
+
2020-01-24 Mikey Austin
* Added new log_to_file configuration
diff --git a/configure.ac b/configure.ac
index 4252cc1..fa6f41d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,5 +1,5 @@
AC_PREREQ([2.68])
-AC_INIT([greyd], [0.11.5], [mikey@greyd.org])
+AC_INIT([greyd], [0.11.6], [mikey@greyd.org])
AC_USE_SYSTEM_EXTENSIONS
AM_INIT_AUTOMAKE
LT_INIT([dlopen])
diff --git a/packages/docker/Dockerfile.greyd.centos7 b/packages/docker/Dockerfile.greyd.centos7
index 55bef75..83d9ad4 100644
--- a/packages/docker/Dockerfile.greyd.centos7
+++ b/packages/docker/Dockerfile.greyd.centos7
@@ -8,5 +8,7 @@ RUN rpm --import https://greyd.org/repo/greyd_pkg_sign_pub.asc && \
EXPOSE 8025/tcp
+RUN ln -sf /dev/stdout /tmp/greyd.log
+
# Override config at /etc/greyd/greyd.conf
ENTRYPOINT [ "/usr/sbin/greyd", "-F" ]
diff --git a/src/log.c b/src/log.c
index e8783fd..8e51f66 100644
--- a/src/log.c
+++ b/src/log.c
@@ -18,7 +18,7 @@
* @file log.c
* @brief Logging interface implementation.
* @author Mikey Austin
- * @date 2014
+ * @date 2014-2020
*/
#include "constants.h"
@@ -35,15 +35,21 @@
static short Log_debug = 0;
static short Log_syslog = 0;
static const char* Log_ident = NULL;
+static FILE* log_file = NULL;
+static struct flock lock;
extern void Log_reinit(Config_T config)
{
- char *log_file = NULL;
- if ((log_file = Config_get_str(config, "log_to_file", NULL, NULL)) != NULL) {
- int fd = open(log_file, O_WRONLY);
- if (fd >= 0) {
- dup2(fd, STDOUT_FILENO);
- }
+ // Lock the entire file.
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_len = 0;
+ lock.l_start = 0;
+ lock.l_pid = 0;
+
+ char* path = NULL;
+ if ((path = Config_get_str(config, "log_to_file", NULL, NULL)) != NULL) {
+ log_file = fopen(path, "a");
}
}
@@ -61,10 +67,31 @@ Log_setup(Config_T config, const char* prog_name)
Log_reinit(config);
}
+static void
+write_to(FILE* f, const char* msg, va_list args)
+{
+ // Lock the file for writing.
+ lock.l_type = F_WRLCK;
+ if (fcntl(fileno(f), F_SETLK, &lock) == -1) {
+ return;
+ }
+
+ fprintf(f, "%s[%d]: ", Log_ident, getpid());
+ vfprintf(f, msg, args);
+ fprintf(f, "\n");
+ fflush(f);
+
+ // Release the lock.
+ lock.l_type = F_UNLCK;
+ if (fcntl(fileno(f), F_SETLK, &lock) == -1) {
+ return;
+ }
+}
+
extern void
Log_write(int severity, const char* msg, va_list args)
{
- va_list syslog_args;
+ va_list syslog_args, file_args;
if (Log_debug || (!Log_debug && severity < LOG_DEBUG)) {
if (Log_syslog) {
@@ -73,9 +100,11 @@ Log_write(int severity, const char* msg, va_list args)
va_end(syslog_args);
}
- /* Always write to console. */
- printf("%s[%d]: ", Log_ident, getpid());
- vprintf(msg, args);
- printf("\n");
+ if (log_file != NULL) {
+ va_copy(file_args, args);
+ write_to(log_file, msg, file_args);
+ }
+
+ write_to(stderr, msg, args);
}
}
diff --git a/website/downloads.php b/website/downloads.php
index c8d2ca2..8bb431f 100644
--- a/website/downloads.php
+++ b/website/downloads.php
@@ -15,6 +15,9 @@
Grab the latest distribution:
-$ tar xzf greyd-0.11.5.tar.gz && cd greyd-0.11.5
+$ tar xzf greyd-0.11.6.tar.gz && cd greyd-0.11.6
$ ./configure --with-sqlite --with-netfilter
$ make
$ sudo make install