Bug 86396: added timestamps to PR_LOG. Timestamps are enabled by the
authorwtc%google.com
Thu, 07 May 2009 01:00:47 +0000
changeset 4100 5f4d53238493282ec3835ea9f4b229fc5ab4400e
parent 4099 cb4e53ac9dd002d75f2bf5d552d128374998e2ea
child 4101 f58b1fc316b1a1439ee50e4ff8b0680c98514339
push idunknown
push userunknown
push dateunknown
bugs86396
Bug 86396: added timestamps to PR_LOG. Timestamps are enabled by the special log module "__timestamp". We use calendar times in the local timezone. The patch is contributed by Hiroyuki Ikezoe <ikezoe@clear-code.com>. r=wtc. Modified Files: prlog.c prinit.c
pr/src/io/prlog.c
pr/src/misc/prinit.c
--- a/pr/src/io/prlog.c
+++ b/pr/src/io/prlog.c
@@ -139,16 +139,17 @@ static PRLogModuleInfo *logModules;
 static char *logBuf = NULL;
 static char *logp;
 static char *logEndp;
 #ifdef _PR_USE_STDIO_FOR_LOGGING
 static FILE *logFile = NULL;
 #else
 static PRFileDesc *logFile = 0;
 #endif
+static PRBool outputTimeStamp = PR_FALSE;
 
 #define LINE_BUF_SIZE           512
 #define DEFAULT_BUF_SIZE        16384
 
 #ifdef _PR_NEED_STRCASECMP
 
 /*
  * strcasecmp is defined in /usr/ucblib/libucb.a on some platforms
@@ -224,16 +225,18 @@ void _PR_InitLog(void)
             ** == 1, then level defaults to 1 (module enabled).
             */
             if (strcasecmp(module, "sync") == 0) {
                 isSync = PR_TRUE;
             } else if (strcasecmp(module, "bufsize") == 0) {
                 if (level >= LINE_BUF_SIZE) {
                     bufSize = level;
                 }
+            } else if (strcasecmp(module, "__timestamp") == 0) {
+                outputTimeStamp = PR_TRUE;
             } else {
                 PRLogModuleInfo *lm = logModules;
                 PRBool skip_modcheck =
                     (0 == strcasecmp (module, "all")) ? PR_TRUE : PR_FALSE;
 
                 while (lm != NULL) {
                     if (skip_modcheck) lm -> level = (PRLogModuleLevel)level;
                     else if (strcasecmp(module, lm->name) == 0) {
@@ -435,36 +438,38 @@ PR_IMPLEMENT(void) PR_SetLogBuffering(PR
     }
 }
 
 PR_IMPLEMENT(void) PR_LogPrint(const char *fmt, ...)
 {
     va_list ap;
     char line[LINE_BUF_SIZE];
     char *line_long = NULL;
-    PRUint32 nb_tid, nb;
+    PRUint32 nb_tid = 0, nb;
     PRThread *me;
+    PRExplodedTime now;
 
     if (!_pr_initialized) _PR_ImplicitInitialization();
 
     if (!logFile) {
         return;
     }
 
+    if (outputTimeStamp) {
+        PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &now);
+        nb_tid = PR_FormatTimeUSEnglish(line, sizeof(line)-1,
+                                        "%Y-%m-%d %H:%M:%S - ", &now);
+    }
+
     me = PR_GetCurrentThread();
-    nb_tid = PR_snprintf(line, sizeof(line)-1, "%ld[%p]: ",
-#if defined(_PR_DCETHREADS)
-             /* The problem is that for _PR_DCETHREADS, pthread_t is not a 
-              * pointer, but a structure; so you can't easily print it...
-              */
-                         me ? &(me->id): 0L, me);
-#elif defined(_PR_BTHREADS)
-                         me, me);
+    nb_tid += PR_snprintf(line+nb_tid, sizeof(line)-nb_tid-1, "%ld[%p]: ",
+#if defined(_PR_BTHREADS)
+                          me, me);
 #else
-                         me ? me->id : 0L, me);
+                          me ? me->id : 0L, me);
 #endif
 
     va_start(ap, fmt);
     nb = nb_tid + PR_vsnprintf(line+nb_tid, sizeof(line)-nb_tid-1, fmt, ap);
     va_end(ap);
 
     /*
      * Check if we might have run out of buffer space (in case we have a
@@ -479,17 +484,20 @@ PR_IMPLEMENT(void) PR_LogPrint(const cha
 
     if (line_long) {
         nb = strlen(line_long);
         _PR_LOCK_LOG();
         if (logBuf != 0) {
             _PUT_LOG(logFile, logBuf, logp - logBuf);
             logp = logBuf;
         }
-        /* Write out the thread id and the malloc'ed buffer. */
+        /*
+         * Write out the thread id (with an optional timestamp) and the
+         * malloc'ed buffer.
+         */
         _PUT_LOG(logFile, line, nb_tid);
         _PUT_LOG(logFile, line_long, nb);
         /* Ensure there is a trailing newline. */
         if (!nb || (line_long[nb-1] != '\n')) {
             char eol[2];
             eol[0] = '\n';
             eol[1] = '\0';
             _PUT_LOG(logFile, eol, 1);
--- a/pr/src/misc/prinit.c
+++ b/pr/src/misc/prinit.c
@@ -228,21 +228,21 @@ static void _PR_InitStuff(void)
  */
 #ifdef _PR_OVERRIDE_MALLOC
     _PR_InitMem();
 #endif
 
     _PR_InitCMon();
     _PR_InitIO();
     _PR_InitNet();
+    _PR_InitTime();
     _PR_InitLog();
     _PR_InitLinker();
     _PR_InitCallOnce();
     _PR_InitDtoa();
-    _PR_InitTime();
     _PR_InitMW();
     _PR_InitRWLocks();
 
     nspr_InitializePRErrorTable();
 
     _PR_MD_FINAL_INIT();
 }