util: debugstacktrace, flush before and after writing
authorMads Kiilerich <madski@unity3d.com>
Thu, 20 Feb 2014 02:38:36 +0100
changeset 20542 be27652675ce
parent 20541 44dcca9be1d5
child 20543 01df3660eb11
util: debugstacktrace, flush before and after writing Close another stream (default stdout, which often is buffered) before writing to the primary stream (default stderr, which often is unbuffered). The primary stream is also flushed after writing (in case it is buffered). This fixes non-deterministic output order, especially on windows.
mercurial/util.py
--- a/mercurial/util.py	Wed Feb 19 21:32:14 2014 -0800
+++ b/mercurial/util.py	Thu Feb 20 02:38:36 2014 +0100
@@ -1989,12 +1989,14 @@
         for source, hook in self._hooks:
             hook(*args)
 
-def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr):
+def debugstacktrace(msg='stacktrace', skip=0, f=sys.stderr, otherf=sys.stdout):
     '''Writes a message to f (stderr) with a nicely formatted stacktrace.
-    Skips the 'skip' last entries.
+    Skips the 'skip' last entries. By default it will flush stdout first.
     It can be used everywhere and do intentionally not require an ui object.
     Not be used in production code but very convenient while developing.
     '''
+    if otherf:
+        otherf.flush()
     f.write('%s at:\n' % msg)
     entries = [('%s:%s' % (fn, ln), func)
         for fn, ln, func, _text in traceback.extract_stack()[:-skip - 1]]
@@ -2002,6 +2004,7 @@
         fnmax = max(len(entry[0]) for entry in entries)
         for fnln, func in entries:
             f.write(' %-*s in %s\n' % (fnmax, fnln, func))
+    f.flush()
 
 # convenient shortcut
 dst = debugstacktrace