improve server-side logging of exceptions
[sfa.git] / sfa / util / method.py
index e7a12d8..484b77b 100644 (file)
@@ -1,29 +1,29 @@
 #
-# Base class for all GeniAPI functions
+# Base class for all SfaAPI functions
 #
 #
 
 ### $Id$
 ### $URL$
 
-import xmlrpclib
+import os, time
 from types import *
+from types import StringTypes
+import traceback
 import textwrap
-import os
-import time
-import pprint
+import xmlrpclib
 
-from types import StringTypes
 
+import sfa.util.sfalogging
 from sfa.util.faults import * 
 from sfa.util.parameter import Parameter, Mixed, python_type, xmlrpc_type
-from sfa.util.auth import Auth
-from sfa.util.debug import profile, log
+from sfa.trust.auth import Auth
+#from sfa.util.debug import profile, log
 
 # we inherit object because we use new-style classes for legacy methods
 class Method (object):
     """
-    Base class for all GeniAPI functions. At a minimum, all GeniAPI
+    Base class for all SfaAPI functions. At a minimum, all SfaAPI
     functions must define:
 
     interfaces = [allowed interfaces]
@@ -48,7 +48,7 @@ class Method (object):
 
     def call(self, *args):
         """
-        Method body for all GeniAPI functions. Must override.
+        Method body for all SfaAPI functions. Must override.
 
         """
 
@@ -67,7 +67,7 @@ class Method (object):
        
     def __call__(self, *args, **kwds):
         """
-        Main entry point for all GeniAPI functions. Type checks
+        Main entry point for all SfaAPI functions. Type checks
         arguments, authenticates, and executes call().
         """
 
@@ -75,7 +75,7 @@ class Method (object):
             start = time.time()
             methodname = self.name
             if not self.api.interface or self.api.interface not in self.interfaces:
-                raise GeniInvalidAPIMethod, methodname, self.api.interface 
+                raise SfaInvalidAPIMethod(methodname, self.api.interface) 
 
             # legacy code cannot be type-checked, due to the way Method.args() works
             if not hasattr(self,"skip_typecheck"):
@@ -83,7 +83,7 @@ class Method (object):
                                
                 # Check that the right number of arguments were passed in
                 if len(args) < len(min_args) or len(args) > len(max_args):
-                    raise GeniInvalidArgumentCount(len(args), len(min_args), len(max_args))
+                    raise SfaInvalidArgumentCount(len(args), len(min_args), len(max_args))
 
                 for name, value, expected in zip(max_args, args, self.accepts):
                     self.type_check(name, value, expected, args)
@@ -91,25 +91,24 @@ class Method (object):
             result = self.call(*args, **kwds)
             runtime = time.time() - start
 
-            if self.api.config.GENI_API_DEBUG or hasattr(self, 'message'):
+            if self.api.config.SFA_API_DEBUG or hasattr(self, 'message'):
+                msg=getattr(self,'message',"method %s completed"%methodname)
+                sfa.util.sfalogging.logger.info(msg)
                 # XX print to some log file
                 # print >> log, "some output"
-                   pass
 
             return result
 
-        except GeniFault, fault:
+        except SfaFault, fault:
 
             caller = ""
 
             # Prepend caller and method name to expected faults
             fault.faultString = caller + ": " +  self.name + ": " + fault.faultString
             runtime = time.time() - start
-           
-            if self.api.config.GENI_API_DEBUG:
-                # XX print to some log file
-                #print >> log, "Some debugging output"              
-                pass 
+#            if self.api.config.SFA_API_DEBUG:
+#                traceback.print_exc()
+            sfa.util.sfalogging.log_exc("Method %s raised an exception"%self.name) 
             raise fault
 
 
@@ -221,7 +220,7 @@ class Method (object):
                 try:
                     self.type_check(name, value, item, args)
                     return
-                except GeniInvalidArgument, fault:
+                except SfaInvalidArgument, fault:
                     pass
             raise fault
 
@@ -260,7 +259,7 @@ class Method (object):
             pass
 
         elif not isinstance(value, expected_type):
-            raise GeniInvalidArgument("expected %s, got %s" % \
+            raise SfaInvalidArgument("expected %s, got %s" % \
                                      (xmlrpc_type(expected_type),
                                       xmlrpc_type(type(value))),
                                      name)
@@ -269,20 +268,20 @@ class Method (object):
         if expected_type in StringTypes:
             if min is not None and \
                len(value.encode(self.api.encoding)) < min:
-                raise GeniInvalidArgument, "%s must be at least %d bytes long" % (name, min)
+                raise SfaInvalidArgument, "%s must be at least %d bytes long" % (name, min)
             if max is not None and \
                len(value.encode(self.api.encoding)) > max:
-                raise GeniInvalidArgument, "%s must be at most %d bytes long" % (name, max)
+                raise SfaInvalidArgument, "%s must be at most %d bytes long" % (name, max)
         elif expected_type in (list, tuple, set):
             if min is not None and len(value) < min:
-                raise GeniInvalidArgument, "%s must contain at least %d items" % (name, min)
+                raise SfaInvalidArgument, "%s must contain at least %d items" % (name, min)
             if max is not None and len(value) > max:
-                raise GeniInvalidArgument, "%s must contain at most %d items" % (name, max)
+                raise SfaInvalidArgument, "%s must contain at most %d items" % (name, max)
         else:
             if min is not None and value < min:
-                raise GeniInvalidArgument, "%s must be > %s" % (name, str(min))
+                raise SfaInvalidArgument, "%s must be > %s" % (name, str(min))
             if max is not None and value > max:
-                raise GeniInvalidArgument, "%s must be < %s" % (name, str(max))
+                raise SfaInvalidArgument, "%s must be < %s" % (name, str(max))
 
         # If a list with particular types of items is expected
         if isinstance(expected, (list, tuple, set)):
@@ -303,7 +302,7 @@ class Method (object):
                 if isinstance(subparam, Parameter) and \
                    subparam.optional is not None and \
                    not subparam.optional and key not in value.keys():
-                    raise GeniInvalidArgument("'%s' not specified" % key, name)
+                    raise SfaInvalidArgument("'%s' not specified" % key, name)
 
         #if auth is not None:
         #    auth.check(self, *args)