From: W. Trevor King Date: Thu, 24 Jan 2013 07:31:52 +0000 (-0500) Subject: Transition to libbe.LOG for logging X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=932679f1e82a57feb4743e52d6c60f7331f057c2;p=be.git Transition to libbe.LOG for logging This makes it easier to tweak log verbosity and redirect logs to other handlers. For example, the WSGI servers are unstable with stderr closed, and crash with an IOError if they try to print a warning to stderr. --- diff --git a/libbe/__init__.py b/libbe/__init__.py index efd744a..b297636 100644 --- a/libbe/__init__.py +++ b/libbe/__init__.py @@ -37,6 +37,14 @@ The available submodules are: * :py:mod:`libbe._version` """ +import logging as _logging + + +LOG = _logging.getLogger('be') +LOG.addHandler(_logging.StreamHandler()) +LOG.setLevel(_logging.ERROR) + + TESTING = False """Flag controlling test-suite generation. diff --git a/libbe/bug.py b/libbe/bug.py index 66510ba..bb7a37d 100644 --- a/libbe/bug.py +++ b/libbe/bug.py @@ -439,7 +439,7 @@ class Bug (settings_object.SavedSettingsObject): sep = '\n' + istring return istring + sep.join(lines).rstrip('\n') - def from_xml(self, xml_string, preserve_uuids=False, verbose=True): + def from_xml(self, xml_string, preserve_uuids=False): u""" Note: If a bug uuid is given, set .alt_id to it's value. >>> bugA = Bug(uuid="0123", summary="Need to test Bug.from_xml()") @@ -451,7 +451,7 @@ class Bug (settings_object.SavedSettingsObject): >>> commC = commA.new_reply(body='comment C') >>> xml = bugA.xml(show_comments=True) >>> bugB = Bug() - >>> bugB.from_xml(xml, verbose=True) + >>> bugB.from_xml(xml) >>> bugB.xml(show_comments=True) == xml False >>> bugB.uuid = bugB.alt_id @@ -489,8 +489,7 @@ class Bug (settings_object.SavedSettingsObject): pass elif child.tag == 'comment': comm = comment.Comment(bug=self) - comm.from_xml( - child, preserve_uuids=preserve_uuids, verbose=verbose) + comm.from_xml(child, preserve_uuids=preserve_uuids) comments.append(comm) continue elif child.tag in tags: @@ -515,9 +514,10 @@ class Bug (settings_object.SavedSettingsObject): attr_name = child.tag.replace('-','_') self.explicit_attrs.append(attr_name) setattr(self, attr_name, text) - elif verbose == True: - print >> sys.stderr, 'Ignoring unknown tag %s in %s' \ - % (child.tag, comment.tag) + else: + libbe.LOG.warning( + 'ignoring unknown tag {0} in {1}'.format( + child.tag, comment.tag)) if uuid != self.uuid: if not hasattr(self, 'alt_id') or self.alt_id == None: self.alt_id = uuid @@ -610,8 +610,9 @@ class Bug (settings_object.SavedSettingsObject): parent = uuid_map[c.in_reply_to] except KeyError: if ignore_missing_references == True: - print >> sys.stderr, \ - 'Ignoring missing reference to %s' % c.in_reply_to + libbe.LOG.warning( + 'ignoring missing reference to {0}'.format( + c.in_reply_to)) parent = default_parent if parent.uuid != comment.INVALID_UUID: c.in_reply_to = parent.uuid diff --git a/libbe/bugdir.py b/libbe/bugdir.py index 40097f7..8b9e1e7 100644 --- a/libbe/bugdir.py +++ b/libbe/bugdir.py @@ -396,7 +396,7 @@ class BugDir (list, settings_object.SavedSettingsObject): sep = '\n' + istring return istring + sep.join(lines).rstrip('\n') - def from_xml(self, xml_string, preserve_uuids=False, verbose=True): + def from_xml(self, xml_string, preserve_uuids=False): """ Note: If a bugdir uuid is given, set .alt_id to it's value. >>> bug.load_severities(bug.severity_def) @@ -457,8 +457,7 @@ class BugDir (list, settings_object.SavedSettingsObject): pass elif child.tag == 'bug': bg = bug.Bug(bugdir=self) - bg.from_xml( - child, preserve_uuids=preserve_uuids, verbose=verbose) + bg.from_xml(child, preserve_uuids=preserve_uuids) self.append(bg, update=True) continue elif child.tag in tags: @@ -513,8 +512,9 @@ class BugDir (list, settings_object.SavedSettingsObject): attr_name = child.tag.replace('-','_') self.explicit_attrs.append(attr_name) setattr(self, attr_name, text) - elif verbose == True: - sys.stderr.write('Ignoring unknown tag {} in {}\n'.format( + else: + libbe.LOG.warning( + 'ignoring unknown tag {0} in {1}'.format( child.tag, bugdir.tag)) if uuid != self.uuid: if not hasattr(self, 'alt_id') or self.alt_id == None: diff --git a/libbe/command/import_xml.py b/libbe/command/import_xml.py index a16b0b0..fbf456b 100644 --- a/libbe/command/import_xml.py +++ b/libbe/command/import_xml.py @@ -203,11 +203,11 @@ class Import_XML (libbe.command.Command): text = text.decode('unicode_escape').strip() version[child.tag] = text else: - sys.stderr.write( - 'ignoring unknown tag {} in {}\n'.format( + libbe.LOG.warning( + 'ignoring unknown tag {0} in {1}\n'.format( gchild.tag, child.tag)) else: - sys.stderr.write('ignoring unknown tag {} in {}\n'.format( + libbe.LOG.warning('ignoring unknown tag {0} in {1}\n'.format( child.tag, be_xml.tag)) return (version, root_bugdirs, root_bugs, root_comments) diff --git a/libbe/comment.py b/libbe/comment.py index a669e4e..9bef50a 100644 --- a/libbe/comment.py +++ b/libbe/comment.py @@ -333,7 +333,7 @@ class Comment (Tree, settings_object.SavedSettingsObject): sep = '\n' + istring return istring + sep.join(lines).rstrip('\n') - def from_xml(self, xml_string, preserve_uuids=False, verbose=True): + def from_xml(self, xml_string, preserve_uuids=False): u""" Note: If alt-id is not given, translates any fields to fields. @@ -344,7 +344,7 @@ class Comment (Tree, settings_object.SavedSettingsObject): >>> commA.extra_strings += ['TAG: very helpful'] >>> xml = commA.xml() >>> commB = Comment() - >>> commB.from_xml(xml, verbose=True) + >>> commB.from_xml(xml) >>> commB.explicit_attrs ['author', 'date', 'content_type', 'body', 'alt_id'] >>> commB.xml() == xml @@ -397,9 +397,10 @@ class Comment (Tree, settings_object.SavedSettingsObject): attr_name = child.tag.replace('-','_') self.explicit_attrs.append(attr_name) setattr(self, attr_name, text) - elif verbose == True: - print >> sys.stderr, 'Ignoring unknown tag %s in %s' \ - % (child.tag, comment.tag) + else: + libbe.LOG.warning( + 'ignoring unknown tag {0} in {1}'.format( + child.tag, comment.tag)) if uuid != self.uuid and self.alt_id == None: self.explicit_attrs.append('alt_id') self.alt_id = uuid diff --git a/libbe/storage/vcs/base.py b/libbe/storage/vcs/base.py index 845336d..671df43 100644 --- a/libbe/storage/vcs/base.py +++ b/libbe/storage/vcs/base.py @@ -207,7 +207,7 @@ class CachedPathID (object): self._cache_path = os.path.join( self._root, self._spacer_dirs[0], 'id-cache') - def init(self, verbose=True, cache=None): + def init(self, cache=None): """Create cache file for an existing .be directory. The file contains multiple lines of the form:: @@ -227,8 +227,10 @@ class CachedPathID (object): id = self.id(dirpath) relpath = dirpath[len(self._root + os.path.sep):] if id.count('/') == 0: - if verbose == True and id in self._cache: - print >> sys.stderr, 'Multiple paths for %s: \n %s\n %s' % (id, self._cache[id], relpath) + if id in self._cache: + libbe.LOG.warning( + 'multiple paths for {0}:\n {1}\n {2}'.format( + id, self._cache[id], relpath)) self._cache[id] = relpath except InvalidPath: pass @@ -271,7 +273,7 @@ class CachedPathID (object): else: extra = fields[1:] if uuid not in self._cache: - self.init(verbose=False, cache=self._cache) + self.init(cache=self._cache) if uuid not in self._cache: raise InvalidID(uuid) if relpath == True: @@ -355,7 +357,6 @@ class VCS (libbe.storage.base.VersionedStorage): libbe.storage.base.VersionedStorage.__init__(self, *args, **kwargs) self.versioned = False self.interspersed_vcs_files = False - self.verbose_invoke = False self._cached_path_id = CachedPathID() self._rooted = False @@ -923,8 +924,6 @@ class VCS (libbe.storage.base.VersionedStorage): def _u_invoke(self, *args, **kwargs): if 'cwd' not in kwargs: kwargs['cwd'] = self.repo - if 'verbose' not in kwargs: - kwargs['verbose'] = self.verbose_invoke if 'encoding' not in kwargs: kwargs['encoding'] = self.encoding return invoke(*args, **kwargs) diff --git a/libbe/util/subproc.py b/libbe/util/subproc.py index 08980c9..f1f04c1 100644 --- a/libbe/util/subproc.py +++ b/libbe/util/subproc.py @@ -47,8 +47,7 @@ class CommandError(Exception): self.stderr = stderr def invoke(args, stdin=None, stdout=PIPE, stderr=PIPE, expect=(0,), - cwd=None, shell=None, unicode_output=True, verbose=False, - encoding=None, **kwargs): + cwd=None, shell=None, unicode_output=True, encoding=None, **kwargs): """ expect should be a tuple of allowed exit codes. cwd should be the directory from which the command will be executed. When @@ -63,8 +62,7 @@ def invoke(args, stdin=None, stdout=PIPE, stderr=PIPE, expect=(0,), else: list_args = args str_args = ' '.join(args) # sloppy, but just for logging - if verbose == True: - print >> sys.stderr, '%s$ %s' % (cwd, str_args) + libbe.LOG.debug('{0}$ {1}'.format(cwd, str_args)) try : if _POSIX: if shell is None: @@ -89,8 +87,7 @@ def invoke(args, stdin=None, stdout=PIPE, stderr=PIPE, expect=(0,), stdout = unicode(stdout, encoding) if stderr != None: stderr = unicode(stderr, encoding) - if verbose == True: - print >> sys.stderr, '%d\n%s%s' % (status, stdout, stderr) + libbe.LOG.debug('{0}\n{1}{2}'.format(status, stdout, stderr)) if status not in expect: raise CommandError(list_args, status, stdout, stderr) return status, stdout, stderr