diff options
author | wtc%google.com <devnull@localhost> | 2007-09-03 15:41:09 +0000 |
---|---|---|
committer | wtc%google.com <devnull@localhost> | 2007-09-03 15:41:09 +0000 |
commit | a4c67ec31ec8f97695916a82526bdd20d4bb5f04 (patch) | |
tree | 5fe1072b6604e62bcb57d5b8f364a960318bb104 | |
parent | 7347a7daa7c5bdb2a833c020a1c05985e22fefe6 (diff) | |
download | nspr-hg-a4c67ec31ec8f97695916a82526bdd20d4bb5f04.tar.gz |
Bug 244478: allow a logging line to be longer than LINE_BUF_SIZE (512)
characters. The patch is contributed by Dan Witte <dwitte@stanford.edu>.
r=wtc.
-rw-r--r-- | pr/src/io/prlog.c | 81 |
1 files changed, 53 insertions, 28 deletions
diff --git a/pr/src/io/prlog.c b/pr/src/io/prlog.c index d6c6e6c5..935d2add 100644 --- a/pr/src/io/prlog.c +++ b/pr/src/io/prlog.c @@ -253,7 +253,7 @@ void _PR_InitLog(void) pos += delta; if (count == EOF) break; } - PR_SetLogBuffering(isSync ? bufSize : 0); + PR_SetLogBuffering(isSync ? 0 : bufSize); #ifdef XP_UNIX if ((getuid() != geteuid()) || (getgid() != getegid())) { @@ -442,7 +442,8 @@ PR_IMPLEMENT(void) PR_LogPrint(const char *fmt, ...) { va_list ap; char line[LINE_BUF_SIZE]; - PRUint32 nb; + char *line_long = NULL; + PRUint32 nb_tid, nb; PRThread *me; if (!_pr_initialized) _PR_ImplicitInitialization(); @@ -451,47 +452,71 @@ PR_IMPLEMENT(void) PR_LogPrint(const char *fmt, ...) return; } - va_start(ap, fmt); me = PR_GetCurrentThread(); - nb = PR_snprintf(line, sizeof(line)-1, "%ld[%p]: ", + 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); + me ? &(me->id): 0L, me); #elif defined(_PR_BTHREADS) - me, me); + me, me); #else - me ? me->id : 0L, me); + me ? me->id : 0L, me); #endif - nb += PR_vsnprintf(line+nb, sizeof(line)-nb-1, fmt, ap); - if (nb && (line[nb-1] != '\n')) { -#ifndef XP_MAC - line[nb++] = '\n'; -#else - line[nb++] = '\015'; -#endif - line[nb] = '\0'; - } else { -#ifdef XP_MAC - line[nb-1] = '\015'; -#endif - } + va_start(ap, fmt); + nb = nb_tid + PR_vsnprintf(line+nb_tid, sizeof(line)-nb_tid-1, fmt, ap); va_end(ap); - _PR_LOCK_LOG(); - if (logBuf == 0) { - _PUT_LOG(logFile, line, nb); - } else { - if (logp + nb > logEndp) { + /* + * Check if we might have run out of buffer space (in case we have a + * long line), and malloc a buffer just this once. + */ + if (nb == sizeof(line)-2) { + va_start(ap, fmt); + line_long = PR_vsmprintf(fmt, ap); + va_end(ap); + /* If this failed, we'll fall back to writing the truncated line. */ + } + + if (line_long) { + nb = strlen(line_long); + _PR_LOCK_LOG(); + if (logBuf != 0) { _PUT_LOG(logFile, logBuf, logp - logBuf); logp = logBuf; } - memcpy(logp, line, nb); - logp += nb; + /* Write out the thread id 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')) { + _PUT_LOG(logFile, "\n", 1); + } + _PR_UNLOCK_LOG(); + PR_smprintf_free(line_long); + } else { + /* Ensure there is a trailing newline. */ + if (nb && (line[nb-1] != '\n')) { + line[nb++] = '\n'; + line[nb] = '\0'; + } + _PR_LOCK_LOG(); + if (logBuf == 0) { + _PUT_LOG(logFile, line, nb); + } else { + /* If nb can't fit into logBuf, write out logBuf first. */ + if (logp + nb > logEndp) { + _PUT_LOG(logFile, logBuf, logp - logBuf); + logp = logBuf; + } + /* nb is guaranteed to fit into logBuf. */ + memcpy(logp, line, nb); + logp += nb; + } + _PR_UNLOCK_LOG(); } - _PR_UNLOCK_LOG(); PR_LogFlush(); } |