[Patches] [Developers] WarnLevel bold off unless output is to tty

Steve White steve.white at aei.mpg.de
Thu Jun 8 08:56:10 CDT 2006


Tom,

For the record, attached is the replacement, which no longer does
the autoconfig stuff to look for isatty.

On  8.06.06, Tom Goodale wrote:
> 
> Hi Steve,
> 
> I just double checked the POSIX book, and fileno does seem to be posix, 
> even though it's not mentioned in the linux man pages.  I then looked at 
> isatty and that is also there !  So please go back to your original patch 
> and apply.
> 
> Lesson for the day is that if the Linux man pages don't mention a function 
> is POSIX, that doesn't mean the function isn't.
> 
> Sorry for the extra work.
> 
> Tom
> 
> On Sat, 22 Apr 2006, Steve White wrote:
> 
> > Tom,
> >
> > I started to use fileno(), and then discovered it was yet another
> > non-POSIX function!  It would require another build-time test.  Overall,
> > the difference in the amount of code isn't much.
> >
> > If this code were to be used in a more general context, one would use
> > fileno() for general streams.  Here, the streams are only stdout and stderr,
> > and used only a few time.  So I think it's appropriate as-is.
> >
> > My only concern is, isatty() is defined in unistd.h. but the function
> > and the header are tested for independently!  I'm not sure what to do
> > in such situations.
> >
> > Cheers!
> >
> >
> > On 21.04.06, Tom Goodale wrote:
> >>
> >> This is looking good.  One thing, though, you could reduce the amount of
> >> code by using the fileno() function to get the file descriptor from the
> >> stream, thus allowing you to have a function
> >>
> >> static void bold_stream(FILE *this, BOLDING on)
> >> {
> >> ...
> >> }
> >>
> >> Cheers,
> >>
> >> Tom
> >>
> >> On Fri, 21 Apr 2006, Steve White wrote:
> >>
> >>>
> >>> Here is an updated version, which handles systems that don't have
> >>> isatty
> >>>
> >>> On 16.04.06, Steve White wrote:
> >>>> Hi,
> >>>>
> >>>> I got tired of seeing all the terminal control characters in Cactus
> >>>> output that was piped to files.
> >>>>
> >>>> Patch attached.
> >>>>
> >>>> Tested by watching Cactus output on a terminal, then piping ouput from the
> >>>> same program to a file.  E.g. in bash
> >>>>
> >>>> 	make config-testsuite 2>&1 |tee > ts
> >>>

-- 
Steve White : Programmer
Max-Planck-Institut für Gravitationsphysik      Albert-Einstein-Institut
Am Mühlenberg 1, D-14476 Golm, Germany                  +49-331-567-7625
-------------- next part --------------
Index: src/main/WarnLevel.c
===================================================================
RCS file: /cactusdevcvs/Cactus/src/main/WarnLevel.c,v
retrieving revision 1.76
diff -u -r1.76 WarnLevel.c
--- src/main/WarnLevel.c	13 Dec 2005 16:31:40 -0000	1.76
+++ src/main/WarnLevel.c	8 Jun 2006 13:56:10 -0000
@@ -15,6 +15,10 @@
 #include <stdarg.h>
 #include <string.h>
 
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
 #if TIME_WITH_SYS_TIME
 # include <sys/time.h>
 # include <time.h>
@@ -46,6 +50,7 @@
  ********************************************************************/
 /* Escape sequences to highlight warning messages.
  */
+
 #ifndef WIN32
 #define BOLD_ON  "\033[1m"
 #define BOLD_OFF "\033[0m"
@@ -54,6 +59,43 @@
 #define BOLD_OFF ""
 #endif
 
+typedef enum { ON = 0, OFF = 1 } BOLDING;
+
+static void bold_stdout (BOLDING on)
+{
+  const char *val = (on == ON) ? BOLD_ON : BOLD_OFF;
+
+  if (!isatty (STDOUT_FILENO))
+    val = "";
+
+  fprintf (stdout, "%s", val);
+}
+
+static void bold_stderr (BOLDING on)
+{
+  const char *val = (on == ON) ? BOLD_ON : BOLD_OFF;
+
+  if (!isatty (STDERR_FILENO))
+    val = "";
+
+  fprintf (stderr, "%s", val);
+}
+
+static void print_bold_stderr (int do_bold, const char *fmt, ...)
+{
+  va_list args;
+
+  if (do_bold)
+    bold_stderr (ON);
+
+  va_start (args, fmt);
+  vfprintf (stderr, fmt, args);
+  va_end (args);
+
+  if (do_bold)
+    bold_stderr (OFF);
+}
+
 /* maximum buffer length to hold the hostname */
 #define MAXNAMELEN 255
 
@@ -534,7 +576,7 @@
 
       if (highlight_warning_messages)
       {
-        fprintf (stderr, BOLD_ON);
+        bold_stderr(ON);
       }
 
       if (level <= error_level || cctk_full_warnings)
@@ -552,7 +594,7 @@
 
       if (highlight_warning_messages)
       {
-        fprintf (stderr, BOLD_OFF);
+        bold_stderr (OFF);
       }
 
       fprintf (stderr, " ");
@@ -567,7 +609,7 @@
 
       if (highlight_warning_messages)
       {
-        fprintf (stdout, BOLD_ON);
+        bold_stdout(ON);
       }
 
       if (level <= error_level || cctk_full_warnings)
@@ -585,7 +627,7 @@
 
       if (highlight_warning_messages)
       {
-        fprintf (stdout, BOLD_OFF);
+        bold_stdout(OFF);
       }
 
       fprintf (stdout, " ");
@@ -670,16 +712,10 @@
   highlight_warning_messages =
     ! highlight_warning_messages_ptr || *highlight_warning_messages_ptr;
 
-  if (highlight_warning_messages)
-  {
-    fprintf (stderr, BOLD_ON "PARAM %s (%s):" BOLD_OFF " %s\n",
+  print_bold_stderr ( highlight_warning_messages, "PARAM %s (%s): %s",
              cctk_strong_param_check ? "ERROR" : "WARNING", thorn, message);
-  }
-  else
-  {
-    fprintf (stderr, "PARAM %s (%s): %s\n",
-             cctk_strong_param_check ? "ERROR" : "WARNING", thorn, message);
-  }
+  fprintf (stderr, "\n");
+
   param_errors++;
 
   return (0);
@@ -747,16 +783,8 @@
   highlight_warning_messages =
     ! highlight_warning_messages_ptr || *highlight_warning_messages_ptr;
 
-  if (highlight_warning_messages)
-  {
-    fprintf (stderr, BOLD_ON "PARAM %s (%s)" BOLD_OFF ": ",
-             cctk_strong_param_check ? "ERROR" : "WARNING", thorn);
-  }
-  else
-  {
-    fprintf (stderr, "PARAM %s (%s): ",
+  print_bold_stderr ( highlight_warning_messages, "PARAM %s (%s): ",
              cctk_strong_param_check ? "ERROR" : "WARNING", thorn);
-  }
 
   va_start (ap, format);
   vfprintf (stderr, format, ap);


More information about the Patches mailing list