Move the code for generating a human-readable time string into its own
function.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4181 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/m_libcprint.c b/coregrind/m_libcprint.c
index e5f32d9..9adb661 100644
--- a/coregrind/m_libcprint.c
+++ b/coregrind/m_libcprint.c
@@ -41,6 +41,7 @@
#include <time.h>
#include <sys/time.h>
+
/* ---------------------------------------------------------------------
Writing to file or a socket
------------------------------------------------------------------ */
@@ -197,6 +198,29 @@
for (i = 0; i < space; i++) buf[i] = ' ';
}
+
+/* ---------------------------------------------------------------------
+ ctime()
+ ------------------------------------------------------------------ */
+
+/* BUF must be at least 25 characters long. This is unchecked. */
+
+void VG_(ctime) ( /*OUT*/HChar* buf )
+{
+ struct timeval tv;
+ struct tm tm;
+ buf[0] = 0;
+ 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 );
+ }
+}
+
+
/* ---------------------------------------------------------------------
message()
------------------------------------------------------------------ */
@@ -228,17 +252,9 @@
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 );
- }
+ HChar buf[50];
+ VG_(ctime)(buf);
+ count += VG_(printf)( "%s ", buf);
}
if (!VG_(clo_xml))
diff --git a/coregrind/pub_core_libcprint.h b/coregrind/pub_core_libcprint.h
index 303a430..15f99f8 100644
--- a/coregrind/pub_core_libcprint.h
+++ b/coregrind/pub_core_libcprint.h
@@ -42,6 +42,10 @@
descriptor or a socket descriptor. */
extern Bool VG_(logging_to_socket);
+/* Get a human-readable representation of the local time into BUF,
+ which must be at least 25 characters long. This is unchecked. */
+extern void VG_(ctime) ( /*OUT*/HChar* buf );
+
#endif // __PUB_CORE_LIBCPRINT_H
/*--------------------------------------------------------------------*/