From 2e496b82c5dcb8070d689fdaa55a7873f7f4fc03 Mon Sep 17 00:00:00 2001
From: Mandeep Dhami <mandeep.dhami@bigswitch.com>
Date: Mon, 23 Jan 2012 14:10:41 -0800
Subject: [PATCH] Updated jython server to use java logging (and made log
 object available to python code)

---
 .../floodlightcontroller/jython/Server.java   | 10 +++++++-
 src/main/python/debugserver.py                | 24 ++++++++++++++++---
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/jython/Server.java b/src/main/java/net/floodlightcontroller/jython/Server.java
index cd330fa27..2f909ab84 100644
--- a/src/main/java/net/floodlightcontroller/jython/Server.java
+++ b/src/main/java/net/floodlightcontroller/jython/Server.java
@@ -1,9 +1,12 @@
 package net.floodlightcontroller.jython;
 
 import java.net.URL;
+import java.util.HashMap;
 import java.util.Map;
 
 import org.python.util.PythonInterpreter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This class starts a thread that runs a jython interpreter that
@@ -13,6 +16,8 @@ import org.python.util.PythonInterpreter;
  *
  */
 public class Server extends Thread {
+    protected static Logger log = LoggerFactory.getLogger(Server.class);
+
 	int port;
 	Map<String, Object> locals;
 	
@@ -23,7 +28,10 @@ public class Server extends Thread {
 	public Server(int port_, Map<String, Object> locals_) {
 		this.port = port_ ;
 		this.locals = locals_;
-		
+		if (this.locals == null) {
+			this.locals = new HashMap<String, Object>();
+		}
+		this.locals.put("log", Server.log);
 	}
 
     /**
diff --git a/src/main/python/debugserver.py b/src/main/python/debugserver.py
index 2f6346f02..5eb3f60a4 100644
--- a/src/main/python/debugserver.py
+++ b/src/main/python/debugserver.py
@@ -6,6 +6,17 @@ from code import InteractiveConsole
 
 _locals = None
 
+class DebugLogger(object):
+    def do_print(self, *args):
+        for i in args:
+            print i,
+        print
+    info = do_print
+    warn = do_print
+    debug = do_print
+_log = DebugLogger()
+
+
 class DebugConsole(InteractiveConsole):
     def __init__(self, request):
         self.request = request
@@ -26,7 +37,7 @@ class DebugConsole(InteractiveConsole):
 
 class DebugServerHandler(BaseRequestHandler):
     def __init__(self, request, client_address, server):
-        print 'Open connection to DebugServer from: %s' % str(client_address)
+        _log.debug('Open connection to DebugServer from: %s' % str(client_address))
         BaseRequestHandler.__init__(self, request, client_address, server)
 
     def handle(self):
@@ -36,15 +47,22 @@ class DebugServerHandler(BaseRequestHandler):
         self.request.close()
 
 class DebugServer(TCPServer):
+    daemon_threads = True
+    allow_reuse_address = True
+
     def handle_error(self, request, client_address):
-        print 'Closing connection to DebugServer from: %s' % str(client_address)
+        _log.debug('Closing connection to DebugServer from: %s' % str(client_address))
         request.close()
 
 def run_server(port=6655, host='0.0.0.0', locals=locals()):
     global _locals
     _locals = locals
 
-    print "Starting DebugServer on port %d" % port
+    if "log" in locals.keys():
+        global _log
+        _log = locals["log"]
+
+    _log.info("Starting DebugServer on port %d" % port)
     server = DebugServer(('', port), DebugServerHandler)
     try:
         server.serve_forever()
-- 
GitLab