diff --git a/xpcom/base/Logging.cpp b/xpcom/base/Logging.cpp index 2223f1b4bfbb..2d44d152ee02 100644 --- a/xpcom/base/Logging.cpp +++ b/xpcom/base/Logging.cpp @@ -12,12 +12,19 @@ #include "mozilla/FileUtils.h" #include "mozilla/Mutex.h" #include "mozilla/StaticPtr.h" +#include "mozilla/Snprintf.h" #include "nsClassHashtable.h" #include "nsDebug.h" #include "NSPRLogModulesParser.h" #include "prenv.h" #include "prprf.h" +#ifdef XP_WIN +#include +#else +#include +#include +#endif // NB: Initial amount determined by auditing the codebase for the total amount // of unique module names and padding up to the next power of 2. @@ -49,6 +56,15 @@ void log_print(const LogModule* aModule, va_end(ap); } +int log_pid() +{ +#ifdef XP_WIN + return _getpid(); +#else + return getpid(); +#endif +} + } LogLevel @@ -126,6 +142,18 @@ public: const char* logFile = PR_GetEnv("NSPR_LOG_FILE"); if (logFile && logFile[0]) { + static const char kPIDToken[] = "%PID"; + const char* pidTokenPtr = strstr(logFile, kPIDToken); + char buf[2048]; + if (pidTokenPtr && + snprintf_literal(buf, "%.*s%d%s", + static_cast(pidTokenPtr - logFile), logFile, + detail::log_pid(), + pidTokenPtr + strlen(kPIDToken)) > 0) + { + logFile = buf; + } + mOutFile = fopen(logFile, shouldAppend ? "a" : "w"); } }