[Patches] [patch] make CCTK_VWarn() flush stdout/stderr before printing error msg

Jonathan Thornburg jthorn at aei.mpg.de
Tue Aug 29 08:27:02 CDT 2006


The current-CVS version of CCTK_VWarn() does not flush stdout or stderr
before printing the warning message.  Therefore, if Cactus's stdout and
stderr are being combined into a single output stream, as with
  bash% cactus_foo myrun.par 2>&1 | tee logfile
  csh% cactus_foo myrun.par |& tee logfile
then CCTK_VWarn() messages usually appear out of sequence with respect
to output from both CCTK_VInfo() and printf().  IMHO this is a serious
problem: it means that it's hard to tell from looking at the combined
stdout/stderr what the program has done before vs after a warning message
was generated.

I propose the enclosed patch, which fixes this by changing CCTK_VWarn()
to flush stdout and stderr before printing its own warning message.
(Note that CCTK_VWarn() may print either only to stderr, or to both
stdout and stderr, depending on which processor this is, so to be safe
we flush both streams.)

ciao,

-- 
-- Jonathan Thornburg <jthorn at aei.mpg.de>      
   Max-Planck-Institut fuer Gravitationsphysik (Albert-Einstein-Institut),
   Golm, Germany, "Old Europe"     http://www.aei.mpg.de/~jthorn/home.html      
   "Washing one's hands of the conflict between the powerful and the
    powerless means to side with the powerful, not to be neutral."
                                      -- quote by Freire / poster by Oxfam
-------------- next part --------------
Index: WarnLevel.c
===================================================================
RCS file: /cactusdevcvs/Cactus/src/main/WarnLevel.c,v
retrieving revision 1.78
diff -u -r1.78 WarnLevel.c
--- WarnLevel.c	6 Jul 2006 09:14:46 -0000	1.78
+++ WarnLevel.c	29 Aug 2006 13:24:54 -0000
@@ -518,6 +518,19 @@
   char *message = NULL;
   char hostname[MAXNAMELEN+1];
 
+  /*
+   * If stdout and stderr are merged into a single output stream, as with
+   *    basn%   cactus_foo myrun.par 2>&1 | tee logfile
+   *    csh%    cactus_foo myrun.par |& tee logfile
+   * then we need to flush buffers before switching from one to another,
+   * in order to ensure that stderr messages appear correctly sequenced
+   * with respect to stdout output.  Note we need to flush *both* stdout
+   * and stderr since this function may print to just stderr, or to both
+   * of these, depending on which processor this is.
+   */
+  fflush(stdout);
+  fflush(stderr);
+
   /* Start generating message only if the warbcallback list is not NULL */
   if(warncallbacks)
   {


More information about the Patches mailing list