Bug 1242097 - Support sync module for logging (r=erahm)
authorBill McCloskey <billm@mozilla.com>
Fri, 22 Jan 2016 17:21:31 -0800
changeset 281998 57e9eb4ed010fc4a909023de9c86f2af731d4b4a
parent 281997 4ce037f2107dc1f39fe0c7b04ebd8b22f3d3d7a2
child 281999 3274c88c07a24be53588e6ae97f62d8949d8ef50
push id29950
push usercbook@mozilla.com
push dateThu, 28 Jan 2016 11:14:03 +0000
treeherdermozilla-central@2b73b0a4d52b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1242097
milestone47.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1242097 - Support sync module for logging (r=erahm)
xpcom/base/Logging.cpp
--- a/xpcom/base/Logging.cpp
+++ b/xpcom/base/Logging.cpp
@@ -82,46 +82,51 @@ ToLogStr(LogLevel aLevel) {
 class LogModuleManager
 {
 public:
   LogModuleManager()
     : mModulesLock("logmodules")
     , mModules(kInitialModuleCount)
     , mOutFile(nullptr)
     , mAddTimestamp(false)
+    , mIsSync(false)
   {
   }
 
   ~LogModuleManager()
   {
     // NB: mModules owns all of the log modules, they will get destroyed by
     //     its destructor.
   }
 
   /**
    * Loads config from env vars if present.
    */
   void Init()
   {
     bool shouldAppend = false;
     bool addTimestamp = false;
+    bool isSync = false;
     const char* modules = PR_GetEnv("NSPR_LOG_MODULES");
     NSPRLogModulesParser(modules,
-        [&shouldAppend, &addTimestamp]
+        [&shouldAppend, &addTimestamp, &isSync]
             (const char* aName, LogLevel aLevel) mutable {
           if (strcmp(aName, "append") == 0) {
             shouldAppend = true;
           } else if (strcmp(aName, "timestamp") == 0) {
             addTimestamp = true;
+          } else if (strcmp(aName, "sync") == 0) {
+            isSync = true;
           } else {
             LogModule::Get(aName)->SetLevel(aLevel);
           }
     });
 
     mAddTimestamp = addTimestamp;
+    mIsSync = isSync;
 
     const char* logFile = PR_GetEnv("NSPR_LOG_FILE");
     if (logFile && logFile[0]) {
       mOutFile = fopen(logFile, shouldAppend ? "a" : "w");
     }
   }
 
   LogModule* CreateOrGetModule(const char* aName)
@@ -183,26 +188,31 @@ public:
           out,
           "%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - [%p]: %s/%s %s%s",
           now.tm_year, now.tm_month + 1, now.tm_mday,
           now.tm_hour, now.tm_min, now.tm_sec, now.tm_usec,
           PR_GetCurrentThread(), ToLogStr(aLevel),
           aName, buffToWrite, newline);
     }
 
+    if (mIsSync) {
+      fflush(out);
+    }
+
     if (buffToWrite != buff) {
       PR_smprintf_free(buffToWrite);
     }
   }
 
 private:
   OffTheBooksMutex mModulesLock;
   nsClassHashtable<nsCharPtrHashKey, LogModule> mModules;
   ScopedCloseFile mOutFile;
   bool mAddTimestamp;
+  bool mIsSync;
 };
 
 StaticAutoPtr<LogModuleManager> sLogModuleManager;
 
 LogModule*
 LogModule::Get(const char* aName)
 {
   // This is just a pass through to the LogModuleManager so