diff mbox

[ovs-dev,v2,5/6] vlog: Add vlog/close command.

Message ID 1454536211-12354-6-git-send-email-blp@ovn.org
State Accepted
Headers show

Commit Message

Ben Pfaff Feb. 3, 2016, 9:50 p.m. UTC
Requested-by: P R Dinesh
Requested-at: https://github.com/openvswitch/ovs/pull/94
Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 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(-)

Comments

Russell Bryant Feb. 10, 2016, 9:27 p.m. UTC | #1
On 02/03/2016 04:50 PM, Ben Pfaff wrote:
> Requested-by: P R Dinesh
> Requested-at: https://github.com/openvswitch/ovs/pull/94
> Signed-off-by: Ben Pfaff <blp@ovn.org>

Acked-by: Russell Bryant <russell@ovn.org>
diff mbox

Patch

diff --git a/NEWS b/NEWS
index e683dae..a4b84e1 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,8 @@  Post-v2.5.0
      * OpenFlow 1.4+ OFPMP_QUEUE_DESC is now supported.
    - ovs-ofctl:
      * queue-get-config command now allows a queue ID to be specified.
+   - 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 7c47634..7372a7e 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 f514d65..49260d8 100644
--- a/lib/vlog.c
+++ b/lib/vlog.c
@@ -678,6 +678,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)
 {
     struct vlog_module *mp;
@@ -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 b41f2f0..4996387 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,
@@ -390,6 +392,13 @@  class Vlog(object):
             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:
             msg = Vlog.set_levels_from_string(arg)
diff --git a/tests/unixctl-py.at b/tests/unixctl-py.at
index ec029fc..4838d2d 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 4842c15..9cfbb1f 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 f477534..0eda7f2 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.