Should mutter() flush the log file?

John A Meinel john at arbash-meinel.com
Tue Dec 27 22:44:23 GMT 2005


I was working on rewriting mutter() so that it never fails.
Specifically, I was doing:

=== modified file 'bzrlib/trace.py'
--- bzrlib/trace.py
+++ bzrlib/trace.py
@@ -94,7 +94,12 @@
     else:
         out = fmt
     out += '\n'
-    _trace_file.write(out)
+    try:
+        _trace_file.write(out)
+    except UnicodeError, e:
+        warning('UnicodeError: %s', e)
+        _trace_file.write(repr(out))
 debug = mutter

I was writing a test, to make sure that mutter() doesn't fail, even if
you pass in a bogus string. My test did this:

    def test_mutter_never_fails(self):
        # Even if the decode/encode stage fails, mutter should not
        # raise an exception
        mutter(u'Writing a greek mu (\xb5) works in a unicode string')
        mutter('But fails in an ascii string \xb5')
        log = self._get_log()
        self.assertContainsRe(log, 'Writing a greek mu')
        self.assertContainsRe(log, 'UnicodeError')
        self.assertContainsRe(log, "'But fails in an ascii string")

The problem is that the log file doesn't actually contain the last line,
because it hasn't been flushed to disk yet.
Now, I could but in some sort of "bzrlib.trace._trace_file.flush()"
before I place the call to _get_log(), but it lead me to wonder if
mutter() should be doing the flush itself.

I don't think we want mutter() to do the flush, because we want mutter
to be relatively cheap, and a flush() after each statement would add a
bit of expense.

I know if we get a segfault, we would lose anything that isn't flushed,
but segfaults are rare. Though I'm thinking that if BZR_DEBUG is set,
then it should flush, because you are trying to track down a bug, and
that is extra frustrating when log files aren't getting flushed.

Any comments?

To be clear, I'm currently planning to update my test so that it does
its own flush, and then changing mutter() so that it flushes if
BZR_DEBUG is set.

John
=:->

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 249 bytes
Desc: OpenPGP digital signature
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20051227/11e42108/attachment.pgp 


More information about the bazaar mailing list