1455
1455
The file is removed as the test is torn down.
1457
fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
1458
self._log_file = os.fdopen(fileno, 'w+')
1457
self._log_file = StringIO()
1459
1458
self._log_memento = bzrlib.trace.push_log_file(self._log_file)
1460
self._log_file_name = name
1461
1459
self.addCleanup(self._finishLogFile)
1463
1461
def _finishLogFile(self):
1665
1663
unicodestr = self._log_contents.decode('utf8', 'replace')
1666
1664
self._log_contents = unicodestr.encode('utf8')
1667
1665
return self._log_contents
1669
if bzrlib.trace._trace_file:
1670
# flush the log file, to get all content
1671
bzrlib.trace._trace_file.flush()
1672
if self._log_file_name is not None:
1673
logfile = open(self._log_file_name)
1675
log_contents = logfile.read()
1666
if self._log_file is not None:
1667
log_contents = self._log_file.getvalue()
1679
1669
log_contents.decode('utf8')
1680
1670
except UnicodeDecodeError:
1681
1671
unicodestr = log_contents.decode('utf8', 'replace')
1682
1672
log_contents = unicodestr.encode('utf8')
1683
1673
if not keep_log_file:
1685
max_close_attempts = 100
1686
first_close_error = None
1687
while close_attempts < max_close_attempts:
1690
self._log_file.close()
1691
except IOError, ioe:
1692
if ioe.errno is None:
1693
# No errno implies 'close() called during
1694
# concurrent operation on the same file object', so
1695
# retry. Probably a thread is trying to write to
1697
if first_close_error is None:
1698
first_close_error = ioe
1703
if close_attempts > 1:
1705
'Unable to close log file on first attempt, '
1706
'will retry: %s\n' % (first_close_error,))
1707
if close_attempts == max_close_attempts:
1709
'Unable to close log file after %d attempts.\n'
1710
% (max_close_attempts,))
1711
1674
self._log_file = None
1712
1675
# Permit multiple calls to get_log until we clean it up in
1713
1676
# finishLogFile
1714
1677
self._log_contents = log_contents
1716
os.remove(self._log_file_name)
1718
if sys.platform == 'win32' and e.errno == errno.EACCES:
1719
sys.stderr.write(('Unable to delete log file '
1720
' %r\n' % self._log_file_name))
1723
self._log_file_name = None
1724
1678
return log_contents
1726
return "No log file content and no log file name."
1680
return "No log file content."
1728
1682
def get_log(self):
1729
1683
"""Get a unicode string containing the log from bzrlib.trace.