]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Cleaned up vg_messages.c and related printf stuff. vg_messages.c is now a
authorNicholas Nethercote <njn@valgrind.org>
Thu, 12 May 2005 03:51:15 +0000 (03:51 +0000)
committerNicholas Nethercote <njn@valgrind.org>
Thu, 12 May 2005 03:51:15 +0000 (03:51 +0000)
layer above the printf stuff in vg_mylibc.c, which is layered over
m_debuglog.  This makes the module interfaces neater, more consistent, and
cuts 40 lines of code.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@3670

coregrind/core.h
coregrind/vg_messages.c
coregrind/vg_mylibc.c
include/tool.h

index 8e035d73e7e20909ccda5f4152016d96bca941a1..82076e1ed037c9a3320420afb66831b2e7cfad11 100644 (file)
@@ -635,14 +635,6 @@ extern void   VG_(env_remove_valgrind_env_stuff) ( Char** env );
 
 extern void   VG_(nanosleep)(struct vki_timespec *);
 
-/* ---------------------------------------------------------------------
-   Exports of vg_message.c
-   ------------------------------------------------------------------ */
-
-/* Low-level -- send bytes directly to the message sink.  Do not
-   use. */
-extern void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes );
-
 /* ---------------------------------------------------------------------
    Exports of vg_translate.c
    ------------------------------------------------------------------ */
index 1314b3b7a88d7f20abf269dc586329cf1543f483..59825f5eb9cb7bc7ba2e1588de817e81f5784e02 100644 (file)
    The GNU General Public License is contained in the file COPYING.
 */
 
-
 #include "core.h"
-#include "pub_core_debuglog.h"  /* VG_(debugLog_vprintf) */
 
 #include <time.h>
 #include <sys/time.h>
 
-/* Size of a buffer used for creating messages. */
-#define M_MSGBUF 10000
-
-static char mbuf[M_MSGBUF];
-static int n_mbuf;
-
-static void add_to_buf ( HChar c, void *p )
-{
-  if (n_mbuf >= (M_MSGBUF-1)) return;
-  mbuf[n_mbuf++] = c;
-  mbuf[n_mbuf]   = 0;
-}
-
-static void add_timestamp ( Char *buf )
-{
-   struct timeval tv;
-   struct tm tm;
-  
-   if ( gettimeofday( &tv, NULL ) == 0 &&
-        localtime_r( &tv.tv_sec, &tm ) == &tm ) {
-      VG_(sprintf)( buf, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
-                    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
-                    tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 );
-   }
-   else {
-      VG_(strcpy)( buf, "" );
-   }
-   
-   return;
-}
-
-static int add_to_msg ( const Char *format, ... )
+UInt VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs )
 {
-   int count;
-   va_list vargs;
-   va_start(vargs,format);
-   count = VG_(debugLog_vprintf) ( add_to_buf, NULL, format, vargs );
-   va_end(vargs);
-   return count;
-}
-
-static int start_msg ( VgMsgKind kind )
-{
-   Char ts[32];
-   Char c;
+   UInt  count = 0;
+   Char  c;
+   const Char* pfx_s;
    static const Char pfx[] = ">>>>>>>>>>>>>>>>";
-   n_mbuf = 0;
-   mbuf[n_mbuf] = 0;
 
-   if (VG_(clo_time_stamp))
-     add_timestamp(ts);
-   else
-     VG_(strcpy)(ts, "");
    switch (kind) {
       case Vg_UserMsg:       c = '='; break;
       case Vg_DebugMsg:      c = '-'; break;
@@ -96,39 +48,40 @@ static int start_msg ( VgMsgKind kind )
       case Vg_ClientMsg:     c = '*'; break;
       default:               c = '?'; break;
    }
+
    // The pfx trick prints one or more '>' characters in front of the
    // messages when running Valgrind under Valgrind, one per level of
    // self-hosting.
-   return add_to_msg( "%s%c%c%s%d%c%c ", 
-                      &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND],
-                      c,c, ts, VG_(getpid)(), c,c );
-}
-
-static 
-int end_msg ( void )
-{
-   int count = 0;
-   if (VG_(clo_log_fd) >= 0) {
-      add_to_buf('\n',0);
-      VG_(send_bytes_to_logging_sink) ( mbuf, VG_(strlen)(mbuf) );
-      count = 1;
+   pfx_s = &pfx[sizeof(pfx)-1-RUNNING_ON_VALGRIND],
+
+   // Print the message
+   count = 0;
+   count += VG_(printf) ("%s%c%c", pfx_s, c,c);
+
+   if (VG_(clo_time_stamp)) {
+      struct timeval tv;
+      struct tm tm;
+     
+      if ( gettimeofday( &tv, NULL ) == 0 &&
+           localtime_r( &tv.tv_sec, &tm ) == &tm )
+      {
+         count +=
+            VG_(printf)( "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
+                         tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
+                         tm.tm_hour, tm.tm_min, tm.tm_sec, tv.tv_usec / 1000 );
+      }
    }
-   return count;
-}
 
-int VG_(vmessage) ( VgMsgKind kind, const Char* format, va_list vargs )
-{
-   int count;
-   count = start_msg ( kind );
-   count += VG_(debugLog_vprintf) ( add_to_buf, NULL, format, vargs );
-   count += end_msg();
+   count += VG_(printf) ("%d%c%c ", VG_(getpid)(), c,c);
+   count += VG_(vprintf)(format, vargs);
+   count += VG_(printf) ("\n");
    return count;
 }
 
 /* Send a simple single-part message. */
-int VG_(message) ( VgMsgKind kind, const Char* format, ... )
+UInt VG_(message) ( VgMsgKind kind, const Char* format, ... )
 {
-   int count;
+   UInt count;
    va_list vargs;
    va_start(vargs,format);
    count = VG_(vmessage) ( kind, format, vargs );
@@ -136,25 +89,6 @@ int VG_(message) ( VgMsgKind kind, const Char* format, ... )
    return count;
 }
 
-/* Do the low-level send of a message to the logging sink. */
-void VG_(send_bytes_to_logging_sink) ( Char* msg, Int nbytes )
-{
-   Int rc;
-   if (VG_(logging_to_filedes)) {
-      VG_(write)( VG_(clo_log_fd), msg, nbytes );
-   } else {
-      rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes );
-      if (rc == -1) {
-         /* for example, the listener process died.  Switch back to
-            stderr. */
-         VG_(logging_to_filedes) = True;
-         VG_(clo_log_to) = VgLogTo_Fd;
-         VG_(clo_log_fd) = 2;
-         VG_(write)( VG_(clo_log_fd), msg, nbytes );
-      }
-   }
-}
-
 /*--------------------------------------------------------------------*/
 /*--- end                                                          ---*/
 /*--------------------------------------------------------------------*/
index 750d59ba05ebcbce77840a40d4836409b4105573..f9cf01758cf691ff423e845a2b93dee3fe3eb005 100644 (file)
@@ -390,41 +390,67 @@ Int VG_(poll)( struct vki_pollfd *ufds, UInt nfds, Int timeout)
    debugging info should be sent via here.  The official route is to
    to use vg_message().  This interface is deprecated.
 */
+
+/* Do the low-level send of a message to the logging sink. */
+static void send_bytes_to_logging_sink ( Char* msg, Int nbytes )
+{
+   if (VG_(logging_to_filedes)) {
+      VG_(write)( VG_(clo_log_fd), msg, nbytes );
+   } else {
+      Int rc = VG_(write_socket)( VG_(clo_log_fd), msg, nbytes );
+      if (rc == -1) {
+         // For example, the listener process died.  Switch back to stderr.
+         VG_(logging_to_filedes) = True;
+         VG_(clo_log_to) = VgLogTo_Fd;
+         VG_(clo_log_fd) = 2;
+         VG_(write)( VG_(clo_log_fd), msg, nbytes );
+      }
+   }
+}
+
 typedef struct {
    char buf[100];
    int n;
 } printf_buf;
 
+// Adds a single char to the buffer.  When the buffer gets sufficiently
+// full, we write its contents to the logging sink.
 static void add_to_myprintf_buf ( HChar c, void *p )
 {
    printf_buf *myprintf_buf = (printf_buf *)p;
    
    if (myprintf_buf->n >= 100-10 /*paranoia*/ ) {
-      if (VG_(clo_log_fd) >= 0) {
-         VG_(send_bytes_to_logging_sink)( 
-            myprintf_buf->buf, VG_(strlen)(myprintf_buf->buf) );
-      }
+      send_bytes_to_logging_sink( myprintf_buf->buf, myprintf_buf->n );
       myprintf_buf->n = 0;
-      myprintf_buf->buf[myprintf_buf->n] = 0;      
    }
    myprintf_buf->buf[myprintf_buf->n++] = c;
-   myprintf_buf->buf[myprintf_buf->n] = 0;
+   myprintf_buf->buf[myprintf_buf->n]   = 0;
 }
 
-UInt VG_(printf) ( const char *format, ... )
+UInt VG_(vprintf) ( const char *format, va_list vargs )
 {
-   UInt ret;
-   va_list vargs;
+   UInt ret = 0;
    printf_buf myprintf_buf = {"",0};
-   va_start(vargs,format);
-   
-   ret = VG_(debugLog_vprintf) 
-            ( add_to_myprintf_buf, &myprintf_buf, format, vargs );
 
-   if (myprintf_buf.n > 0 && VG_(clo_log_fd) >= 0) {
-      VG_(send_bytes_to_logging_sink)( myprintf_buf.buf, myprintf_buf.n );
+   if (VG_(clo_log_fd) >= 0) {
+      ret = VG_(debugLog_vprintf) 
+               ( add_to_myprintf_buf, &myprintf_buf, format, vargs );
+
+      // Write out any chars left in the buffer.
+      if (myprintf_buf.n > 0) {
+         send_bytes_to_logging_sink( myprintf_buf.buf, myprintf_buf.n );
+      }
    }
+   return ret;
+}
+
+UInt VG_(printf) ( const char *format, ... )
+{
+   UInt ret;
+   va_list vargs;
 
+   va_start(vargs, format);
+   ret = VG_(vprintf)(format, vargs);
    va_end(vargs);
 
    return ret;
@@ -437,25 +463,32 @@ static void add_to_vg_sprintf_buf ( HChar c, void *p )
    *(*vg_sprintf_ptr)++ = c;
 }
 
-UInt VG_(sprintf) ( Char* buf, Char *format, ... )
+UInt VG_(vsprintf) ( Char* buf, const Char *format, va_list vargs )
 {
    Int ret;
-   va_list vargs;
    Char *vg_sprintf_ptr = buf;
 
-   va_start(vargs,format);
-
    ret = VG_(debugLog_vprintf) 
             ( add_to_vg_sprintf_buf, &vg_sprintf_ptr, format, vargs );
-   add_to_vg_sprintf_buf(0,&vg_sprintf_ptr);
-
-   va_end(vargs);
+   add_to_vg_sprintf_buf('\0', &vg_sprintf_ptr);
 
    vg_assert(VG_(strlen)(buf) == ret);
 
    return ret;
 }
 
+UInt VG_(sprintf) ( Char* buf, const Char *format, ... )
+{
+   UInt ret;
+   va_list vargs;
+
+   va_start(vargs,format);
+   ret = VG_(vsprintf)(buf, format, vargs);
+   va_end(vargs);
+
+   return ret;
+}
+
 
 /* ---------------------------------------------------------------------
    Misc str* functions.
@@ -935,7 +968,7 @@ void VG_(assert_fail) ( Bool isCore, const Char* expr, const Char* file,
    entered = True;
 
    va_start(vargs,format);
-   VG_(debugLog_vprintf) ( add_to_vg_sprintf_buf, &bufptr, format, vargs );
+   VG_(vsprintf) ( bufptr, format, vargs );
    add_to_vg_sprintf_buf('\0', &bufptr);
    va_end(vargs);
 
index cdf5849ebd2a3865c7e81b937e3c0fe89d9d3521..9ba5644aa52b8c6725e76a9f723261a39dc06aca 100644 (file)
@@ -139,8 +139,8 @@ typedef
    VgMsgKind;
 
 /* Send a single-part message.  Appends a newline. */
-extern int VG_(message)    ( VgMsgKind kind, const Char* format, ... );
-extern int VG_(vmessage)   ( VgMsgKind kind, const Char* format, va_list vargs );
+extern UInt VG_(message)    ( VgMsgKind kind, const Char* format, ... );
+extern UInt VG_(vmessage)   ( VgMsgKind kind, const Char* format, va_list vargs );
 
 
 /*====================================================================*/
@@ -262,8 +262,10 @@ extern Addr VG_(get_IP) ( ThreadId tid );
  * Hence no need for VG_(fprintf)().
  */
 extern UInt VG_(printf)  ( const char *format, ... );
+extern UInt VG_(vprintf) ( const char *format, va_list vargs );
 /* too noisy ...  __attribute__ ((format (printf, 1, 2))) ; */
-extern UInt VG_(sprintf) ( Char* buf, Char *format, ... );
+extern UInt VG_(sprintf) ( Char* buf, const Char* format, ... );
+extern UInt VG_(vsprintf)( Char* buf, const Char* format, va_list vargs );
 
 extern Int  VG_(rename) ( Char* old_name, Char* new_name );