Files
palemoon27/xpcom/base/Logging.cpp
T
roytam1 4d2f35fc4a import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1208554 - Take Velocity into account for WheelScrollAnimations. r=kats (2fe3c0e047)
- Bug 1207656: APZ mouse wheel scroll: don't ignore minimum duration. r=dvander (c195a2b7e2)
- Bug 1199468 - Create prefs for the smooth scroll timing function shape. r=kats (f65176f9b1)
- Bug 1247677 - Add support for the general.smoothScroll.{pages,pixels} prefs in APZ. r=dvander (e56a15b294)
- Bug 1244735 - Preference to override the default behaviour for partial present. r=jrmuizel (a0e19e8a8e)
- Bug 1223144 - Scale texture coordinates in CompositorOGL for non POT textures. r=mwoodrow (a4b654b00a)
- set reporter end (09ddff0c78)
- Add full mix-blend mode support to the D3D11 compositor. (bug 1238496, r=bas) (6ef3e68cbf)
- Bug 1235994 - For wheel events which may have momentum following them, handle scroll snapping in APZ. r=mstange (b9837185ea)
- Bug 1230674 - part 4, Update AsyncPanZoomController to call overscroll api when building for Android r=kats (60d551820d)
- Bug 1235994 - Extract a helper function to request a snap to the predicted destination. r=botond (c4436d3c5e)
- Bug 1243547 - Route scroll position adjusting for surface shifting directly to APZC. r=rbarker (3b86ab8f2f)
- Bug 1192919 - Follow-up to fix build error with logging enabled. r=me and DONTBUILD (f29229d6c4)
- Bug 1242173 - Typing in input fields does not visually update r= (a2ef65d735)
- Bug 1244901 - When recording transform duration for potential checkerboarding time, do so on the compositor thread. r=botond (cf0be4b328)
- Bug 1240202 - Avoid getting stuck in overscroll when a second finger goes down while overscrolled and in the TOUCHING state. r=kats (96c7ef87af)
- Bug 1236519 - Ensure JSFunction::isDerivedClassConstructor properly handles lazy self-hosted functions. r=till a=bustage (236b72dd50)
- Bug 1232229 - Add some instrumentation and more fencing to ArenaLists; r=ehoogeveen (f6595958c6)
- Bug 1232229 - Instrument setting ArenaHeader::next to catch misuse and fix existing instrumentation. r=terrence (68377d13b7)
- Bug 1248949 - Optimize Arena::thingsPerArena. r=terrence (ee28f3a6af)
- Bug 1245965 - Fix and OOM handling failure in NewMemInfoObject; r=sfink (29e684a163)
- Bug 1245518 - Implement ModuleEnvironmentObject::getOwnPropertyDescriptor() which is called by the debugger r=shu (cef4dc6fce)
- Bug 1232113 - "Make the format specifiers in JS_snprintf() invocations more portable". r=jwalden+bmo (cdf8a9915f)
- Bug 1234387 - DumpBacktrace: Check the result of Sprinter::init function. r=bbouvier (3b7f6aac37)
- Bug 1233944 - Upgrade MOZ_ASSERT to MOZ_RELEASE_ASSERT in JSObject::setGroup. r=me (75e7aa92dc)
- Bug 1203696: Make js::IsInternalFunctionObject consider non-lambdas internal. r=bhackett (1531e8ecd9)
- Bug 1235641 - Add line and column info to dump bytecode basic blocks. r=sstangl (0f642867a0)
- Bug 1052793, part 1 - Do per-zone GC for CC_WAITING triggers. r=smaug (b733898730)
- Bug 1052793, part 2 - Shrink GC buffers during compartmental GCs. r=terrence (0e4cd6218e)
- Bug 1237169 - Remove some unused parameters in and around xpcom/base/. r=mccr8. (9a08690fa7)
- Bug 1247122 - Propagate the NS_ERROR_OUT_OF_MEMORY from CycleCollectedJSRuntime CTOR, r=khuey (5e8bd1d14b)
- Bug 1200514 - Run remaining stable state runnables after final cycle collection. r=froydnj,mccr8 (410c3bf2ad)
- Bug 1247381 - Restore a CC optimization dropped in bug 1105069 part 7; r=mccr8 (c76e8613e7)
- Bug 1249932 - indicate in the GC and CC log (browser console) whetherit is about default or content process, r=mccr8 (1d89d18e69)
- Bug 1232113 - "Make the format specifiers in JS_snprintf() invocations more portable". r=jandem (d9a480c28c)
- Bug 1223222 - Part 1: Add NSPRLogModulesParser. r=froydnj (2a79976e1f)
- Bug 1223222 - Part 2: Add tests for NSPRLogModulesParser. r=froydnj (bde482099c)
- Bug 1233881 - Support dynamically setting log levels at runtime via prefs. r=froydnj (c5c685f924)
- Bug 1223222 - Part 3: Remove usage of PR_NewLogModule in mozilla LogModule code. r=froydnj (6eee38c6a7)
- Bug 1150917 - Remove all values from nsIProgrammingLanguage except UNKNOWN and JAVASCRIPT. r=froydnj (9626abcc33)
- Bug 1174972 - Add basic mozilla logging backend. r=froydnj,r=bwc,r=jduell (8e22fb5a66)
- Bug 961430 - Allow pid be added to the log file name automatically. r=froydnj (4d28e09d9a)
- Bug 1239212 - Append missing newlines to log messages. r=froydnj (0a94f048f0)
- Bug 1242097 - Support sync module for logging (r=erahm) (ce3648c763)
- Bug 885952 - Add thread name to MozLogging, r=froydnj (94bb1979e2)
- Bug 1239941 - Copy va_list before using it in LogModuleManager::Print. r=froydnj (0767e1900e)
- Bug 1249451 - nsCycleCollector::ScanRoots may cause some unexpected purple buffer usage, r=mccr8 (96591fa1a1)
- Bug 1237169 (follow-up) - Fix static analysis bustage. r=me. (3e177f98cc)
- Bug 1242151: using va_list needs <stdarg.h> at least on OpenBSD r=erahm (7c36e9191c)
- Bug 1226376 - Part 1: Allow non-trivial constexpr constructors in MOZ_TRIVIAL_CTOR_DTOR classes, r=ehsan (ec1fb5cc1d)
- Bug 1226376 - Part 2: Annotate StaticMutex with MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS, r=nfroyd (484904f38a)
- Bug 1223927 - Add resident-unique measurement to OS X. r=njn (4b97965c38)
- Bug 1224685 - Add |resident-unique| measurement to Windows. r=njn (bde0a2fbed)
- Bug 1239516 - Use nsDependentCSubstring for wrapping a slice of a char buffer. r=karlt (bd62aee33a)
- bug 1245745 include libpulse version in update URL r=glandium (74f389b1c7)
- Bug 1245673 - Remove useless NS_INT32_TO_PTR from DumpSerialNumbers. r=froydnj (16d58e9abd)
- Bug 1243949 - Fatally assert when we fail to create an XPCOM log. r=froydnj (9121c52fed)
- Remove the workaround added in bug 1022420 now that clang-cl understands __declspec(novtable) (fc81af6630)
- Bug 1239888 - Inline PLDHashTable::{Done,Get}(). r=froydnjx. (9faa5b7c2a)
- Bug 1244606 - Remove PL_DHASH_FASTCALL. r=froydnj. (6620b7c82e)
- Bug 1159306 - explicitly initialize nsBaseHashtableET::mData; r=erahm (458559bea7)
- Bug 1220544 - Fix -Wunreachable-code warnings in xpcom. r=mccr8 (371d0809e4)
- reapply Bug 833117 - Replace g_slice_set_config() with G_SLICE environmen variable (c47805a6cf)
- bug 1171574 - XPCOMGlue doesn't need LEADING_UNDERSCORE for iOS. r=froydnj (a4472ef3d3)
2023-11-30 09:46:13 +08:00

292 lines
7.6 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Logging.h"
#include <algorithm>
#include "mozilla/ClearOnShutdown.h"
#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 <process.h>
#else
#include <sys/types.h>
#include <unistd.h>
#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.
const uint32_t kInitialModuleCount = 256;
namespace mozilla {
namespace detail {
void log_print(const PRLogModuleInfo* aModule,
LogLevel aLevel,
const char* aFmt, ...)
{
va_list ap;
va_start(ap, aFmt);
char* buff = PR_vsmprintf(aFmt, ap);
PR_LogPrint("%s", buff);
PR_smprintf_free(buff);
va_end(ap);
}
void log_print(const LogModule* aModule,
LogLevel aLevel,
const char* aFmt, ...)
{
va_list ap;
va_start(ap, aFmt);
aModule->Printv(aLevel, aFmt, ap);
va_end(ap);
}
int log_pid()
{
#ifdef XP_WIN
return _getpid();
#else
return getpid();
#endif
}
}
LogLevel
ToLogLevel(int32_t aLevel)
{
aLevel = std::min(aLevel, static_cast<int32_t>(LogLevel::Verbose));
aLevel = std::max(aLevel, static_cast<int32_t>(LogLevel::Disabled));
return static_cast<LogLevel>(aLevel);
}
const char*
ToLogStr(LogLevel aLevel) {
switch (aLevel) {
case LogLevel::Error:
return "E";
case LogLevel::Warning:
return "W";
case LogLevel::Info:
return "I";
case LogLevel::Debug:
return "D";
case LogLevel::Verbose:
return "V";
case LogLevel::Disabled:
default:
MOZ_CRASH("Invalid log level.");
return "";
}
}
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
// 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, &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]) {
static const char kPIDToken[] = "%PID";
const char* pidTokenPtr = strstr(logFile, kPIDToken);
char buf[2048];
if (pidTokenPtr &&
snprintf_literal(buf, "%.*s%d%s",
static_cast<int>(pidTokenPtr - logFile), logFile,
detail::log_pid(),
pidTokenPtr + strlen(kPIDToken)) > 0)
{
logFile = buf;
}
mOutFile = fopen(logFile, shouldAppend ? "a" : "w");
}
}
LogModule* CreateOrGetModule(const char* aName)
{
OffTheBooksMutexAutoLock guard(mModulesLock);
LogModule* module = nullptr;
if (!mModules.Get(aName, &module)) {
module = new LogModule(aName, LogLevel::Disabled);
mModules.Put(aName, module);
}
return module;
}
void Print(const char* aName, LogLevel aLevel, const char* aFmt, va_list aArgs)
{
const size_t kBuffSize = 1024;
char buff[kBuffSize];
char* buffToWrite = buff;
// For backwards compat we need to use the NSPR format string versions
// of sprintf and friends and then hand off to printf.
va_list argsCopy;
va_copy(argsCopy, aArgs);
size_t charsWritten = PR_vsnprintf(buff, kBuffSize, aFmt, argsCopy);
va_end(argsCopy);
if (charsWritten == kBuffSize - 1) {
// We may have maxed out, allocate a buffer instead.
buffToWrite = PR_vsmprintf(aFmt, aArgs);
charsWritten = strlen(buffToWrite);
}
// Determine if a newline needs to be appended to the message.
const char* newline = "";
if (charsWritten == 0 || buffToWrite[charsWritten - 1] != '\n') {
newline = "\n";
}
FILE* out = mOutFile ? mOutFile : stderr;
// 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,
"[%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 - [%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,
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)
{
// This is just a pass through to the LogModuleManager so
// that the LogModuleManager implementation can be kept internal.
MOZ_ASSERT(sLogModuleManager != nullptr);
return sLogModuleManager->CreateOrGetModule(aName);
}
void
LogModule::Init()
{
// NB: This method is not threadsafe; it is expected to be called very early
// in startup prior to any other threads being run.
if (sLogModuleManager) {
// Already initialized.
return;
}
// NB: We intentionally do not register for ClearOnShutdown as that happens
// before all logging is complete. And, yes, that means we leak, but
// we're doing that intentionally.
sLogModuleManager = new LogModuleManager();
sLogModuleManager->Init();
}
void
LogModule::Printv(LogLevel aLevel, const char* aFmt, va_list aArgs) const
{
MOZ_ASSERT(sLogModuleManager != nullptr);
// Forward to LogModule manager w/ level and name
sLogModuleManager->Print(Name(), aLevel, aFmt, aArgs);
}
} // namespace mozilla