Bug 885952 - Add thread name to MozLogging, r=froydnj
authorHonza Bambas <honzab.moz@firemni.cz>
Tue, 02 Feb 2016 07:21:00 +0100
changeset 282860 7289e167a227bba986b29e3329d11ae7bdf85846
parent 282859 39bf4d2518d00c4ddd71727c21e7a2b79e6ceb78
child 282861 ea7d83c65f5d36ca48f0c0d9765411876c0a1787
push id29972
push usercbook@mozilla.com
push dateThu, 04 Feb 2016 11:02:19 +0000
treeherdermozilla-central@f53533d9eb77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs885952
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 885952 - Add thread name to MozLogging, r=froydnj
xpcom/base/Logging.cpp
--- a/xpcom/base/Logging.cpp
+++ b/xpcom/base/Logging.cpp
@@ -81,16 +81,17 @@ ToLogStr(LogLevel aLevel) {
 
 class LogModuleManager
 {
 public:
   LogModuleManager()
     : mModulesLock("logmodules")
     , mModules(kInitialModuleCount)
     , mOutFile(nullptr)
+    , mMainThread(PR_GetCurrentThread())
     , mAddTimestamp(false)
     , mIsSync(false)
   {
   }
 
   ~LogModuleManager()
   {
     // NB: mModules owns all of the log modules, they will get destroyed by
@@ -171,46 +172,58 @@ public:
 
     // This differs from the NSPR format in that we do not output the
     // opaque system specific thread pointer (ie pthread_t) cast
     // to a long. The address of the current PR_Thread continues to be
     // prefixed.
     //
     // Additionally we prefix the output with the abbreviated log level
     // and the module name.
+    PRThread *currentThread = PR_GetCurrentThread();
+    const char *currentThreadName = (mMainThread == currentThread)
+      ? "Main Thread"
+      : PR_GetThreadName(currentThread);
+
+    char noNameThread[40];
+    if (!currentThreadName) {
+      snprintf_literal(noNameThread, "Unnamed thread %p", currentThread);
+      currentThreadName = noNameThread;
+    }
+
     if (!mAddTimestamp) {
       fprintf_stderr(out,
-                     "[%p]: %s/%s %s%s",
-                     PR_GetCurrentThread(), ToLogStr(aLevel),
+                     "[%s]: %s/%s %s%s",
+                     currentThreadName, ToLogStr(aLevel),
                      aName, buffToWrite, newline);
     } else {
       PRExplodedTime now;
       PR_ExplodeTime(PR_Now(), PR_GMTParameters, &now);
       fprintf_stderr(
           out,
-          "%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - [%p]: %s/%s %s%s",
+          "%04d-%02d-%02d %02d:%02d:%02d.%06d UTC - [%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,
-          PR_GetCurrentThread(), ToLogStr(aLevel),
+          currentThreadName, 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;
+  PRThread *mMainThread;
   bool mAddTimestamp;
   bool mIsSync;
 };
 
 StaticAutoPtr<LogModuleManager> sLogModuleManager;
 
 LogModule*
 LogModule::Get(const char* aName)