- fixed logging of calls that fail at or before auth.check
[plcapi.git] / PLC / Method.py
index 01ec187..7791c1a 100644 (file)
@@ -4,7 +4,7 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: Method.py,v 1.3 2006/10/13 15:11:31 tmack Exp $
+# $Id: Method.py,v 1.7 2006/10/18 19:42:46 tmack Exp $
 #
 
 import xmlrpclib
@@ -65,16 +65,16 @@ class Method:
 
         # API may set this to a (addr, port) tuple if known
         self.source = None
-       self.__call__ = self.log(self.__call__) 
 
        
-    def __call__(self, *args):
+    def __call__(self, *args, **kwds):
         """
         Main entry point for all PLCAPI functions. Type checks
         arguments, authenticates, and executes call().
         """
 
         try:
+           start = time.time()
             (min_args, max_args, defaults) = self.args()
                                
            # Check that the right number of arguments were passed in
@@ -101,70 +101,67 @@ class Method:
                             break
                 if isinstance(auth, Auth):
                     auth.check(self, *args)
-                       
-           return self.call(*args)
+          
+           result = self.call(*args, **kwds)
+           runtime = time.time() - start
+
+           if self.api.config.PLC_API_DEBUG:
+               self.log(0, runtime, *args)
+               
+           return result
 
         except PLCFault, fault:
             # Prepend method name to expected faults
             fault.faultString = self.name + ": " + fault.faultString
+           runtime = time.time() - start
+           self.log(fault.faultCode, runtime, *args)
             raise fault
 
 
-    def log(self, callable):
+    def log(self, fault_code, runtime, *args):
         """
         Log the transaction 
-        """
-       def __log__(vars):
-               """
-               Commit the transaction 
-               """
-
-               # only log api calls
-               if vars['call_name'] in ['listMethods', 'methodSignature']:
-                       return False
-        
-               sql = "INSERT INTO events " \
-                        " (person_id, event_type, object_type, fault_code, call, runtime)" \
-                        " VALUES (%d, '%s', '%s', %d, '%s', %f)" %  \
-                        (vars['person_id'], vars['event_type'], vars['object_type'], 
-                       vars['fault_code'], vars['call'], vars['runtime'])
-                self.api.db.do(sql)
-                self.api.db.commit()
-                       
-
-        def wrapper(*args, **kwds):
+        """    
+       # Gather necessary logging variables
+       event_type = 'Unknown'
+       object_type = 'Unknown'
+       person_id = 'Null'
+       object_ids = []
+       call_name = self.name
+       call_args = ", ".join([unicode(arg) for arg in list(args)[1:]])
+       call = "%s(%s)" % (call_name, call_args)
                
-               # Gather necessary logging vars 
-               fault_code = 0
-               person_id = 0
-               event_type = 'Unknown'
-               object_type = 'Unknown'
-               call_name = callable.im_class.__module__.split('.')[-1:][0]
-               call_args = ", ".join([str(arg) for arg in list(args)[1:]]).replace('\'', '\\\'')
-               call = "%s(%s)" % (call_name, call_args)
-               
-               if hasattr(self, 'event_type'):
-                       event_type = self.event_type
-                if hasattr(self, 'object_type'):
-                       object_type = self.object_type
-               if self.caller:
-                       person_id = self.caller['person_id']
-               
-               start = time.time()
-                       
-               try:
-                       result =  callable(*args, **kwds)
-                       runtime =  time.time() - start
-                       __log__(locals())                       
-                       return result
-                                                                      
-                except PLCFault, fault:
-                       fault_code = fault.faultCode
-                       runtime =  time.time() - start
-                       __log__(locals())
-                       raise fault
-                       
-       return wrapper
+       if hasattr(self, 'event_type'):
+               event_type = self.event_type
+        if hasattr(self, 'object_type'):
+               object_type = self.object_type
+       if self.caller:
+               person_id = self.caller['person_id']
+       if hasattr(self, 'object_ids'):
+               object_ids = self.object_ids 
+
+       # do not log system calls
+        if call_name.startswith('system'):
+               return False
+       # do not log get calls
+       if call_name.startswith('Get'):
+               return False
+       
+       sql_event = "INSERT INTO events " \
+              " (person_id, event_type, object_type, fault_code, call, runtime) VALUES" \
+              " (%(person_id)s, '%(event_type)s', '%(object_type)s'," \
+             "  %(fault_code)d, '%(call)s', %(runtime)f)" 
+       self.api.db.do(sql_event % locals())    
+
+       print self.api.db.last_insert_id('events', 'event_id')  
+       # log objects affected
+       for object_id in object_ids:
+               event_id =  self.api.db.last_insert_id('events', 'event_id')
+               sql_objects = "INSERT INTO event_object (event_id, object_id) VALUES" \
+                        " (%(event_id)d, %(object_id)d) "  % (locals()) 
+               self.api.db.do(sql_objects)
+                       
+        self.api.db.commit()           
        
 
     def help(self, indent = "  "):