Bug 1060419 - make nsDebugImpl.cpp use PrintfTarget, r=froydnj

MozReview-Commit-ID: DyxdtYwmaVW

--HG--
extra : rebase_source : ef976daab148286c4c77c98bdd7c4e9c02b2ee43
This commit is contained in:
Tom Tromey 2016-12-13 16:05:54 -07:00
parent 6774f8026a
commit 2c32a85186

View File

@ -8,6 +8,7 @@
#include "base/process_util.h" #include "base/process_util.h"
#include "mozilla/Atomics.h" #include "mozilla/Atomics.h"
#include "mozilla/Printf.h"
#include "nsDebugImpl.h" #include "nsDebugImpl.h"
#include "nsDebug.h" #include "nsDebug.h"
@ -264,7 +265,7 @@ GetAssertBehavior()
return gAssertBehavior; return gAssertBehavior;
} }
struct FixedBuffer struct FixedBuffer : public mozilla::PrintfTarget
{ {
FixedBuffer() : curlen(0) FixedBuffer() : curlen(0)
{ {
@ -273,33 +274,33 @@ struct FixedBuffer
char buffer[500]; char buffer[500];
uint32_t curlen; uint32_t curlen;
bool append(const char* sp, size_t len);
}; };
static int bool
StuffFixedBuffer(void* aClosure, const char* aBuf, uint32_t aLen) FixedBuffer::append(const char* aBuf, size_t aLen)
{ {
if (!aLen) { if (!aLen) {
return 0; return true;
} }
FixedBuffer* fb = (FixedBuffer*)aClosure;
// strip the trailing null, we add it again later // strip the trailing null, we add it again later
if (aBuf[aLen - 1] == '\0') { if (aBuf[aLen - 1] == '\0') {
--aLen; --aLen;
} }
if (fb->curlen + aLen >= sizeof(fb->buffer)) { if (curlen + aLen >= sizeof(buffer)) {
aLen = sizeof(fb->buffer) - fb->curlen - 1; aLen = sizeof(buffer) - curlen - 1;
} }
if (aLen) { if (aLen) {
memcpy(fb->buffer + fb->curlen, aBuf, aLen); memcpy(buffer + curlen, aBuf, aLen);
fb->curlen += aLen; curlen += aLen;
fb->buffer[fb->curlen] = '\0'; buffer[curlen] = '\0';
} }
return aLen; return true;
} }
EXPORT_XPCOM_API(void) EXPORT_XPCOM_API(void)
@ -327,30 +328,26 @@ NS_DebugBreak(uint32_t aSeverity, const char* aStr, const char* aExpr,
aSeverity = NS_DEBUG_WARNING; aSeverity = NS_DEBUG_WARNING;
} }
#define PRINT_TO_NONPID_BUFFER(...) PR_sxprintf(StuffFixedBuffer, &nonPIDBuf, __VA_ARGS__) nonPIDBuf.print("%s: ", sevString);
PRINT_TO_NONPID_BUFFER("%s: ", sevString);
if (aStr) { if (aStr) {
PRINT_TO_NONPID_BUFFER("%s: ", aStr); nonPIDBuf.print("%s: ", aStr);
} }
if (aExpr) { if (aExpr) {
PRINT_TO_NONPID_BUFFER("'%s', ", aExpr); nonPIDBuf.print("'%s', ", aExpr);
} }
if (aFile) { if (aFile) {
PRINT_TO_NONPID_BUFFER("file %s, ", aFile); nonPIDBuf.print("file %s, ", aFile);
} }
if (aLine != -1) { if (aLine != -1) {
PRINT_TO_NONPID_BUFFER("line %d", aLine); nonPIDBuf.print("line %d", aLine);
} }
#undef PRINT_TO_NONPID_BUFFER
// Print "[PID]" or "[Desc PID]" at the beginning of the message. // Print "[PID]" or "[Desc PID]" at the beginning of the message.
#define PRINT_TO_BUFFER(...) PR_sxprintf(StuffFixedBuffer, &buf, __VA_ARGS__) buf.print("[");
PRINT_TO_BUFFER("[");
if (sMultiprocessDescription) { if (sMultiprocessDescription) {
PRINT_TO_BUFFER("%s ", sMultiprocessDescription); buf.print("%s ", sMultiprocessDescription);
} }
PRINT_TO_BUFFER("%d] %s", base::GetCurrentProcId(), nonPIDBuf.buffer); buf.print("%d] %s", base::GetCurrentProcId(), nonPIDBuf.buffer);
#undef PRINT_TO_BUFFER
// errors on platforms without a debugdlg ring a bell on stderr // errors on platforms without a debugdlg ring a bell on stderr