Bug 1060419 - make nsDebugImpl.cpp use PrintfTarget, r?froydnj draft
authorTom Tromey <tom@tromey.com>
Tue, 13 Dec 2016 16:05:54 -0700
changeset 486163 9cc13c9ac928768e4f14e6b29c939d2c1474591a
parent 486162 447c67ec0672a013b6c4079ce80c10ba03734b5c
child 486164 3de67b82e8f2cd0519e48d5504c558d9b4334c47
push id45909
push userbmo:ttromey@mozilla.com
push dateFri, 17 Feb 2017 16:00:11 +0000
reviewersfroydnj
bugs1060419
milestone54.0a1
Bug 1060419 - make nsDebugImpl.cpp use PrintfTarget, r?froydnj MozReview-Commit-ID: DyxdtYwmaVW
xpcom/base/nsDebugImpl.cpp
--- a/xpcom/base/nsDebugImpl.cpp
+++ b/xpcom/base/nsDebugImpl.cpp
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 // Chromium headers must come before Mozilla headers.
 #include "base/process_util.h"
 
 #include "mozilla/Atomics.h"
+#include "mozilla/Printf.h"
 
 #include "nsDebugImpl.h"
 #include "nsDebug.h"
 #ifdef MOZ_CRASHREPORTER
 # include "nsExceptionHandler.h"
 #endif
 #include "nsString.h"
 #include "nsXULAppAPI.h"
@@ -259,52 +260,52 @@ GetAssertBehavior()
   if (!strcmp(assertString, "stack-and-abort")) {
     return gAssertBehavior = NS_ASSERT_STACK_AND_ABORT;
   }
 
   fprintf(stderr, "Unrecognized value of XPCOM_DEBUG_BREAK\n");
   return gAssertBehavior;
 }
 
-struct FixedBuffer
+struct FixedBuffer : public mozilla::PrintfTarget
 {
   FixedBuffer() : curlen(0)
   {
     buffer[0] = '\0';
   }
 
   char buffer[500];
   uint32_t curlen;
+
+  bool append(const char* sp, size_t len);
 };
 
-static int
-StuffFixedBuffer(void* aClosure, const char* aBuf, uint32_t aLen)
+bool
+FixedBuffer::append(const char* aBuf, size_t aLen)
 {
   if (!aLen) {
-    return 0;
+    return true;
   }
 
-  FixedBuffer* fb = (FixedBuffer*)aClosure;
-
   // strip the trailing null, we add it again later
   if (aBuf[aLen - 1] == '\0') {
     --aLen;
   }
 
-  if (fb->curlen + aLen >= sizeof(fb->buffer)) {
-    aLen = sizeof(fb->buffer) - fb->curlen - 1;
+  if (curlen + aLen >= sizeof(buffer)) {
+    aLen = sizeof(buffer) - curlen - 1;
   }
 
   if (aLen) {
-    memcpy(fb->buffer + fb->curlen, aBuf, aLen);
-    fb->curlen += aLen;
-    fb->buffer[fb->curlen] = '\0';
+    memcpy(buffer + curlen, aBuf, aLen);
+    curlen += aLen;
+    buffer[curlen] = '\0';
   }
 
-  return aLen;
+  return true;
 }
 
 EXPORT_XPCOM_API(void)
 NS_DebugBreak(uint32_t aSeverity, const char* aStr, const char* aExpr,
               const char* aFile, int32_t aLine)
 {
   FixedBuffer nonPIDBuf;
   FixedBuffer buf;
@@ -322,40 +323,36 @@ NS_DebugBreak(uint32_t aSeverity, const 
     case NS_DEBUG_ABORT:
       sevString = "###!!! ABORT";
       break;
 
     default:
       aSeverity = NS_DEBUG_WARNING;
   }
 
-#define PRINT_TO_NONPID_BUFFER(...) PR_sxprintf(StuffFixedBuffer, &nonPIDBuf, __VA_ARGS__)
-  PRINT_TO_NONPID_BUFFER("%s: ", sevString);
+  nonPIDBuf.print("%s: ", sevString);
   if (aStr) {
-    PRINT_TO_NONPID_BUFFER("%s: ", aStr);
+    nonPIDBuf.print("%s: ", aStr);
   }
   if (aExpr) {
-    PRINT_TO_NONPID_BUFFER("'%s', ", aExpr);
+    nonPIDBuf.print("'%s', ", aExpr);
   }
   if (aFile) {
-    PRINT_TO_NONPID_BUFFER("file %s, ", aFile);
+    nonPIDBuf.print("file %s, ", aFile);
   }
   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.
-#define PRINT_TO_BUFFER(...) PR_sxprintf(StuffFixedBuffer, &buf, __VA_ARGS__)
-  PRINT_TO_BUFFER("[");
+  buf.print("[");
   if (sMultiprocessDescription) {
-    PRINT_TO_BUFFER("%s ", sMultiprocessDescription);
+    buf.print("%s ", sMultiprocessDescription);
   }
-  PRINT_TO_BUFFER("%d] %s", base::GetCurrentProcId(), nonPIDBuf.buffer);
-#undef PRINT_TO_BUFFER
+  buf.print("%d] %s", base::GetCurrentProcId(), nonPIDBuf.buffer);
 
 
   // errors on platforms without a debugdlg ring a bell on stderr
 #if !defined(XP_WIN)
   if (aSeverity != NS_DEBUG_WARNING) {
     fprintf(stderr, "\07");
   }
 #endif