Always truncating a log can wipe out useful history. Add an
option to change this.
Signed-off-by: Eric Blake <eblake(a)redhat.com>
---
v2: tweaks to documentation, rely on nbdkit_parse_bool
filters/log/nbdkit-log-filter.pod | 5 +++--
filters/log/log.c | 12 ++++++++++--
tests/test-log.sh | 6 +++++-
3 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/filters/log/nbdkit-log-filter.pod b/filters/log/nbdkit-log-filter.pod
index 0903329..cff209e 100644
--- a/filters/log/nbdkit-log-filter.pod
+++ b/filters/log/nbdkit-log-filter.pod
@@ -4,7 +4,7 @@ nbdkit-log-filter - nbdkit log filter
=head1 SYNOPSIS
- nbdkit --filter=log plugin logfile=FILE [plugin-args...]
+ nbdkit --filter=log plugin logfile=FILE [logappend=BOOL] [plugin-args...]
=head1 DESCRIPTION
@@ -22,7 +22,8 @@ work even when nbdkit is run as a daemon.
The nbdkit-log-filter requires a single parameter C<logfile> which
specifies the path of the file to use for logging. If the file
-already exists, it will be truncated.
+already exists, it will be truncated unless the C<logappend> parameter
+was specified with a value that can be parsed as a boolean true.
=head1 EXAMPLES
diff --git a/filters/log/log.c b/filters/log/log.c
index 2079084..a497e9e 100644
--- a/filters/log/log.c
+++ b/filters/log/log.c
@@ -51,6 +51,7 @@
static uint64_t connections;
static char *logfilename;
static FILE *logfile;
+static int append;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static void
@@ -75,6 +76,12 @@ log_config (nbdkit_next_config *next, void *nxdata,
}
return 0;
}
+ if (strcmp (key, "logappend") == 0) {
+ append = nbdkit_parse_bool (value);
+ if (append < 0)
+ return -1;
+ return 0;
+ }
return next (nxdata, key, value);
}
@@ -86,7 +93,7 @@ log_config_complete (nbdkit_next_config_complete *next, void *nxdata)
nbdkit_error ("missing logfile= parameter for the log filter");
return -1;
}
- logfile = fopen (logfilename, "w");
+ logfile = fopen (logfilename, append ? "a" : "w");
if (!logfile) {
nbdkit_error ("fopen: %m");
return -1;
@@ -96,7 +103,8 @@ log_config_complete (nbdkit_next_config_complete *next, void *nxdata)
}
#define log_config_help \
- "logfile=<FILE> The file to place the log in."
+ "logfile=<FILE> (required) The file to place the log in.\n" \
+ "logappend=<BOOL> True to append to the log (default false).\n"
struct handle {
uint64_t connection;
diff --git a/tests/test-log.sh b/tests/test-log.sh
index f0eacb7..b30d20e 100755
--- a/tests/test-log.sh
+++ b/tests/test-log.sh
@@ -45,7 +45,9 @@ if ! qemu-io -f raw -c 'w 1M 2M' log.img; then
fi
# Run nbdkit with logging enabled to file.
-start_nbdkit -P log.pid -U log.sock --filter=log file log.img logfile=log.log
+echo '# My log' > log.log
+start_nbdkit -P log.pid -U log.sock --filter=log file log.img \
+ logfile=log.log logappend=1
# For easier debugging, dump the final log files before removing them
# on exit.
@@ -61,6 +63,8 @@ cleanup_fn cleanup
qemu-io -f raw -c 'w -P 11 1M 2M' 'nbd+unix://?socket=log.sock'
qemu-io -r -f raw -c 'r -P 11 2M 1M' 'nbd+unix://?socket=log.sock'
+# The log should have been appended, preserving our marker.
+grep '# My log' log.log
# The log should show a write on connection 1, and read on connection 2.
grep 'connection=1 Write id=1 offset=0x100000 count=0x200000 ' log.log
grep 'connection=2 Read id=1 offset=0x200000 count=0x100000 ' log.log
--
2.17.2