This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] add timestamps to nscd debug logs


Hello,

When nscd debugging is enabled, the debug log file does not contain timestamps. If you're trying to debug a sporadic problem, it can be difficult to map debug log entries to when the problem happened.

Attached is a patch to add timestamps to the nscd debug log.

It also uses the thread id instead of the process id on Linux systems.

I've opened a bz at
  http://sources.redhat.com/bugzilla/show_bug.cgi?id=10742

Thank you,
Jeff Bastian
diff --git a/nscd/dbg_log.c b/nscd/dbg_log.c
index 5e192c9..2b17ef8 100644
--- a/nscd/dbg_log.c
+++ b/nscd/dbg_log.c
@@ -16,10 +16,13 @@
    along with this program; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
+#include <sys/syscall.h>
+#include <sys/types.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
 #include <syslog.h>
+#include <time.h>
 #include <unistd.h>
 #include "dbg_log.h"
 #include "nscd.h"
@@ -54,13 +57,27 @@ dbg_log (const char *fmt,...)
 {
   va_list ap;
   char msg[512], msg2[512];
+  pid_t id;
+  time_t rawtime;
+  struct tm now;
+  char datetime[32];
 
   va_start (ap, fmt);
   vsnprintf (msg2, sizeof (msg), fmt, ap);
 
   if (debug_level > 0)
     {
-      snprintf (msg, sizeof (msg), "%d: %s%s", getpid (), msg2,
+#if defined(__linux__)
+      id = (pid_t) syscall (__NR_gettid);
+#else
+      id = getpid ();
+#endif
+
+      time (&rawtime);
+      localtime_r (&rawtime, &now);
+      strftime (datetime, 32, "%b %e %H:%M:%S", &now);
+
+      snprintf (msg, sizeof (msg), "%s %d: %s%s", datetime, id, msg2,
 		msg2[strlen (msg2) - 1] == '\n' ? "" : "\n");
       if (dbgout)
 	{

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]