Bug 1067547 - unify ScopedLogging implementations and use in nsEmbedFunctions; r?bsmedberg draft
authorTom Tromey <tom@tromey.com>
Mon, 26 Sep 2016 11:06:38 -0600
changeset 420236 21c58a602f93b5b2125b72aac8ce00f2c6eac381
parent 420235 9c88092b161f82ec18ff0b5a6eb7a726419b5650
child 532759 3d8f623ae43497cae05f506e091dd66d6043aebd
push id31139
push userbmo:ttromey@mozilla.com
push dateMon, 03 Oct 2016 19:19:02 +0000
reviewersbsmedberg
bugs1067547
milestone52.0a1
Bug 1067547 - unify ScopedLogging implementations and use in nsEmbedFunctions; r?bsmedberg MozReview-Commit-ID: 6hDZDaBsNFM
b2g/app/nsBrowserApp.cpp
toolkit/xre/nsAppRunner.cpp
toolkit/xre/nsEmbedFunctions.cpp
tools/fuzzing/libfuzzer/harness/LibFuzzerTestHarness.h
xpcom/build/nsXPCOM.h
xpcom/tests/TestHarness.h
--- a/b2g/app/nsBrowserApp.cpp
+++ b/b2g/app/nsBrowserApp.cpp
@@ -75,26 +75,16 @@ static bool IsArg(const char* arg, const
 #if defined(XP_WIN)
   if (*arg == '/')
     return !strcasecmp(++arg, s);
 #endif
 
   return false;
 }
 
-/**
- * A helper class which calls NS_LogInit/NS_LogTerm in its scope.
- */
-class ScopedLogging
-{
-public:
-  ScopedLogging() { NS_LogInit(); }
-  ~ScopedLogging() { NS_LogTerm(); }
-};
-
 XRE_GetFileFromPathType XRE_GetFileFromPath;
 XRE_CreateAppDataType XRE_CreateAppData;
 XRE_FreeAppDataType XRE_FreeAppData;
 XRE_TelemetryAccumulateType XRE_TelemetryAccumulate;
 XRE_mainType XRE_main;
 
 static const nsDynamicFunctionLoad kXULFuncs[] = {
     { "XRE_GetFileFromPath", (NSFuncPtr*) &XRE_GetFileFromPath },
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -1552,26 +1552,16 @@ ScopedXPCOMStartup::CreateAppSupport(nsI
   if (!gNativeAppSupport)
     return NS_ERROR_NOT_INITIALIZED;
 
   return gNativeAppSupport->QueryInterface(aIID, aResult);
 }
 
 nsINativeAppSupport* ScopedXPCOMStartup::gNativeAppSupport;
 
-/**
- * A helper class which calls NS_LogInit/NS_LogTerm in its scope.
- */
-class ScopedLogging
-{
-public:
-  ScopedLogging() { NS_LogInit(); }
-  ~ScopedLogging() { NS_LogTerm(); }
-};
-
 static void DumpArbitraryHelp()
 {
   nsresult rv;
 
   ScopedLogging log;
 
   {
     ScopedXPCOMStartup xpcom;
--- a/toolkit/xre/nsEmbedFunctions.cpp
+++ b/toolkit/xre/nsEmbedFunctions.cpp
@@ -362,30 +362,30 @@ XRE_InitChildProcess(int aArgc,
 #if defined(MOZ_SANDBOX)
   if (aChildData->sandboxTargetServices) {
     SandboxTarget::Instance()->SetTargetServices(aChildData->sandboxTargetServices);
   }
 #endif
 #endif
 
   // NB: This must be called before profiler_init
-  NS_LogInit();
+  ScopedLogging logger;
 
   // This is needed by Telemetry to initialize histogram collection.
   // NB: This must be called after NS_LogInit().
   // NS_LogInit must be called before Telemetry::CreateStatisticsRecorder
   // so as to avoid many log messages of the form
   //   WARNING: XPCOM objects created/destroyed from static ctor/dtor: [..]
   // See bug 1279614.
   Telemetry::CreateStatisticsRecorder();
 
   mozilla::LogModule::Init();
 
   char aLocal;
-  profiler_init(&aLocal);
+  GeckoProfilerInitRAII profiler(&aLocal);
 
   PROFILER_LABEL("Startup", "XRE_InitChildProcess",
     js::ProfileEntry::Category::OTHER);
 
   // Complete 'task_t' exchange for Mac OS X. This structure has the same size
   // regardless of architecture so we don't have any cross-arch issues here.
 #ifdef XP_MACOSX
   if (aArgc < 1)
@@ -554,18 +554,16 @@ XRE_InitChildProcess(int aArgc,
     }
   }
 #endif
 
   base::AtExitManager exitManager;
 
   nsresult rv = XRE_InitCommandLine(aArgc, aArgv);
   if (NS_FAILED(rv)) {
-    profiler_shutdown();
-    NS_LogTerm();
     return NS_ERROR_FAILURE;
   }
 
   MessageLoop::Type uiLoopType;
   switch (XRE_GetProcessType()) {
   case GeckoProcessType_Content:
   case GeckoProcessType_GPU:
       // Content processes need the XPCOM/chromium frankenventloop
@@ -664,18 +662,16 @@ XRE_InitChildProcess(int aArgc,
         process = new gfx::GPUProcessImpl(parentPID);
         break;
 
       default:
         NS_RUNTIMEABORT("Unknown main thread class");
       }
 
       if (!process->Init()) {
-        profiler_shutdown();
-        NS_LogTerm();
         return NS_ERROR_FAILURE;
       }
 
 #ifdef MOZ_CRASHREPORTER
 #if defined(XP_WIN) || defined(XP_MACOSX)
       CrashReporter::InitChildProcessTmpDir();
 #endif
 #endif
@@ -712,18 +708,16 @@ XRE_InitChildProcess(int aArgc,
 #if defined(XP_MACOSX)
       // Everybody should be done using shared memory by now.
       mozilla::ipc::SharedMemoryBasic::Shutdown();
 #endif
     }
   }
 
   Telemetry::DestroyStatisticsRecorder();
-  profiler_shutdown();
-  NS_LogTerm();
   return XRE_DeinitCommandLine();
 }
 
 MessageLoop*
 XRE_GetIOMessageLoop()
 {
   if (sChildProcessType == GeckoProcessType_Default) {
     return BrowserProcessSubThread::GetMessageLoop(BrowserProcessSubThread::IO);
--- a/tools/fuzzing/libfuzzer/harness/LibFuzzerTestHarness.h
+++ b/tools/fuzzing/libfuzzer/harness/LibFuzzerTestHarness.h
@@ -70,30 +70,16 @@ void passed(const char* msg, ...)
   vprintf(msg, ap);
   va_end(ap);
 
   putchar('\n');
 }
 
 //-----------------------------------------------------------------------------
 
-class ScopedLogging
-{
-public:
-    ScopedLogging()
-    {
-        NS_LogInit();
-    }
-
-    ~ScopedLogging()
-    {
-        NS_LogTerm();
-    }
-};
-
 static class ScopedXPCOM : public nsIDirectoryServiceProvider2
 {
   public:
     NS_DECL_ISUPPORTS
 
     explicit ScopedXPCOM(const char* testName,
                          nsIDirectoryServiceProvider *dirSvcProvider = nullptr)
     : mDirSvcProvider(dirSvcProvider)
--- a/xpcom/build/nsXPCOM.h
+++ b/xpcom/build/nsXPCOM.h
@@ -263,16 +263,37 @@ XPCOM_API(void) NS_DebugBreak(uint32_t a
  * NS_LogTerm allow applications to enable logging earlier and delay
  * printing of logging statistics. They should always be used as a
  * matched pair.
  */
 XPCOM_API(void) NS_LogInit();
 
 XPCOM_API(void) NS_LogTerm();
 
+#ifdef __cplusplus
+/**
+ * A helper class that calls NS_LogInit in its constructor and
+ * NS_LogTerm in its destructor.
+ */
+
+class ScopedLogging
+{
+public:
+    ScopedLogging()
+    {
+        NS_LogInit();
+    }
+
+    ~ScopedLogging()
+    {
+        NS_LogTerm();
+    }
+};
+#endif
+
 /**
  * Log construction and destruction of objects. Processing tools can use the
  * stacktraces printed by these functions to identify objects that are being
  * leaked.
  *
  * @param aPtr          A pointer to the concrete object.
  * @param aTypeName     The class name of the type
  * @param aInstanceSize The size of the type
--- a/xpcom/tests/TestHarness.h
+++ b/xpcom/tests/TestHarness.h
@@ -68,30 +68,16 @@ void passed(const char* msg, ...)
   vprintf(msg, ap);
   va_end(ap);
 
   putchar('\n');
 }
 
 //-----------------------------------------------------------------------------
 
-class ScopedLogging
-{
-public:
-    ScopedLogging()
-    {
-        NS_LogInit();
-    }
-
-    ~ScopedLogging()
-    {
-        NS_LogTerm();
-    }
-};
-
 class ScopedXPCOM : public nsIDirectoryServiceProvider2
 {
   public:
     NS_DECL_ISUPPORTS
 
     explicit ScopedXPCOM(const char* testName,
                          nsIDirectoryServiceProvider *dirSvcProvider = nullptr)
     : mDirSvcProvider(dirSvcProvider)