From 0cf0d31c313a366e3f272f830bdb4f2a7308e11f Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Thu, 30 Sep 2010 14:36:19 +0200 Subject: [PATCH] * tried to put some sense in the way things get logged, at least on server-side for now /var/log/sfa.log /var/log/sfa_import.log /var/log/htppd/sfa_access_log * please use sfalogging objects as far as possible, deprecated sfa.util.debug and sfa.util.report --- Makefile | 2 + sfa/client/sfiAddAttribute.py | 3 - sfa/client/sfiDeleteAttribute.py | 3 - sfa/managers/aggregate_manager_max.py | 1 - sfa/managers/aggregate_manager_pl.py | 1 - sfa/managers/aggregate_manager_vini.py | 1 - sfa/managers/slice_manager_pl.py | 1 - sfa/methods/CreateSliver.py | 2 - sfa/methods/GetCredential.py | 1 - sfa/methods/GetSelfCredential.py | 1 - sfa/methods/Register.py | 1 - sfa/methods/RegisterPeerObject.py | 1 - sfa/methods/Remove.py | 1 - sfa/methods/RemovePeerObject.py | 1 - sfa/methods/Resolve.py | 1 - sfa/methods/Update.py | 1 - sfa/methods/get_self_credential.py | 1 - sfa/methods/get_trusted_certs.py | 5 +- sfa/methods/register_peer_object.py | 1 - sfa/methods/remove_peer_object.py | 1 - sfa/plc/api-dev.py | 5 +- sfa/plc/api.py | 7 +- sfa/plc/network.py | 6 +- sfa/plc/sfa-import-plc.py | 22 +-- sfa/plc/sfa-nuke-plc.py | 3 +- sfa/plc/sfaImport.py | 46 +++--- sfa/plc/slices.py | 1 - sfa/rspecs/aggregates/rspec_manager_max.py | 1 - sfa/server/interface.py | 6 +- sfa/server/modpython/SfaAggregateModPython.py | 17 +-- sfa/server/modpython/SfaRegistryModPython.py | 18 +-- sfa/server/modpython/SfaSliceMgrModPython.py | 17 +-- sfa/server/modpythonapi/BaseClient.py | 46 +++--- sfa/server/modpythonapi/ModPython.py | 16 +- sfa/server/sfa-server.py | 8 +- sfa/trust/auth.py | 1 - sfa/trust/certificate.py | 12 +- sfa/trust/credential.py | 6 +- sfa/trust/gid.py | 7 +- sfa/trust/hierarchy.py | 3 +- sfa/util/PostgreSQL.py | 21 ++- sfa/util/api.py | 7 +- sfa/util/componentserver.py | 5 +- sfa/util/config.py | 2 - sfa/util/debug.py | 57 -------- sfa/util/filter.py | 1 - sfa/util/method.py | 11 +- sfa/util/policy.py | 1 - sfa/util/record.py | 1 - sfa/util/report.py | 11 -- sfa/util/rspec.py | 8 +- sfa/util/server.py | 4 +- sfa/util/sfalogging.py | 137 ++++++++++++++---- sfa/util/table.py | 1 - 54 files changed, 226 insertions(+), 319 deletions(-) delete mode 100644 sfa/util/debug.py delete mode 100644 sfa/util/report.py mode change 100644 => 100755 sfa/util/sfalogging.py diff --git a/Makefile b/Makefile index fbd2a0e9..06f184c3 100644 --- a/Makefile +++ b/Makefile @@ -57,6 +57,8 @@ tags: # (*) otherwise, entering through the root context # make sync PLCHOST=testbox1.inria.fr GUEST=vplc03.inria.fr +PLCHOST ?= testplc.onelab.eu + ifdef GUEST ifdef PLCHOST SSHURL:=root@$(PLCHOST):/vservers/$(GUEST) diff --git a/sfa/client/sfiAddAttribute.py b/sfa/client/sfiAddAttribute.py index 33b94a3a..f4e613c7 100755 --- a/sfa/client/sfiAddAttribute.py +++ b/sfa/client/sfiAddAttribute.py @@ -31,6 +31,3 @@ for name in attrs: print >> sys.stderr, "FAILED: on node %s: %s=%s" % (node, name, value) print command.rspec - - - diff --git a/sfa/client/sfiDeleteAttribute.py b/sfa/client/sfiDeleteAttribute.py index 8e8f0a28..f248e495 100755 --- a/sfa/client/sfiDeleteAttribute.py +++ b/sfa/client/sfiDeleteAttribute.py @@ -32,6 +32,3 @@ for name in attrs: print >> sys.stderr, "FAILED: on node %s: %s=%s" % (node, name, value) print command.rspec - - - diff --git a/sfa/managers/aggregate_manager_max.py b/sfa/managers/aggregate_manager_max.py index d7aed8e3..14168a0d 100644 --- a/sfa/managers/aggregate_manager_max.py +++ b/sfa/managers/aggregate_manager_max.py @@ -9,7 +9,6 @@ from sfa.util.specdict import * from sfa.util.faults import * from sfa.util.storage import * from sfa.util.policy import Policy -from sfa.util.debug import log from sfa.server.aggregate import Aggregates from sfa.server.registry import Registries from sfa.util.faults import * diff --git a/sfa/managers/aggregate_manager_pl.py b/sfa/managers/aggregate_manager_pl.py index fe4e53d1..c0e9d9eb 100644 --- a/sfa/managers/aggregate_manager_pl.py +++ b/sfa/managers/aggregate_manager_pl.py @@ -15,7 +15,6 @@ from sfa.util.record import SfaRecord from sfa.util.policy import Policy from sfa.util.record import * from sfa.util.sfaticket import SfaTicket -from sfa.util.debug import log from sfa.plc.slices import Slices from sfa.trust.credential import Credential import sfa.plc.peers as peers diff --git a/sfa/managers/aggregate_manager_vini.py b/sfa/managers/aggregate_manager_vini.py index ee693786..2ec4999a 100644 --- a/sfa/managers/aggregate_manager_vini.py +++ b/sfa/managers/aggregate_manager_vini.py @@ -16,7 +16,6 @@ from sfa.util.policy import Policy from sfa.util.record import * from sfa.util.sfaticket import SfaTicket from sfa.server.registry import Registries -from sfa.util.debug import log from sfa.plc.slices import Slices import sfa.plc.peers as peers from sfa.managers.vini.vini_network import * diff --git a/sfa/managers/slice_manager_pl.py b/sfa/managers/slice_manager_pl.py index 05cd043a..43d3392f 100644 --- a/sfa/managers/slice_manager_pl.py +++ b/sfa/managers/slice_manager_pl.py @@ -21,7 +21,6 @@ from sfa.util.sfaticket import * from sfa.trust.credential import Credential from sfa.util.threadmanager import ThreadManager import sfa.util.xmlrpcprotocol as xmlrpcprotocol -from sfa.util.debug import log import sfa.plc.peers as peers from copy import copy diff --git a/sfa/methods/CreateSliver.py b/sfa/methods/CreateSliver.py index 7c7a00ec..8b45917e 100644 --- a/sfa/methods/CreateSliver.py +++ b/sfa/methods/CreateSliver.py @@ -3,7 +3,6 @@ from sfa.util.namespace import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed from sfa.util.sfatablesRuntime import run_sfatables -import sys from sfa.trust.credential import Credential class CreateSliver(Method): @@ -48,4 +47,3 @@ class CreateSliver(Method): allocated = manager.create_slice(self.api, slice_xrn, creds, rspec, users) return rspec - diff --git a/sfa/methods/GetCredential.py b/sfa/methods/GetCredential.py index 1f8c71a0..3acc3b12 100644 --- a/sfa/methods/GetCredential.py +++ b/sfa/methods/GetCredential.py @@ -7,7 +7,6 @@ from sfa.util.faults import * from sfa.util.namespace import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed -from sfa.util.debug import log from sfa.trust.credential import Credential class GetCredential(Method): diff --git a/sfa/methods/GetSelfCredential.py b/sfa/methods/GetSelfCredential.py index a5c7d400..6bdc63d1 100644 --- a/sfa/methods/GetSelfCredential.py +++ b/sfa/methods/GetSelfCredential.py @@ -8,7 +8,6 @@ from sfa.util.namespace import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed from sfa.util.record import SfaRecord -from sfa.util.debug import log from sfa.trust.certificate import Certificate class GetSelfCredential(Method): diff --git a/sfa/methods/Register.py b/sfa/methods/Register.py index 4f5a452c..1233fa82 100644 --- a/sfa/methods/Register.py +++ b/sfa/methods/Register.py @@ -7,7 +7,6 @@ from sfa.util.faults import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed from sfa.util.record import SfaRecord -from sfa.util.debug import log from sfa.trust.auth import Auth from sfa.trust.gid import create_uuid from sfa.trust.credential import Credential diff --git a/sfa/methods/RegisterPeerObject.py b/sfa/methods/RegisterPeerObject.py index 68b3105c..dae8f6e1 100644 --- a/sfa/methods/RegisterPeerObject.py +++ b/sfa/methods/RegisterPeerObject.py @@ -10,7 +10,6 @@ from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed from sfa.util.record import SfaRecord from sfa.util.table import SfaTable -from sfa.util.debug import log from sfa.trust.auth import Auth from sfa.trust.gid import create_uuid from sfa.trust.credential import Credential diff --git a/sfa/methods/Remove.py b/sfa/methods/Remove.py index 82050b40..d9911592 100644 --- a/sfa/methods/Remove.py +++ b/sfa/methods/Remove.py @@ -5,7 +5,6 @@ from sfa.util.faults import * from sfa.util.namespace import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed -from sfa.util.debug import log from sfa.trust.credential import Credential class Remove(Method): diff --git a/sfa/methods/RemovePeerObject.py b/sfa/methods/RemovePeerObject.py index ed46ea71..866122d4 100644 --- a/sfa/methods/RemovePeerObject.py +++ b/sfa/methods/RemovePeerObject.py @@ -4,7 +4,6 @@ from sfa.util.parameter import Parameter, Mixed from sfa.trust.auth import Auth from sfa.util.record import SfaRecord from sfa.util.table import SfaTable -from sfa.util.debug import log from sfa.trust.credential import Credential from types import StringTypes diff --git a/sfa/methods/Resolve.py b/sfa/methods/Resolve.py index ec47e41b..3f7a61ab 100644 --- a/sfa/methods/Resolve.py +++ b/sfa/methods/Resolve.py @@ -6,7 +6,6 @@ from sfa.util.faults import * from sfa.util.namespace import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed -from sfa.util.debug import log from sfa.trust.credential import Credential from sfa.util.record import SfaRecord diff --git a/sfa/methods/Update.py b/sfa/methods/Update.py index 3b8b55a2..d36ea367 100644 --- a/sfa/methods/Update.py +++ b/sfa/methods/Update.py @@ -5,7 +5,6 @@ import time from sfa.util.faults import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed -from sfa.util.debug import log from sfa.trust.credential import Credential class Update(Method): diff --git a/sfa/methods/get_self_credential.py b/sfa/methods/get_self_credential.py index 3bdaecc7..97e70520 100644 --- a/sfa/methods/get_self_credential.py +++ b/sfa/methods/get_self_credential.py @@ -8,7 +8,6 @@ from sfa.util.namespace import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed from sfa.util.record import SfaRecord -from sfa.util.debug import log from sfa.methods.GetSelfCredential import GetSelfCredential class get_self_credential(GetSelfCredential): diff --git a/sfa/methods/get_trusted_certs.py b/sfa/methods/get_trusted_certs.py index 6ae4d50e..c0871a11 100644 --- a/sfa/methods/get_trusted_certs.py +++ b/sfa/methods/get_trusted_certs.py @@ -1,6 +1,3 @@ - -### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/methods/reset_slices.py $ - from sfa.util.faults import * from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed @@ -26,7 +23,7 @@ class get_trusted_certs(Method): # If cred is not specified just return the gid for this interface. # This is true when when a peer is attempting to initiate federation # with this interface - print cred + sfaloging.logger.debug("get_trusted_certs: %r"%cred) if not cred: gid_strings = [] for gid in self.api.auth.trusted_cert_list: diff --git a/sfa/methods/register_peer_object.py b/sfa/methods/register_peer_object.py index 68b3105c..dae8f6e1 100644 --- a/sfa/methods/register_peer_object.py +++ b/sfa/methods/register_peer_object.py @@ -10,7 +10,6 @@ from sfa.util.method import Method from sfa.util.parameter import Parameter, Mixed from sfa.util.record import SfaRecord from sfa.util.table import SfaTable -from sfa.util.debug import log from sfa.trust.auth import Auth from sfa.trust.gid import create_uuid from sfa.trust.credential import Credential diff --git a/sfa/methods/remove_peer_object.py b/sfa/methods/remove_peer_object.py index ed46ea71..866122d4 100644 --- a/sfa/methods/remove_peer_object.py +++ b/sfa/methods/remove_peer_object.py @@ -4,7 +4,6 @@ from sfa.util.parameter import Parameter, Mixed from sfa.trust.auth import Auth from sfa.util.record import SfaRecord from sfa.util.table import SfaTable -from sfa.util.debug import log from sfa.trust.credential import Credential from types import StringTypes diff --git a/sfa/plc/api-dev.py b/sfa/plc/api-dev.py index 724d8076..41ae35c7 100644 --- a/sfa/plc/api-dev.py +++ b/sfa/plc/api-dev.py @@ -11,11 +11,10 @@ import traceback import string import xmlrpclib -import sfa.util.sfalogging +from sfa.util.sfalogging import sfa_logger from sfa.trust.auth import Auth from sfa.util.config import * from sfa.util.faults import * -from sfa.util.debug import * from sfa.trust.rights import * from sfa.trust.credential import * from sfa.trust.certificate import * @@ -325,7 +324,7 @@ class SfaAPI(BaseAPI): self.hrn = self.config.SFA_INTERFACE_HRN self.time_format = "%Y-%m-%d %H:%M:%S" - self.logger=sfa.util.sfalogging.logger + self.logger=sfa_logger def getPLCShell(self): self.plauth = {'Username': self.config.SFA_PLC_USER, diff --git a/sfa/plc/api.py b/sfa/plc/api.py index 79555c45..6da11882 100644 --- a/sfa/plc/api.py +++ b/sfa/plc/api.py @@ -11,12 +11,11 @@ import traceback import string import xmlrpclib -import sfa.util.sfalogging +from sfa.util.sfalogging import sfa_logger import sfa.util.xmlrpcprotocol as xmlrpcprotocol from sfa.trust.auth import Auth from sfa.util.config import * from sfa.util.faults import * -from sfa.util.debug import * from sfa.trust.rights import * from sfa.trust.credential import * from sfa.trust.certificate import * @@ -107,7 +106,7 @@ class SfaAPI(BaseAPI): self.hrn = self.config.SFA_INTERFACE_HRN self.time_format = "%Y-%m-%d %H:%M:%S" - self.logger=sfa.util.sfalogging.logger + self.logger=sfa_logger def getPLCShell(self): self.plauth = {'Username': self.config.SFA_PLC_USER, @@ -127,7 +126,7 @@ class SfaAPI(BaseAPI): def getCredential(self): """ - Retrun a valid credential for this interface. + Return a valid credential for this interface. """ if self.interface in ['registry']: return self.getCredentialFromLocalRegistry() diff --git a/sfa/plc/network.py b/sfa/plc/network.py index 1553e3ba..3e9603b2 100644 --- a/sfa/plc/network.py +++ b/sfa/plc/network.py @@ -30,10 +30,10 @@ class Iface: self.hostname = iface['hostname'] self.primary = iface['is_primary'] - """ - Just print out bwlimit right now - """ def toxml(self, xml): + """ + Just print out bwlimit right now + """ if self.bwlimit: with xml.bw_limit(units="kbps"): xml << str(self.bwlimit / 1000) diff --git a/sfa/plc/sfa-import-plc.py b/sfa/plc/sfa-import-plc.py index 786d57ac..084b90cd 100755 --- a/sfa/plc/sfa-import-plc.py +++ b/sfa/plc/sfa-import-plc.py @@ -20,8 +20,7 @@ import getopt import sys import tempfile -import logging.handlers -import logging + from sfa.util.record import * from sfa.util.table import SfaTable from sfa.util.namespace import * @@ -31,8 +30,7 @@ from sfa.trust.trustedroot import * from sfa.trust.hierarchy import * from sfa.plc.api import * from sfa.trust.gid import create_uuid -from sfa.plc.sfaImport import * -from sfa.util.report import trace, error +from sfa.plc.sfaImport import sfaImport def process_options(): global hrn @@ -60,15 +58,7 @@ def save_keys(filename, keys): f.close() def main(): - # setup the logger - LOGFILE='/var/log/sfa_import_plc.log' - logging.basicConfig(level=logging.INFO, - format='%(asctime)s - %(message)s', - filename=LOGFILE) - rotate_handler = logging.handlers.RotatingFileHandler(LOGFILE, maxBytes=1000000, backupCount=5) - logger = logging.getLogger() - logger.addHandler(rotate_handler) - + process_options() config = Config() if not config.SFA_REGISTRY_ENABLED: @@ -76,7 +66,7 @@ def main(): root_auth = config.SFA_REGISTRY_ROOT_AUTH interface_hrn = config.SFA_INTERFACE_HRN keys_filename = config.config_path + os.sep + 'person_keys.py' - sfaImporter = sfaImport(logger) + sfaImporter = sfaImport() shell = sfaImporter.shell plc_auth = sfaImporter.plc_auth AuthHierarchy = sfaImporter.AuthHierarchy @@ -91,7 +81,7 @@ def main(): if not root_auth == interface_hrn: sfaImporter.create_top_level_auth_records(interface_hrn) - trace("Import: adding " + interface_hrn + " to trusted list", logger) + sfaImporter.logger.info("Import: adding " + interface_hrn + " to trusted list") authority = AuthHierarchy.get_auth_info(interface_hrn) TrustedRoots.add_gid(authority.get_gid_object()) @@ -266,7 +256,7 @@ def main(): sfaImporter.delete_record(record_hrn, type) # save pub keys - trace('Import: saving current pub keys', logger) + sfaImporter.logger.info('Import: saving current pub keys') save_keys(keys_filename, person_keys) if __name__ == "__main__": diff --git a/sfa/plc/sfa-nuke-plc.py b/sfa/plc/sfa-nuke-plc.py index 2b1b41f5..4bbf003b 100755 --- a/sfa/plc/sfa-nuke-plc.py +++ b/sfa/plc/sfa-nuke-plc.py @@ -13,6 +13,7 @@ import sys from sfa.trust.hierarchy import * from sfa.util.record import * from sfa.util.table import SfaTable +from sfa.util.sfalogging import sfa_import_logger def process_options(): @@ -24,7 +25,7 @@ def process_options(): def main(): process_options() - print "Purging SFA records from database" + sfa_import_logger.info("Purging SFA records from database") table = SfaTable() table.sfa_records_purge() diff --git a/sfa/plc/sfaImport.py b/sfa/plc/sfaImport.py index 98f6b799..320730d4 100644 --- a/sfa/plc/sfaImport.py +++ b/sfa/plc/sfaImport.py @@ -12,24 +12,24 @@ import getopt import sys import tempfile +from sfa.util.sfalogging import sfa_import_logger from sfa.util.record import * from sfa.util.table import SfaTable from sfa.util.namespace import * from sfa.util.config import Config -from sfa.util.report import trace, error from sfa.trust.certificate import convert_public_key, Keypair from sfa.trust.trustedroot import * from sfa.trust.hierarchy import * from sfa.trust.gid import create_uuid -def un_unicode(str): +def _un_unicode(str): if isinstance(str, unicode): return str.encode("ascii", "ignore") else: return str -def cleanup_string(str): +def _cleanup_string(str): # pgsql has a fit with strings that have high ascii in them, so filter it # out when generating the hrns. tmp = "" @@ -38,7 +38,7 @@ def cleanup_string(str): tmp = tmp + c str = tmp - str = un_unicode(str) + str = _un_unicode(str) str = str.replace(" ", "_") str = str.replace(".", "_") str = str.replace("(", "_") @@ -49,8 +49,8 @@ def cleanup_string(str): class sfaImport: - def __init__(self, logger=None): - self.logger = logger + def __init__(self): + self.logger = sfa_import_logger self.AuthHierarchy = Hierarchy() self.config = Config() self.TrustedRoots = TrustedRootList(Config.get_trustedroots_dir(self.config)) @@ -79,7 +79,7 @@ class sfaImport: # create the authority if it doesnt already exist if not AuthHierarchy.auth_exists(urn): - trace("Import: creating top level authorites", self.logger) + self.logger.info("Import: creating top level authorites") AuthHierarchy.create_auth(urn) # create the db record if it doesnt already exist @@ -90,7 +90,7 @@ class sfaImport: if not auth_record: auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1) auth_record['authority'] = get_authority(auth_record['hrn']) - trace("Import: inserting authority record for " + hrn, self.logger) + self.logger.info("Import: inserting authority record for " + hrn) table.insert(auth_record) @@ -102,7 +102,7 @@ class sfaImport: if len(hrn) > 64: hrn = hrn[:64] - trace("Import: importing person " + hrn, self.logger) + self.logger.info("Import: importing person " + hrn) key_ids = [] if 'key_ids' in person and person['key_ids']: key_ids = person["key_ids"] @@ -115,7 +115,7 @@ class sfaImport: pkey = Keypair(create=True) else: # the user has no keys - trace(" person " + hrn + " does not have a PL public key", self.logger) + self.logger.info(" person " + hrn + " does not have a PL public key") # if a key is unavailable, then we still need to put something in the # user's GID. So make one up. pkey = Keypair(create=True) @@ -130,7 +130,7 @@ class sfaImport: if not existing_records: table.insert(person_record) else: - trace("Import: %s exists, updating " % hrn, self.logger) + self.logger.info("Import: %s exists, updating " % hrn) existing_record = existing_records[0] person_record['record_id'] = existing_record['record_id'] table.update(person_record) @@ -138,14 +138,14 @@ class sfaImport: def import_slice(self, parent_hrn, slice): AuthHierarchy = self.AuthHierarchy slicename = slice['name'].split("_",1)[-1] - slicename = cleanup_string(slicename) + slicename = _cleanup_string(slicename) if not slicename: - error("Import_Slice: failed to parse slice name " + slice['name'], self.logger) + self.logger.error("Import_Slice: failed to parse slice name " + slice['name']) return hrn = parent_hrn + "." + slicename - trace("Import: importing slice " + hrn, self.logger) + self.logger.info("Import: importing slice " + hrn) pkey = Keypair(create=True) urn = hrn_to_urn(hrn, 'slice') @@ -157,7 +157,7 @@ class sfaImport: if not existing_records: table.insert(slice_record) else: - trace("Import: %s exists, updating " % hrn, self.logger) + self.logger.info("Import: %s exists, updating " % hrn) existing_record = existing_records[0] slice_record['record_id'] = existing_record['record_id'] table.update(slice_record) @@ -165,14 +165,14 @@ class sfaImport: def import_node(self, parent_hrn, node): AuthHierarchy = self.AuthHierarchy nodename = node['hostname'].split(".")[0] - nodename = cleanup_string(nodename) + nodename = _cleanup_string(nodename) if not nodename: - error("Import_node: failed to parse node name " + node['hostname'], self.logger) + self.logger.error("Import_node: failed to parse node name " + node['hostname']) return hrn = parent_hrn + "." + nodename - trace("Import: importing node " + hrn, self.logger) + self.logger.info("Import: importing node " + hrn) # ASN.1 will have problems with hrn's longer than 64 characters if len(hrn) > 64: hrn = hrn[:64] @@ -188,7 +188,7 @@ class sfaImport: if not existing_records: table.insert(node_record) else: - trace("Import: %s exists, updating " % hrn, self.logger) + self.logger.info("Import: %s exists, updating " % hrn) existing_record = existing_records[0] node_record['record_id'] = existing_record['record_id'] table.update(node_record) @@ -199,7 +199,7 @@ class sfaImport: shell = self.shell plc_auth = self.plc_auth sitename = site['login_base'] - sitename = cleanup_string(sitename) + sitename = _cleanup_string(sitename) print 'importing site %s' % sitename hrn = parent_hrn + "." + sitename # Hardcode 'internet2' into the hrn for sites hosting @@ -214,7 +214,7 @@ class sfaImport: hrn = ".".join([parent_hrn, "internet2", sitename]) urn = hrn_to_urn(hrn, 'authority') - trace("Import: importing site " + hrn, self.logger) + self.logger.info("Import: importing site " + hrn) # create the authority if not AuthHierarchy.auth_exists(urn): @@ -229,7 +229,7 @@ class sfaImport: if not existing_records: table.insert(auth_record) else: - trace("Import: %s exists, updating " % hrn, self.logger) + self.logger.info("Import: %s exists, updating " % hrn) existing_record = existing_records[0] auth_record['record_id'] = existing_record['record_id'] table.update(auth_record) @@ -242,5 +242,5 @@ class sfaImport: table = SfaTable() record_list = table.find({'type': type, 'hrn': hrn}) for record in record_list: - trace("Import: Removing record %s %s" % (type, hrn), self.logger) + self.logger.info("Import: Removing record %s %s" % (type, hrn)) table.remove(record) diff --git a/sfa/plc/slices.py b/sfa/plc/slices.py index 9b3ff5df..173de194 100644 --- a/sfa/plc/slices.py +++ b/sfa/plc/slices.py @@ -14,7 +14,6 @@ from sfa.util.faults import * from sfa.util.record import SfaRecord from sfa.util.policy import Policy from sfa.util.prefixTree import prefixTree -from sfa.util.debug import log MAXINT = 2L**31-1 diff --git a/sfa/rspecs/aggregates/rspec_manager_max.py b/sfa/rspecs/aggregates/rspec_manager_max.py index c6504b99..51d7d14c 100644 --- a/sfa/rspecs/aggregates/rspec_manager_max.py +++ b/sfa/rspecs/aggregates/rspec_manager_max.py @@ -9,7 +9,6 @@ from sfa.util.specdict import * from sfa.util.faults import * from sfa.util.storage import * from sfa.util.policy import Policy -from sfa.util.debug import log from sfa.server.aggregate import Aggregates from sfa.server.registry import Registries from sfa.util.faults import * diff --git a/sfa/server/interface.py b/sfa/server/interface.py index dbc54891..9a1ceeaa 100644 --- a/sfa/server/interface.py +++ b/sfa/server/interface.py @@ -12,7 +12,6 @@ import traceback import sfa.util.xmlrpcprotocol as xmlrpcprotocol import sfa.util.soapprotocol as soapprotocol - # GeniLight client support is optional try: from egeni.geniLight_client import * @@ -136,8 +135,7 @@ class Interfaces(dict): except: message = "interface: %s\tunable to install trusted gid for %s" % \ (self.api.interface, new_hrn) - self.api.logger.info(message) - traceback.print_exc() + self.api.logger.log_exc(message) # reload the trusted certs list self.api.auth.load_trusted_certs() @@ -149,7 +147,7 @@ class Interfaces(dict): defined in the config file (registries.xml). Removes old records from the db. """ - # import SfaTable here so this module can be loaded by CompoenetAPI + # import SfaTable here so this module can be loaded by ComponentAPI from sfa.util.table import SfaTable if not gids: return diff --git a/sfa/server/modpython/SfaAggregateModPython.py b/sfa/server/modpython/SfaAggregateModPython.py index e1ab86dc..502fea63 100755 --- a/sfa/server/modpython/SfaAggregateModPython.py +++ b/sfa/server/modpython/SfaAggregateModPython.py @@ -13,23 +13,10 @@ import xmlrpclib from mod_python import apache from sfa.plc.api import SfaAPI -from sfa.util.debug import log +from sfa.util.sfalogging import sfa_logger api = SfaAPI(interface='aggregate') -class unbuffered: - """ - Write to /var/log/httpd/error_log. See - - http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp - """ - - def write(self, data): - sys.stderr.write(data) - sys.stderr.flush() - -#log = unbuffered() - def handler(req): try: if req.method != "POST": @@ -66,5 +53,5 @@ def handler(req): except Exception, err: # Log error in /var/log/httpd/(ssl_)?error_log - print >> log, err, traceback.format_exc() + sfa_logger.log_exc('%r'%err) return apache.HTTP_INTERNAL_SERVER_ERROR diff --git a/sfa/server/modpython/SfaRegistryModPython.py b/sfa/server/modpython/SfaRegistryModPython.py index a9044ebb..276005e8 100755 --- a/sfa/server/modpython/SfaRegistryModPython.py +++ b/sfa/server/modpython/SfaRegistryModPython.py @@ -11,24 +11,12 @@ import sys import traceback import xmlrpclib from mod_python import apache -from sfa.util.debug import log + from sfa.plc.api import SfaAPI +from sfa.util.sfalogging import sfa_logger api = SfaAPI(interface='registry') -class unbuffered: - """ - Write to /var/log/httpd/error_log. See - - http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp - """ - - def write(self, data): - sys.stderr.write(data) - sys.stderr.flush() - -#log = unbuffered() - def handler(req): try: if req.method != "POST": @@ -65,5 +53,5 @@ def handler(req): except Exception, err: # Log error in /var/log/httpd/(ssl_)?error_log - print >> log, err, traceback.format_exc() + sfa_logger.log_exc('%r'%err) return apache.HTTP_INTERNAL_SERVER_ERROR diff --git a/sfa/server/modpython/SfaSliceMgrModPython.py b/sfa/server/modpython/SfaSliceMgrModPython.py index 7ad8e0d4..1560c801 100755 --- a/sfa/server/modpython/SfaSliceMgrModPython.py +++ b/sfa/server/modpython/SfaSliceMgrModPython.py @@ -13,23 +13,10 @@ import xmlrpclib from mod_python import apache from sfa.plc.api import SfaAPI -from sfa.util.debug import log +from sfa.util.sfalogging import sfa_logger api = SfaAPI(interface='slicemgr') -class unbuffered: - """ - Write to /var/log/httpd/error_log. See - - http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp - """ - - def write(self, data): - sys.stderr.write(data) - sys.stderr.flush() - -#log = unbuffered() - def handler(req): try: if req.method != "POST": @@ -66,5 +53,5 @@ def handler(req): except Exception, err: # Log error in /var/log/httpd/(ssl_)?error_log - print >> log, err, traceback.format_exc() + sfa_logger.log_exc('%r'%err) return apache.HTTP_INTERNAL_SERVER_ERROR diff --git a/sfa/server/modpythonapi/BaseClient.py b/sfa/server/modpythonapi/BaseClient.py index 448f9346..5dd72a12 100755 --- a/sfa/server/modpythonapi/BaseClient.py +++ b/sfa/server/modpythonapi/BaseClient.py @@ -10,31 +10,31 @@ def EnableVerboseExceptions(x=True): class ExceptionUnmarshaller(xmlrpclib.Unmarshaller): def close(self): - try: - return xmlrpclib.Unmarshaller.close(self) - except xmlrpclib.Fault, e: - # if the server tagged some traceback info onto the end of the - # exception text, then print it out on the client. - - if "\nFAULT_TRACEBACK:" in e.faultString: - parts = e.faultString.split("\nFAULT_TRACEBACK:") - e.faultString = parts[0] - if VerboseExceptions: - print "\n|Server Traceback:", "\n|".join(parts[1].split("\n")) - - raise e - + try: + return xmlrpclib.Unmarshaller.close(self) + except xmlrpclib.Fault, e: + # if the server tagged some traceback info onto the end of the + # exception text, then print it out on the client. + + if "\nFAULT_TRACEBACK:" in e.faultString: + parts = e.faultString.split("\nFAULT_TRACEBACK:") + e.faultString = parts[0] + if VerboseExceptions: + print "\n|Server Traceback:", "\n|".join(parts[1].split("\n")) + + raise e + class ExceptionReportingTransport(xmlrpclib.Transport): def make_connection(self, host): - import httplib - if host.startswith("https:"): - return httplib.HTTPS(host) - else: - return httplib.HTTP(host) - - def getparser(self): - unmarshaller = ExceptionUnmarshaller() - parser = xmlrpclib.ExpatParser(unmarshaller) + import httplib + if host.startswith("https:"): + return httplib.HTTPS(host) + else: + return httplib.HTTP(host) + + def getparser(self): + unmarshaller = ExceptionUnmarshaller() + parser = xmlrpclib.ExpatParser(unmarshaller) return parser, unmarshaller class BaseClient(): diff --git a/sfa/server/modpythonapi/ModPython.py b/sfa/server/modpythonapi/ModPython.py index 64ceb990..4e5f80a7 100755 --- a/sfa/server/modpythonapi/ModPython.py +++ b/sfa/server/modpythonapi/ModPython.py @@ -12,22 +12,10 @@ import traceback import xmlrpclib from mod_python import apache +from sfa.util.logging import sfa_logger from API import RemoteApi api = RemoteApi() -class unbuffered: - """ - Write to /var/log/httpd/error_log. See - - http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp - """ - - def write(self, data): - sys.stderr.write(data) - sys.stderr.flush() - -#log = unbuffered() - def handler(req): try: if req.method != "POST": @@ -64,5 +52,5 @@ def handler(req): except Exception, err: # Log error in /var/log/httpd/(ssl_)?error_log - print >> log, err, traceback.format_exc() + sfa_logger.log_exc("%r"%e) return apache.HTTP_INTERNAL_SERVER_ERROR diff --git a/sfa/server/sfa-server.py b/sfa/server/sfa-server.py index 326258ed..96b9625e 100755 --- a/sfa/server/sfa-server.py +++ b/sfa/server/sfa-server.py @@ -37,6 +37,7 @@ component_port=12346 import os, os.path import sys from optparse import OptionParser +from sfa.util.sfalogging import sfa_logger from sfa.trust.trustedroot import TrustedRootList from sfa.trust.certificate import Keypair, Certificate from sfa.trust.hierarchy import Hierarchy @@ -56,7 +57,7 @@ def daemon(): devnull = os.open(os.devnull, os.O_RDWR) os.dup2(devnull, 0) # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull - crashlog = os.open('/var/log/sfa.daemon', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644) + crashlog = os.open('/var/log/httpd/sfa_access_log', os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644) os.dup2(crashlog, 1) os.dup2(crashlog, 2) @@ -218,4 +219,7 @@ def main(): c.start() if __name__ == "__main__": - main() + try: + main() + except: + sfa_logger.log_exc_critical("SFA server is exiting") diff --git a/sfa/trust/auth.py b/sfa/trust/auth.py index d0d4abf4..9cb905d1 100644 --- a/sfa/trust/auth.py +++ b/sfa/trust/auth.py @@ -6,7 +6,6 @@ # -#import sfa.util.sfalogging from sfa.trust.certificate import Keypair, Certificate from sfa.trust.credential import Credential from sfa.trust.trustedroot import TrustedRootList diff --git a/sfa/trust/certificate.py b/sfa/trust/certificate.py index ca4858bb..64ac865f 100644 --- a/sfa/trust/certificate.py +++ b/sfa/trust/certificate.py @@ -48,7 +48,7 @@ from OpenSSL import crypto import M2Crypto from M2Crypto import X509 -import sfa.util.sfalogging +from sfa.util.sfalogging import sfa_logger from sfa.util.namespace import urn_to_hrn from sfa.util.faults import * @@ -79,7 +79,7 @@ def convert_public_key(key): try: k.load_pubkey_from_file(ssl_fn) except: - traceback.print_exc() + sfa_logger.log_exc("convert_public_key caught exception") k = None # remove the temporary files @@ -585,21 +585,21 @@ class Certificate: # if this cert is signed by a trusted_cert, then we are set for trusted_cert in trusted_certs: if self.is_signed_by_cert(trusted_cert): - sfa.util.sfalogging.logger.debug("Cert %s signed by trusted cert %s", self.get_subject(), trusted_cert.get_subject()) + sfa_logger.debug("Cert %s signed by trusted cert %s", self.get_subject(), trusted_cert.get_subject()) # verify expiration of trusted_cert ? if not trusted_cert.cert.has_expired(): return trusted_cert else: - sfa.util.sfalogging.logger.debug("Trusted cert %s is expired", trusted_cert.get_subject()) + sfa_logger.debug("Trusted cert %s is expired", trusted_cert.get_subject()) # if there is no parent, then no way to verify the chain if not self.parent: - #print self.get_subject(), "has no parent" + sfa_logger.debug("%r has no parent"%self.get_subject()) raise CertMissingParent(self.get_subject()) # if it wasn't signed by the parent... if not self.is_signed_by_cert(self.parent): - #print self.get_subject(), "is not signed by parent" + sfa_logger.debug("%r is not signed by parent"%self.get_subject()) return CertNotSignedByParent(self.get_subject()) # if the parent isn't verified... diff --git a/sfa/trust/credential.py b/sfa/trust/credential.py index 6384afcc..1c41b089 100644 --- a/sfa/trust/credential.py +++ b/sfa/trust/credential.py @@ -35,7 +35,7 @@ from tempfile import mkstemp from xml.dom.minidom import Document, parseString from dateutil.parser import parse -import sfa.util.sfalogging +from sfa.util.sfalogging import sfa_logger from sfa.trust.certificate import Keypair from sfa.trust.credential_legacy import CredentialLegacy from sfa.trust.rights import * @@ -662,7 +662,7 @@ class Credential(object): trusted_cert_objects.append(GID(filename=f)) ok_trusted_certs.append(f) except Exception, exc: - sfa.util.sfalogging.logger.error("Failed to load trusted cert from %s: %r", f, exc) + sfa_logger.error("Failed to load trusted cert from %s: %r", f, exc) trusted_certs = ok_trusted_certs # Use legacy verification if this is a legacy credential @@ -745,7 +745,7 @@ class Credential(object): # Maybe should be (hrn, type) = urn_to_hrn(root_cred_signer.get_urn()) root_cred_signer_type = root_cred_signer.get_type() if (root_cred_signer_type == 'authority'): - #sfa.util.sfalogging.logger.debug('Cred signer is an authority') + #sfa_logger.debug('Cred signer is an authority') # signer is an authority, see if target is in authority's domain hrn = root_cred_signer.get_hrn() if root_target_gid.get_hrn().startswith(hrn): diff --git a/sfa/trust/gid.py b/sfa/trust/gid.py index 678c1a3f..6adfec5f 100644 --- a/sfa/trust/gid.py +++ b/sfa/trust/gid.py @@ -30,7 +30,7 @@ import xmlrpclib import uuid -import sfa.util.sfalogging +from sfa.util.sfalogging import sfa_logger from sfa.trust.certificate import Certificate from sfa.util.namespace import * @@ -82,7 +82,7 @@ class GID(Certificate): Certificate.__init__(self, create, subject, string, filename) if subject: - sfa.util.sfalogging.logger.debug("Creating GID for subject: %s" % subject) + sfa_logger.debug("Creating GID for subject: %s" % subject) if uuid: self.uuid = int(uuid) if hrn: @@ -204,8 +204,7 @@ class GID(Certificate): if self.parent: # make sure the parent's hrn is a prefix of the child's hrn if not self.get_hrn().startswith(self.parent.get_hrn()): - #print self.get_hrn(), " ", self.parent.get_hrn() - raise GidParentHrn("This cert %s HRN doesnt start with parent HRN %s" % (self.get_hrn(), self.parent.get_hrn())) + raise GidParentHrn("This cert HRN %s doesnt start with parent HRN %s" % (self.get_hrn(), self.parent.get_hrn())) else: # make sure that the trusted root's hrn is a prefix of the child's trusted_gid = GID(string=trusted_root.save_to_string()) diff --git a/sfa/trust/hierarchy.py b/sfa/trust/hierarchy.py index 6ab509e1..1ba7fb11 100644 --- a/sfa/trust/hierarchy.py +++ b/sfa/trust/hierarchy.py @@ -17,6 +17,7 @@ import os +from sfa.util.sfalogging import sfa_logger from sfa.util.report import * from sfa.trust.certificate import Keypair from sfa.trust.credential import * @@ -181,7 +182,7 @@ class Hierarchy: pass if os.path.exists(privkey_filename): - print "using existing key", privkey_filename, "for authority", hrn + sfa_logger.debug("using existing key %r for authority %r"%(privkey_filename,hrn)) pkey = Keypair(filename = privkey_filename) else: pkey = Keypair(create = True) diff --git a/sfa/util/PostgreSQL.py b/sfa/util/PostgreSQL.py index 984e5149..7ae94426 100644 --- a/sfa/util/PostgreSQL.py +++ b/sfa/util/PostgreSQL.py @@ -18,7 +18,7 @@ import re from pprint import pformat from sfa.util.faults import * -from sfa.util.debug import * +from sfa.util.sfalogging import sfa_logger if not psycopg2: is8bit = re.compile("[\x80-\xff]").search @@ -179,21 +179,21 @@ class PostgreSQL: if not params: if self.debug: - print >> log,'execute0',query + sfa_logger.debug('execute0 %r'%query) cursor.execute(query) elif isinstance(params,dict): if self.debug: - print >> log,'execute-dict: params',params,'query',query%params + sfa_logger.debug('execute-dict: params=[%r] query=[%r]'%(params,query%params)) cursor.execute(query,params) elif isinstance(params,tuple) and len(params)==1: if self.debug: - print >> log,'execute-tuple',query%params[0] + sfa_logger.debug('execute-tuple %r'%(query%params[0])) cursor.execute(query,params[0]) else: param_seq=(params,) if self.debug: for params in param_seq: - print >> log,'executemany',query%params + sfa_logger.debug('executemany %r'%(query%params)) cursor.executemany(query, param_seq) (self.rowcount, self.description, self.lastrowid) = \ (cursor.rowcount, cursor.description, cursor.lastrowid) @@ -203,12 +203,11 @@ class PostgreSQL: except: pass uuid = commands.getoutput("uuidgen") - print >> log, "Database error %s:" % uuid - print >> log, e - print >> log, "Query:" - print >> log, query - print >> log, "Params:" - print >> log, pformat(params) + sfa_logger.error("Database error %s:" % uuid) + sfa_logger.error("Exception=%r"%e) + sfa_logger.error("Query=%r"%query) + sfa_logger.error("Params=%r"%pformat(params)) + sfa_logger.log_exc("PostgreSQL.execute caught exception") raise SfaDBError("Please contact support: %s" % str(e)) return cursor diff --git a/sfa/util/api.py b/sfa/util/api.py index 65ccc88b..1e131be4 100644 --- a/sfa/util/api.py +++ b/sfa/util/api.py @@ -11,11 +11,10 @@ import traceback import string import xmlrpclib -import sfa.util.sfalogging +from sfa.util.sfalogging import sfa_logger from sfa.trust.auth import Auth from sfa.util.config import * from sfa.util.faults import * -from sfa.util.debug import * from sfa.trust.credential import * from sfa.trust.certificate import * from sfa.util.namespace import * @@ -148,7 +147,7 @@ class BaseAPI: self.credential = None self.source = None self.time_format = "%Y-%m-%d %H:%M:%S" - self.logger=sfa.util.sfalogging.logger + self.logger=sfa_logger # load registries from sfa.server.registry import Registries @@ -243,7 +242,7 @@ class BaseAPI: except SfaFault, fault: result = fault except Exception, fault: - traceback.print_exc(file = log) + sfa_logger.log_exc("BaseAPI.handle has caught Exception") result = SfaAPIError(fault) diff --git a/sfa/util/componentserver.py b/sfa/util/componentserver.py index 542b4be0..50c312e0 100644 --- a/sfa/util/componentserver.py +++ b/sfa/util/componentserver.py @@ -18,12 +18,13 @@ import BaseHTTPServer import SimpleHTTPServer import SimpleXMLRPCServer from OpenSSL import SSL + +from sfa.util.sfalogging import sfa_logger from sfa.trust.certificate import Keypair, Certificate from sfa.trust.credential import * from sfa.util.faults import * from sfa.plc.api import ComponentAPI from sfa.util.server import verify_callback, ThreadedServer -from sfa.util.debug import log ## @@ -73,7 +74,7 @@ class SecureXMLRpcRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler): # internal error, report as HTTP server error self.send_response(500) self.end_headers() - traceback.print_exc() + sfa_logger.log_exc("componentserver.SecureXMLRpcRequestHandler.do_POST") else: # got a valid XML RPC response self.send_response(200) diff --git a/sfa/util/config.py b/sfa/util/config.py index 923df4f4..a3fe098e 100644 --- a/sfa/util/config.py +++ b/sfa/util/config.py @@ -20,8 +20,6 @@ import os.path import traceback -from sfa.util.debug import log - class Config: """ Parse the bash/Python/PHP version of the configuration file. Very diff --git a/sfa/util/debug.py b/sfa/util/debug.py deleted file mode 100644 index e5f6f3fb..00000000 --- a/sfa/util/debug.py +++ /dev/null @@ -1,57 +0,0 @@ -### $Id$ -### $URL$ - -import time -import sys -import syslog - -class unbuffered: - """ - Write to /var/log/httpd/error_log. See - - http://www.modpython.org/FAQ/faqw.py?req=edit&file=faq02.003.htp - """ - - def write(self, data): - sys.stderr.write(data) - sys.stderr.flush() - -log = unbuffered() - -def profile(callable): - """ - Prints the runtime of the specified callable. Use as a decorator, e.g., - - @profile - def foo(...): - ... - - Or, equivalently, - - def foo(...): - ... - foo = profile(foo) - - Or inline: - - result = profile(foo)(...) - """ - - def wrapper(*args, **kwds): - start = time.time() - result = callable(*args, **kwds) - end = time.time() - args = map(str, args) - args += ["%s = %s" % (name, str(value)) for (name, value) in kwds.items()] - print >> log, "%s (%s): %f s" % (callable.__name__, ", ".join(args), end - start) - return result - - return wrapper - -if __name__ == "__main__": - def sleep(seconds = 1): - time.sleep(seconds) - - sleep = profile(sleep) - - sleep(1) diff --git a/sfa/util/filter.py b/sfa/util/filter.py index bc22f1b5..2a3b8953 100644 --- a/sfa/util/filter.py +++ b/sfa/util/filter.py @@ -214,5 +214,4 @@ class Filter(Parameter, dict): clip_part += " ORDER BY " + ",".join(sorts) if clips: clip_part += " " + " ".join(clips) -# print 'where_part=',where_part,'clip_part',clip_part return (where_part,clip_part) diff --git a/sfa/util/method.py b/sfa/util/method.py index 484b77b3..9a3d457e 100644 --- a/sfa/util/method.py +++ b/sfa/util/method.py @@ -14,11 +14,10 @@ import textwrap import xmlrpclib -import sfa.util.sfalogging +from sfa.util.sfalogging import sfa_logger from sfa.util.faults import * from sfa.util.parameter import Parameter, Mixed, python_type, xmlrpc_type 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): @@ -93,9 +92,7 @@ class Method (object): 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" + sfa_logger.info(msg) return result @@ -106,9 +103,7 @@ class Method (object): # Prepend caller and method name to expected faults fault.faultString = caller + ": " + self.name + ": " + fault.faultString runtime = time.time() - start -# if self.api.config.SFA_API_DEBUG: -# traceback.print_exc() - sfa.util.sfalogging.log_exc("Method %s raised an exception"%self.name) + sfa_logger.log_exc("Method %s raised an exception"%self.name) raise fault diff --git a/sfa/util/policy.py b/sfa/util/policy.py index 2ff4fc42..aa68f430 100644 --- a/sfa/util/policy.py +++ b/sfa/util/policy.py @@ -4,7 +4,6 @@ import os from sfa.util.storage import * -from sfa.util.debug import log class Policy(SimpleStorage): diff --git a/sfa/util/record.py b/sfa/util/record.py index 6af31197..87360be6 100644 --- a/sfa/util/record.py +++ b/sfa/util/record.py @@ -11,7 +11,6 @@ from types import StringTypes from sfa.trust.gid import * -import sfa.util.report from sfa.util.rspec import * from sfa.util.parameter import * from sfa.util.namespace import * diff --git a/sfa/util/report.py b/sfa/util/report.py deleted file mode 100644 index 9a882f07..00000000 --- a/sfa/util/report.py +++ /dev/null @@ -1,11 +0,0 @@ -def trace(x, logger=None): - if logger: - logger.info(x) - else: - print x - -def error(x, logger=None): - if logger: - logger.error(x) - else: - print x diff --git a/sfa/util/rspec.py b/sfa/util/rspec.py index eedc7f5b..aab5592e 100644 --- a/sfa/util/rspec.py +++ b/sfa/util/rspec.py @@ -10,6 +10,7 @@ from types import StringTypes, ListType from lxml import etree from StringIO import StringIO +from sfa.util.sfalogging import sfa_logger class RSpec: @@ -234,15 +235,14 @@ class RSpec: except Exception, e: # logging.debug("%s: web file not found" % xsdURI) # logging.debug("Using local file %s" % self.xsd") - print e - print "Can't find %s on the web. Continuing." % xsdURI + sfa_logger.log_exc("rspec.parseXSD: can't find %s on the web. Continuing." % xsdURI) if not schemaDom: if os.path.exists(xsdURI): # logging.debug("using local copy.") - print "Using local %s" % xsdURI + sfa_logger.debug("rspec.parseXSD: Using local %s" % xsdURI) schemaDom = minidom.parse(xsdURI) else: - raise Exception("Can't find xsd locally") + raise Exception("rspec.parseXSD: can't find xsd locally") self.schemaDict = self.toDict(schemaDom.childNodes[0]) diff --git a/sfa/util/server.py b/sfa/util/server.py index 505c7889..2d8e13b2 100644 --- a/sfa/util/server.py +++ b/sfa/util/server.py @@ -20,13 +20,11 @@ import SimpleXMLRPCServer from OpenSSL import SSL from Queue import Queue -#import sfa.util.sfalogging from sfa.trust.certificate import Keypair, Certificate from sfa.trust.credential import * from sfa.util.faults import * from sfa.plc.api import SfaAPI from sfa.util.cache import Cache -#from sfa.util.debug import log ## # Verification callback for pyOpenSSL. We do our own checking of keys because # we have our own authentication spec. Thus we disable several of the normal @@ -111,7 +109,7 @@ class SecureXMLRpcRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler): except Exception, fault: # This should only happen if the module is buggy # internal error, report as HTTP server error - traceback.print_exc() + sfa_error.log_exc("server.do_POST") response = self.api.prepare_response(fault) #self.send_response(500) #self.end_headers() diff --git a/sfa/util/sfalogging.py b/sfa/util/sfalogging.py old mode 100644 new mode 100755 index 006250c3..ccb14009 --- a/sfa/util/sfalogging.py +++ b/sfa/util/sfalogging.py @@ -1,38 +1,113 @@ -import logging +#!/usr/bin/python + import os import traceback +import logging, logging.handlers + +class SfaLogging: + def __init__ (self,logfile,name=None,level=logging.INFO): + # default is to locate logger name from the logfile + if not name: + name=os.path.basename(logfile) + self.logger=logging.getLogger(name) + self.logger.setLevel(level) + try: + handler=logging.handlers.RotatingFileHandler(logfile,maxBytes=1000000, backupCount=5) + except IOError: + # This is usually a permissions error becaue the file is + # owned by root, but httpd is trying to access it. + tmplogfile=os.getenv("TMPDIR", "/tmp") + os.path.sep + os.path.basename(logfile) + handler=logging.handlers.RotatingFileHandler(tmplogfile,maxBytes=1000000, backupCount=5) + handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) + self.logger.addHandler(handler) + + def wrap(fun): + def wrapped(self,msg,*args,**kwds): + native=getattr(self.logger,fun.__name__) + return native(msg,*args,**kwds) + #wrapped.__doc__=native.__doc__ + return wrapped -#SFA access log initialization -TMPDIR = os.getenv("TMPDIR", "/tmp") -SFA_HTTPD_ACCESS_LOGFILE = TMPDIR + "/" + 'sfa_httpd_access.log' -SFA_ACCESS_LOGFILE='/var/log/sfa_access.log' -logger=logging.getLogger() -#logger.setLevel(logging.INFO) -logger.setLevel(logging.DEBUG) - -try: - logfile=logging.FileHandler(SFA_ACCESS_LOGFILE) -except IOError: - # This is usually a permissions error becaue the file is - # owned by root, but httpd is trying to access it. - logfile=logging.FileHandler(SFA_HTTPD_ACCESS_LOGFILE) + def setLevel(self,level): + self.logger.setLevel(level) + + @wrap + def critical(): pass + @wrap + def error(): pass + @wrap + def warning(): pass + @wrap + def info(): pass + @wrap + def debug(): pass -formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") -logfile.setFormatter(formatter) -logger.addHandler(logfile) -def get_sfa_logger(): - return logger - -# logs an exception - use in an except statement -def log_exc(message): - logger.error("%s BEG TRACEBACK"%message+"\n"+traceback.format_exc().strip("\n")) - logger.error("%s END TRACEBACK"%message) + # logs an exception - use in an except statement + def log_exc(self,message): + self.error("%s BEG TRACEBACK"%message+"\n"+traceback.format_exc().strip("\n")) + self.error("%s END TRACEBACK"%message) - -# for investigation purposes, can be placed anywhere -def log_stack(message): - to_log="".join(traceback.format_stack()) - logger.debug("%s BEG STACK"%message+"\n"+to_log) - logger.debug("%s END STACK"%message) + def log_exc_critical(self,message): + self.critical("%s BEG TRACEBACK"%message+"\n"+traceback.format_exc().strip("\n")) + self.critical("%s END TRACEBACK"%message) + # for investigation purposes, can be placed anywhere + def log_stack(self,message): + to_log="".join(traceback.format_stack()) + self.debug("%s BEG STACK"%message+"\n"+to_log) + self.debug("%s END STACK"%message) + +sfa_logger=SfaLogging(logfile='/var/log/sfa.log') +sfa_import_logger=SfaLogging(logfile='/var/log/sfa_import.log') + + +######################################## +import time + +def profile(callable): + """ + Prints the runtime of the specified callable. Use as a decorator, e.g., + + @profile + def foo(...): + ... + + Or, equivalently, + + def foo(...): + ... + foo = profile(foo) + + Or inline: + + result = profile(foo)(...) + """ + + def wrapper(*args, **kwds): + start = time.time() + result = callable(*args, **kwds) + end = time.time() + args = map(str, args) + args += ["%s = %s" % (name, str(value)) for (name, value) in kwds.items()] + sfa_logger.debug("%s (%s): %.02f s" % (callable.__name__, ", ".join(args), end - start)) + return result + + return wrapper + +if __name__ == '__main__': + print 'testing sfalogging into logger.log' + global sfa_logger + sfa_logger=SfaLogging('logger.log') + sfa_logger.critical("logger.critical") + sfa_logger.error("logger.error") + sfa_logger.warning("logger.warning") + sfa_logger.info("logger.info") + sfa_logger.debug("logger.debug") + sfa_logger.setLevel(logging.DEBUG) + sfa_logger.debug("logger.debug again") + + @profile + def sleep(seconds = 1): + time.sleep(seconds) + sleep(1) diff --git a/sfa/util/table.py b/sfa/util/table.py index 6c68776d..c0d7d4f4 100644 --- a/sfa/util/table.py +++ b/sfa/util/table.py @@ -11,7 +11,6 @@ import pgdb from sfa.util.PostgreSQL import * from sfa.trust.gid import * from sfa.util.record import * -from sfa.util.debug import * from sfa.util.config import * from sfa.util.filter import * -- 2.43.0