Bug 1060419 - make nsDebugImpl.cpp use PrintfTarget, r=froydnj
authorTom Tromey <tom@tromey.com>
Tue, 13 Dec 2016 16:05:54 -0700
changeset 372579 eff47106a91ec5ecf2122ddfa3dab690f4efba60
parent 372578 2e15220e1106db5066dd38402392170ad22053ed
child 372580 f595410db18f03cba967f94a87688cb155d25338
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
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