From 063801288e2b1c98026d7faa81de9959eee678a3 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 3 Feb 2016 13:41:34 -0800 Subject: [PATCH] vlog: Add vlog/close command. Requested-by: P R Dinesh Requested-at: https://github.com/openvswitch/ovs/pull/94 Signed-off-by: Ben Pfaff Acked-by: Russell Bryant --- NEWS | 2 ++ lib/vlog-unixctl.man | 9 +++-- lib/vlog.c | 24 +++++++++++++ python/ovs/vlog.py | 11 +++++- tests/unixctl-py.at | 1 + tests/vlog.at | 74 +++++++++++++++++++++++++++++++++++++++ utilities/ovs-appctl.8.in | 10 ++++-- 7 files changed, 125 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index 3e33871c1..8c9f31082 100644 --- a/NEWS +++ b/NEWS @@ -12,6 +12,8 @@ Post-v2.5.0 Old 'other_config:n-dpdk-rxqs' is no longer supported. - ovs-benchmark: This utility has been removed due to lack of use and bitrot. + - ovs-appctl: + * New "vlog/close" command. v2.5.0 - xx xxx xxxx diff --git a/lib/vlog-unixctl.man b/lib/vlog-unixctl.man index 7c47634fa..7372a7ef4 100644 --- a/lib/vlog-unixctl.man +++ b/lib/vlog-unixctl.man @@ -54,9 +54,14 @@ Lists the supported logging modules and their current levels. .IP "\fBvlog/list-pattern\fR" Lists logging patterns used for each destination. . +.IP "\fBvlog/close\fR" +Causes \fB\*(PN\fR to close its log file, if it is open. (Use +\fBvlog/reopen\fR to reopen it later.) +. .IP "\fBvlog/reopen\fR" -Causes \fB\*(PN\fR to close and reopen its log file. (This is useful -after rotating log files, to cause a new log file to be used.) +Causes \fB\*(PN\fR to close its log file, if it is open, and then +reopen it. (This is useful after rotating log files, to cause a new +log file to be used.) .IP This has no effect unless \fB\*(PN\fR was invoked with the \fB\-\-log\-file\fR option. diff --git a/lib/vlog.c b/lib/vlog.c index f514d65b5..49260d803 100644 --- a/lib/vlog.c +++ b/lib/vlog.c @@ -677,6 +677,28 @@ vlog_unixctl_reopen(struct unixctl_conn *conn, int argc OVS_UNUSED, } } +static void +vlog_unixctl_close(struct unixctl_conn *conn, int argc OVS_UNUSED, + const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) +{ + ovs_mutex_lock(&log_file_mutex); + if (log_fd >= 0) { + close(log_fd); + log_fd = -1; + + async_append_destroy(log_writer); + log_writer = NULL; + + struct vlog_module *mp; + LIST_FOR_EACH (mp, list, &vlog_modules) { + update_min_level(mp); + } + } + ovs_mutex_unlock(&log_file_mutex); + + unixctl_command_reply(conn, NULL); +} + static void set_all_rate_limits(bool enable) { @@ -771,6 +793,8 @@ vlog_init(void) 0, INT_MAX, vlog_disable_rate_limit, NULL); unixctl_command_register("vlog/reopen", "", 0, 0, vlog_unixctl_reopen, NULL); + unixctl_command_register("vlog/close", "", 0, 0, + vlog_unixctl_close, NULL); ovs_rwlock_rdlock(&pattern_rwlock); print_syslog_target_deprecation = syslog_fd >= 0; diff --git a/python/ovs/vlog.py b/python/ovs/vlog.py index b41f2f078..499638703 100644 --- a/python/ovs/vlog.py +++ b/python/ovs/vlog.py @@ -1,5 +1,5 @@ -# Copyright (c) 2011, 2012, 2013 Nicira, Inc. +# Copyright (c) 2011, 2012, 2013, 2015, 2016 Nicira, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -241,6 +241,8 @@ class Vlog(object): ovs.unixctl.command_register("vlog/reopen", "", 0, 0, Vlog._unixctl_vlog_reopen, None) + ovs.unixctl.command_register("vlog/close", "", 0, 0, + Vlog._unixctl_vlog_close, None) ovs.unixctl.command_register("vlog/set", "spec", 1, sys.maxsize, Vlog._unixctl_vlog_set, None) ovs.unixctl.command_register("vlog/list", "", 0, 0, @@ -389,6 +391,13 @@ class Vlog(object): else: conn.reply("Logging to file not configured") + @staticmethod + def _unixctl_vlog_close(conn, unused_argv, unused_aux): + if Vlog.__log_file: + logger = logging.getLogger("file") + logger.removeHandler(Vlog.__file_handler) + conn.reply(None) + @staticmethod def _unixctl_vlog_set(conn, argv, unused_aux): for arg in argv: diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at index ec029fcde..4838d2db5 100644 --- a/tests/unixctl-py.at +++ b/tests/unixctl-py.at @@ -102,6 +102,7 @@ The available commands are: help log [[arg ...]] version + vlog/close vlog/list vlog/reopen vlog/set spec diff --git a/tests/vlog.at b/tests/vlog.at index 4842c155e..9cfbb1f1d 100644 --- a/tests/vlog.at +++ b/tests/vlog.at @@ -255,6 +255,80 @@ AT_CHECK([sed 's/.*|//' log], [0], [dnl ]) AT_CLEANUP +AT_SETUP([vlog - vlog/close - C]) +on_exit 'kill `cat test-unixctl.pid`' + +AT_CAPTURE_FILE([log]) +AT_CAPTURE_FILE([log.old]) +AT_CHECK([ovstest test-unixctl --log-file=`pwd`/log --pidfile --detach], + [0], [], [stderr]) +AT_CHECK([vlog_filt stderr], [0], [opened log file +]) + +AT_CHECK([APPCTL -t test-unixctl log message]) +AT_CHECK([APPCTL -t test-unixctl log message2]) + +# After closing the log file, message3 won't appear anywhere. +AT_CHECK([APPCTL -t test-unixctl vlog/close]) +mv log log.old +AT_CHECK([APPCTL -t test-unixctl log message3]) + +# Closing the log file again is harmless. +AT_CHECK([APPCTL -t test-unixctl vlog/close]) +AT_CHECK([APPCTL -t test-unixctl log message4]) + +# After reopening the log file, further messages start appearing again. +AT_CHECK([APPCTL -t test-unixctl vlog/reopen]) +AT_CHECK([APPCTL -t test-unixctl log message5]) +AT_CHECK([APPCTL -t test-unixctl exit]) + +AT_CHECK([vlog_filt log.old], [0], [dnl +opened log file +Entering run loop. +message +message2 +]) +AT_CHECK([vlog_filt log], [0], [dnl +opened log file +message5 +]) +AT_CLEANUP + +AT_SETUP([vlog - vlog/close - Python]) +AT_SKIP_IF([test $HAVE_PYTHON = no]) +on_exit 'kill `cat test-unixctl.py.pid`' + +AT_CAPTURE_FILE([log]) +AT_CAPTURE_FILE([log.old]) +AT_CHECK([$PYTHON $srcdir/test-unixctl.py --log-file=`pwd`/log --pidfile --detach]) + +AT_CHECK([APPCTL -t test-unixctl.py log message]) +AT_CHECK([APPCTL -t test-unixctl.py log message2]) + +# After closing the log file, message3 won't appear anywhere. +AT_CHECK([APPCTL -t test-unixctl.py vlog/close]) +mv log log.old +AT_CHECK([APPCTL -t test-unixctl.py log message3]) + +# Closing the log file again is harmless. +AT_CHECK([APPCTL -t test-unixctl.py vlog/close]) +AT_CHECK([APPCTL -t test-unixctl.py log message4]) + +# After reopening the log file, further messages start appearing again. +AT_CHECK([APPCTL -t test-unixctl.py vlog/reopen]) +AT_CHECK([APPCTL -t test-unixctl.py log message5]) +AT_CHECK([APPCTL -t test-unixctl.py exit]) + +AT_CHECK([sed 's/.*|//' log.old], [0], [dnl + Entering run loop. + message + message2 +]) +AT_CHECK([sed 's/.*|//' log], [0], [dnl + message5 +]) +AT_CLEANUP + AT_SETUP([vlog - vlog/set and vlog/list - C]) on_exit 'kill `cat test-unixctl.pid`' diff --git a/utilities/ovs-appctl.8.in b/utilities/ovs-appctl.8.in index f477534eb..0eda7f222 100644 --- a/utilities/ovs-appctl.8.in +++ b/utilities/ovs-appctl.8.in @@ -265,10 +265,14 @@ Sets the RFC5424 facility of the log message. \fIfacility\fR can be one of \fBlocal2\fR, \fBlocal3\fR, \fBlocal4\fR, \fBlocal5\fR, \fBlocal6\fR or \fBlocal7\fR. . +.IP "\fBvlog/close\fR" +Causes the daemon to close its log file, if it is open. (Use +\fBvlog/reopen\fR to reopen it later.) +. .IP "\fBvlog/reopen\fR" -Causes the daemon to close and reopen its log file. (This -is useful after rotating log files, to cause a new log file to be -used.) +Causes the daemon to close its log file, if it is open, and then +reopen it. (This is useful after rotating log files, to cause a new +log file to be used.) .IP This has no effect if the target application was not invoked with the \fB\-\-log\-file\fR option. -- 2.20.1