Bug 1894703 - When the modules preference is removed or absent, use the MOZ_LOG env variable if present r=padenot

The previous patchset got backed out. Indeed it broke MOZ_LOG using the
environment variable. The reason is that the preference watcher was
called with an empty value after the log modules were set by the
enviroment variable handler at init time.

To fix this, now we read the environment variable as well when the pref
is handled. If the pref is empty but the environment variable is set,
the environment variable is used.

This also fixes an issue where profiler stacks wouldn't be disabled.

Differential Revision: https://phabricator.services.mozilla.com/D210785
This commit is contained in:
Julien Wajsberg 2024-05-23 08:14:17 +00:00
parent 16d63d259d
commit bafb803cd9

View file

@ -13,6 +13,7 @@
#include "NSPRLogModulesParser.h"
#include "nsString.h"
#include "nsXULAppAPI.h"
#include "prenv.h"
#include "base/process_util.h"
static const char kLoggingPrefPrefix[] = "logging.";
@ -92,27 +93,29 @@ static void LoadPrefValue(const char* aName) {
// the corresponding log modules (if any) will be enabled, others will be
// disabled.
LogModule::DisableModules();
LogModule::SetCaptureStacks(false);
const char* modulesFromEnv = PR_GetEnv("MOZ_LOG");
const bool hasModulesEnv = modulesFromEnv && modulesFromEnv[0];
rv = Preferences::GetCString(aName, prefValue);
if (NS_FAILED(rv)) {
// If the preference is missing, there's nothing to set.
return;
}
const bool hasModulesPref = NS_SUCCEEDED(rv) && !prefValue.IsEmpty();
NSPRLogModulesParser(
prefValue.BeginReading(),
[](const char* aName, LogLevel aLevel, int32_t aValue) mutable {
// Only the special string "profilerstacks" is taken into account,
// because we're especially interested in usage with the Firefox
// Profiler.
if (strcmp(aName, "profilerstacks") == 0) {
LogModule::SetCaptureStacks(true);
} else {
LogModule::Get(aName)->SetLevel(aLevel);
}
});
if (hasModulesEnv || hasModulesPref) {
NSPRLogModulesParser(
hasModulesPref ? prefValue.BeginReading() : modulesFromEnv,
[](const char* aName, LogLevel aLevel, int32_t aValue) mutable {
// Only the special string "profilerstacks" is taken into account,
// because we're especially interested in usage with the Firefox
// Profiler.
if (strcmp(aName, "profilerstacks") == 0) {
LogModule::SetCaptureStacks(true);
} else {
LogModule::Get(aName)->SetLevel(aLevel);
}
});
}
}
return;
}
if (Preferences::GetInt(aName, &prefLevel) == NS_OK) {