server: Server outputs using python logging
authorAdriana Draghici <adriana008@gmail.com>
Thu, 29 Apr 2010 17:36:20 +0000 (17:36 +0000)
committerAdriana Draghici <adriana008@gmail.com>
Thu, 29 Apr 2010 17:36:20 +0000 (17:36 +0000)
autorun/server/Server.py

index 1c58a4e..8defe1b 100644 (file)
@@ -6,6 +6,7 @@ import time
 import pickle
 import signal
 import struct
+import logging
 
 from daemon import Daemon
 from Util import *
@@ -23,12 +24,15 @@ class MyDaemon(Daemon):
 
     processes_fd = {}   # keeps lists of file descriptors for each process
     processes_info = {} # keeps lists of BT clients name, torrent name and status log name for each process
-   
+    logger = None 
+
     def __init__(self, pidfile, ip='', stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
         Daemon.__init__(self, pidfile, stdin, stdout, stderr)
         self.ip = ip
         self.port = SERVER_PORT
-
+        logging.basicConfig(level = logging.DEBUG)
+        self.logger = logging.getLogger('autorun.Server')
+        self.logger.setLevel(logging.DEBUG) 
     def set_address(self, ip):
         self.ip = ip
     
@@ -50,8 +54,7 @@ class MyDaemon(Daemon):
                 break
         
         dd = pickle.loads(msg) 
-        if self.DEBUG:
-            print "Server: received message: ", dd
+        self.logger.debug("  received message: ", dd)
         return dd
 
     def send_pickled_data (self, clientsock, data):
@@ -62,8 +65,7 @@ class MyDaemon(Daemon):
             if sent == 0:
                 raise RuntimeError,    "socket connection broken"
             totalsent = totalsent + sent
-        if self.DEBUG:
-            print "Server: sent message: ", data
+        self.logger.debug("  sent message: ", data)
 
 
     def save_download_info(self, data):
@@ -87,8 +89,8 @@ class MyDaemon(Daemon):
         f.close()
 
     def add_to_output_msg(self, file_list, info_dict):
-        """ Constructs a list with logging file info - paths, client, metafile
-        @param file_list list to which the created logging info list in appended. """
+        """ Constructs a list with self.logger file info - paths, client, metafile
+        @param file_list list to which the created self.logger info list in appended. """
         
         if CLIENT not in info_dict and TORRENT not in info_dict:
             return -1
@@ -102,8 +104,7 @@ class MyDaemon(Daemon):
         if LOG_FILE in info_dict:
             info_list.append((info_dict[LOG_FILE], LOG_FILE))
         
-        if self.DEBUG:
-            print "Server: read transfer log info: ", info_list
+        self.logger.debug("  read transfer log info: ", info_list)
          
         file_list.append (info_list)
         return 0 # success
@@ -132,14 +133,14 @@ class MyDaemon(Daemon):
                 err_msg = "File does not exist: ", file_path
             else: archives_list.append(file_path)
         
-        print "Server Error: ", err_msg
+        self.logger.error(" ", err_msg)
 
     def read_download_info(self, file_list, create_archive = False, archives_list = []):
         """ Reads all the contents of the file that stores info about 
-        logging files and folders.
+        self.logger files and folders.
         """
         if not os.path.exists(SESSIONS_FILE):
-            print "Server Error: No sessions_file"
+            self.logger.error("  No sessions_file")
             return -1
         else:    
             f = open(SESSIONS_FILE,"r")
@@ -176,8 +177,7 @@ class MyDaemon(Daemon):
                         self.archive_files(info_dict, archives_list)
                 
             except Exception, e:
-                print "Server Error: wrong file type for sessions file"
-                print e
+                self.logger.error("  wrong file type for sessions file\n" + e)
                 f.close()
                 return -1
 
@@ -201,8 +201,7 @@ class MyDaemon(Daemon):
         line_parts.append(parser.parse_last_status_line(line))
         if line_parts == "":
             return "Error occured while reading status file " + status_file_path
-        print line_parts
-        print line
+        self.logger.debug("  status_line = " + line)
         return ""
 
     def start_bt_client(self, bt_client_data):
@@ -233,8 +232,7 @@ class MyDaemon(Daemon):
                                     bt_client_data[TORRENT], 
                                     bt_client_data[OUT_DIR] + "/" + bt_client_data[OUT_FILE]]
         
-        if(self.DEBUG):
-            print "Server: started client with pid = ", pid
+        self.logger.debug("  started client with pid = ", pid)
         return pid
 
     def stop_bt_client(self, pid):
@@ -251,8 +249,7 @@ class MyDaemon(Daemon):
         del self.processes_fd[int_pid]
         del self.processes_info[int_pid]
 
-        if(self.DEBUG):
-            print "Server: killed process with pid = ", pid
+        self.logger.debug("  killed process with pid = ", pid)
         return ""
 
     def set_linger(self,sock, l_onoff, l_linger):
@@ -265,18 +262,15 @@ class MyDaemon(Daemon):
         """ Accepts socket connections and receives messages from commander."""
 
         self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-        if(self.DEBUG):
-            print "Server: host ip = %s, port = %s"%(ip,SERVER_PORT)
+        self.logger.debug( "  host ip = %s, port = %s"%(ip,SERVER_PORT))
         
         self.serversocket.bind((ip,SERVER_PORT));
         self.serversocket.listen(10) #max 10 requests
         self.set_linger(self.serversocket,1, 0)
         while(1):
-            if(self.DEBUG):
-                print "Server: accepting connections"
+            self.logger.debug("  accepting connections")
             (clientsock, address) = self.serversocket.accept();
-            if(self.DEBUG):
-                print "Server: accepted connection from ", address
+            self.logger.debug("  accepted connection from ", address)
             
             msg = self.recv_pickled_data(clientsock)