Bug 1447622 - Add a way to output raw MOZ_LOG, without prefixes. r?erahm draft
authorPaul Adenot <paul@paul.cx>
Wed, 21 Mar 2018 12:11:17 +0100
changeset 770492 1826d8084197404261fd0acfe271723c1d42cd56
parent 770066 8930776bdae783e491d60fed3bef767827e067cc
push id103416
push userpaul@paul.cx
push dateWed, 21 Mar 2018 12:23:36 +0000
reviewerserahm
bugs1447622
milestone60.0a1
Bug 1447622 - Add a way to output raw MOZ_LOG, without prefixes. r?erahm MOZ_LOG=modules:4,raw now outputs the log without prefixes with the thread name and date and stuff, just the exact string that was specified. MozReview-Commit-ID: HACT5EM4BFm
xpcom/base/Logging.cpp
--- a/xpcom/base/Logging.cpp
+++ b/xpcom/base/Logging.cpp
@@ -173,16 +173,17 @@ public:
     , mPrintEntryCount(0)
     , mOutFile(nullptr)
     , mToReleaseFile(nullptr)
     , mOutFileNum(0)
     , mOutFilePath(strdup(""))
     , mMainThread(PR_GetCurrentThread())
     , mSetFromEnv(false)
     , mAddTimestamp(false)
+    , mIsRaw(false)
     , mIsSync(false)
     , mRotate(0)
     , mInitialized(false)
   {
   }
 
   ~LogModuleManager()
   {
@@ -201,16 +202,17 @@ public:
   void Init()
   {
     MOZ_DIAGNOSTIC_ASSERT(!mInitialized);
     mInitialized = true;
 
     bool shouldAppend = false;
     bool addTimestamp = false;
     bool isSync = false;
+    bool isRaw = false;
     int32_t rotate = 0;
     const char* modules = PR_GetEnv("MOZ_LOG");
     if (!modules || !modules[0]) {
       modules = PR_GetEnv("MOZ_LOG_MODULES");
       if (modules) {
         NS_WARNING("MOZ_LOG_MODULES is deprecated."
             "\nPlease use MOZ_LOG instead.");
       }
@@ -221,34 +223,37 @@ public:
         NS_WARNING("NSPR_LOG_MODULES is deprecated."
             "\nPlease use MOZ_LOG instead.");
       }
     }
 
     // Need to capture `this` since `sLogModuleManager` is not set until after
     // initialization is complete.
     NSPRLogModulesParser(modules,
-        [this, &shouldAppend, &addTimestamp, &isSync, &rotate]
+        [this, &shouldAppend, &addTimestamp, &isSync, &isRaw, &rotate]
             (const char* aName, LogLevel aLevel, int32_t aValue) 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 if (strcmp(aName, "raw") == 0) {
+            isRaw = true;
           } else if (strcmp(aName, "rotate") == 0) {
             rotate = (aValue << 20) / kRotateFilesNumber;
           } else {
             this->CreateOrGetModule(aName)->SetLevel(aLevel);
           }
     });
 
     // Rotate implies timestamp to make the files readable
     mAddTimestamp = addTimestamp || rotate > 0;
     mIsSync = isSync;
+    mIsRaw = isRaw;
     mRotate = rotate;
 
     if (rotate > 0 && shouldAppend) {
       NS_WARNING("MOZ_LOG: when you rotate the log, you cannot use append!");
     }
 
     const char* logFile = PR_GetEnv("MOZ_LOG_FILE");
     if (!logFile || !logFile[0]) {
@@ -434,20 +439,24 @@ public:
 
     char noNameThread[40];
     if (!currentThreadName) {
       SprintfLiteral(noNameThread, "Unnamed thread %p", currentThread);
       currentThreadName = noNameThread;
     }
 
     if (!mAddTimestamp) {
-      fprintf_stderr(out,
-                     "[%ld:%s]: %s/%s %s%s",
-                     pid, currentThreadName, ToLogStr(aLevel),
-                     aName, buffToWrite, newline);
+      if (!mIsRaw) {
+        fprintf_stderr(out,
+                      "[%ld:%s]: %s/%s %s%s",
+                      pid, currentThreadName, ToLogStr(aLevel),
+                      aName, buffToWrite, newline);
+      } else {
+        fprintf_stderr(out, "%s%s", buffToWrite, newline);
+      }
     } else {
       PRExplodedTime now;
       PR_ExplodeTime(PR_Now(), PR_GMTParameters, &now);
       fprintf_stderr(
           out,
           "%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - [%ld:%s]: %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,
@@ -518,16 +527,17 @@ private:
   // is atomic regardless ordering.
   Atomic<uint32_t, Relaxed> mOutFileNum;
   // Just keeps the actual file path for further use.
   UniqueFreePtr<char[]> mOutFilePath;
 
   PRThread *mMainThread;
   bool mSetFromEnv;
   Atomic<bool, Relaxed> mAddTimestamp;
+  Atomic<bool, Relaxed> mIsRaw;
   Atomic<bool, Relaxed> mIsSync;
   int32_t mRotate;
   bool mInitialized;
 };
 
 StaticAutoPtr<LogModuleManager> sLogModuleManager;
 
 LogModule*