Log requests only if setting is enabled

Log requests only if corresponding setting is turned on.

The Logging plug-in reads the D-Bus setting and caches the value. It
updates the value if the D-Bus property changes.

The D-Bus setting object is at
/xyz/openbmc_project/logging/rest_api_logs.

Change-Id: If4afcbfd3898d09c6ef31cc7c79a058cb5017769
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/module/obmc/wsgi/apps/rest_dbus.py b/module/obmc/wsgi/apps/rest_dbus.py
index 1ecacfc..2bfce21 100644
--- a/module/obmc/wsgi/apps/rest_dbus.py
+++ b/module/obmc/wsgi/apps/rest_dbus.py
@@ -1518,9 +1518,20 @@
             self.suppress_json_logging = suppress_json_logging
             self.callback = callback
             self.app = app
+            self.logging_enabled = None
+            self.bus = dbus.SystemBus()
+            self.dbus_path = '/xyz/openbmc_project/logging/rest_api_logs'
+            self.bus.add_signal_receiver(
+                self.properties_changed_handler,
+                dbus_interface=dbus.PROPERTIES_IFACE,
+                signal_name='PropertiesChanged',
+                path=self.dbus_path)
+            Greenlet.spawn(self.dbus_loop)
 
         def __call__(self, *a, **kw):
             resp = self.callback(*a, **kw)
+            if not self.enabled():
+                return resp;
             if request.method == 'GET':
                 return resp;
             json = request.json
@@ -1542,6 +1553,36 @@
                     status=response.status))
             return resp;
 
+        def enabled(self):
+            if self.logging_enabled is None:
+                try:
+                    obj = self.bus.get_object(
+                              'xyz.openbmc_project.Settings',
+                              self.dbus_path)
+                    iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
+                    logging_enabled = iface.Get(
+                                          'xyz.openbmc_project.Object.Enable',
+                                          'Enabled')
+                    self.logging_enabled = logging_enabled
+                except dbus.exceptions.DBusException:
+                    self.logging_enabled = False
+            return self.logging_enabled
+
+        def dbus_loop(self):
+            loop = gobject.MainLoop()
+            gcontext = loop.get_context()
+            while loop is not None:
+                try:
+                    if gcontext.pending():
+                        gcontext.iteration()
+                    else:
+                        gevent.sleep(5)
+                except Exception as e:
+                    break
+
+        def properties_changed_handler(self, interface, new, old, **kw):
+            self.logging_enabled = new.values()[0]
+
     def apply(self, callback, route):
         cb = route.get_undecorated_callback()
         skip = getattr(