2015-12-23 02:09:41 +03:00
|
|
|
/* -*- 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 "LogModulePrefWatcher.h"
|
|
|
|
|
|
|
|
#include "mozilla/Logging.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2020-04-08 13:12:33 +03:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsIObserverService.h"
|
2024-05-23 11:14:16 +03:00
|
|
|
#include "NSPRLogModulesParser.h"
|
2015-12-23 02:09:41 +03:00
|
|
|
#include "nsString.h"
|
2016-09-19 18:39:47 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2024-05-23 11:14:17 +03:00
|
|
|
#include "prenv.h"
|
2016-09-19 18:39:47 +03:00
|
|
|
#include "base/process_util.h"
|
2015-12-23 02:09:41 +03:00
|
|
|
|
|
|
|
static const char kLoggingPrefPrefix[] = "logging.";
|
2016-09-19 18:39:47 +03:00
|
|
|
static const char kLoggingConfigPrefPrefix[] = "logging.config";
|
|
|
|
static const int kLoggingConfigPrefixLen = sizeof(kLoggingConfigPrefPrefix) - 1;
|
|
|
|
static const char kLoggingPrefClearOnStartup[] =
|
|
|
|
"logging.config.clear_on_startup";
|
|
|
|
static const char kLoggingPrefLogFile[] = "logging.config.LOG_FILE";
|
|
|
|
static const char kLoggingPrefAddTimestamp[] = "logging.config.add_timestamp";
|
|
|
|
static const char kLoggingPrefSync[] = "logging.config.sync";
|
2022-09-13 16:26:24 +03:00
|
|
|
static const char kLoggingPrefStacks[] = "logging.config.profilerstacks";
|
2024-05-23 11:14:16 +03:00
|
|
|
static const char kLoggingPrefLogModules[] = "logging.config.modules";
|
2015-12-23 02:09:41 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(LogModulePrefWatcher, nsIObserver)
|
|
|
|
|
2016-09-19 18:39:47 +03:00
|
|
|
/**
|
|
|
|
* Resets all the preferences in the logging. branch
|
|
|
|
* This is needed because we may crash while logging, and this would cause us
|
|
|
|
* to log after restarting as well.
|
|
|
|
*
|
|
|
|
* If logging after restart is desired, set the logging.config.clear_on_startup
|
|
|
|
* pref to false, or use the MOZ_LOG_FILE and MOZ_LOG_MODULES env vars.
|
|
|
|
*/
|
2019-02-25 04:35:59 +03:00
|
|
|
static void ResetExistingPrefs() {
|
2019-06-07 04:01:12 +03:00
|
|
|
nsTArray<nsCString> names;
|
|
|
|
nsresult rv =
|
|
|
|
Preferences::GetRootBranch()->GetChildList(kLoggingPrefPrefix, names);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
for (auto& name : names) {
|
2016-09-19 18:39:47 +03:00
|
|
|
// Clearing the pref will cause it to reload, thus resetting the log level
|
2019-06-07 04:01:12 +03:00
|
|
|
Preferences::ClearUser(name.get());
|
2016-09-19 18:39:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:09:41 +03:00
|
|
|
/**
|
|
|
|
* Loads the log level from the given pref and updates the corresponding
|
|
|
|
* LogModule.
|
|
|
|
*/
|
|
|
|
static void LoadPrefValue(const char* aName) {
|
|
|
|
LogLevel logLevel = LogLevel::Disabled;
|
|
|
|
|
2016-09-19 18:39:47 +03:00
|
|
|
nsresult rv;
|
2015-12-23 02:09:41 +03:00
|
|
|
int32_t prefLevel = 0;
|
2016-09-19 18:39:47 +03:00
|
|
|
nsAutoCString prefValue;
|
|
|
|
|
|
|
|
if (strncmp(aName, kLoggingConfigPrefPrefix, kLoggingConfigPrefixLen) == 0) {
|
|
|
|
nsAutoCString prefName(aName);
|
|
|
|
|
|
|
|
if (prefName.EqualsLiteral(kLoggingPrefLogFile)) {
|
2017-07-31 03:52:51 +03:00
|
|
|
rv = Preferences::GetCString(aName, prefValue);
|
2016-09-19 18:39:47 +03:00
|
|
|
// The pref was reset. Clear the user file.
|
|
|
|
if (NS_FAILED(rv) || prefValue.IsEmpty()) {
|
|
|
|
LogModule::SetLogFile(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the pref value doesn't have a PID placeholder, append it to the end.
|
2019-11-05 16:05:36 +03:00
|
|
|
if (!strstr(prefValue.get(), MOZ_LOG_PID_TOKEN)) {
|
|
|
|
prefValue.AppendLiteral(MOZ_LOG_PID_TOKEN);
|
2016-09-19 18:39:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LogModule::SetLogFile(prefValue.BeginReading());
|
|
|
|
} else if (prefName.EqualsLiteral(kLoggingPrefAddTimestamp)) {
|
|
|
|
bool addTimestamp = Preferences::GetBool(aName, false);
|
|
|
|
LogModule::SetAddTimestamp(addTimestamp);
|
|
|
|
} else if (prefName.EqualsLiteral(kLoggingPrefSync)) {
|
|
|
|
bool sync = Preferences::GetBool(aName, false);
|
|
|
|
LogModule::SetIsSync(sync);
|
2022-09-13 16:26:24 +03:00
|
|
|
} else if (prefName.EqualsLiteral(kLoggingPrefStacks)) {
|
|
|
|
bool captureStacks = Preferences::GetBool(aName, false);
|
|
|
|
LogModule::SetCaptureStacks(captureStacks);
|
2024-05-23 11:14:16 +03:00
|
|
|
} else if (prefName.EqualsLiteral(kLoggingPrefLogModules)) {
|
|
|
|
// The content of the preference will be parsed as a MOZ_LOG string, then
|
|
|
|
// the corresponding log modules (if any) will be enabled, others will be
|
|
|
|
// disabled.
|
|
|
|
LogModule::DisableModules();
|
2024-05-23 11:14:17 +03:00
|
|
|
LogModule::SetCaptureStacks(false);
|
|
|
|
|
|
|
|
const char* modulesFromEnv = PR_GetEnv("MOZ_LOG");
|
|
|
|
const bool hasModulesEnv = modulesFromEnv && modulesFromEnv[0];
|
2024-05-23 11:14:16 +03:00
|
|
|
|
|
|
|
rv = Preferences::GetCString(aName, prefValue);
|
2024-05-23 11:14:17 +03:00
|
|
|
const bool hasModulesPref = NS_SUCCEEDED(rv) && !prefValue.IsEmpty();
|
2024-05-23 11:14:16 +03:00
|
|
|
|
2024-05-23 11:14:17 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-09-19 18:39:47 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-23 02:09:41 +03:00
|
|
|
|
|
|
|
if (Preferences::GetInt(aName, &prefLevel) == NS_OK) {
|
|
|
|
logLevel = ToLogLevel(prefLevel);
|
2017-07-31 03:52:51 +03:00
|
|
|
} else if (Preferences::GetCString(aName, prefValue) == NS_OK) {
|
2016-09-19 18:39:47 +03:00
|
|
|
if (prefValue.LowerCaseEqualsLiteral("error")) {
|
2015-12-23 02:09:41 +03:00
|
|
|
logLevel = LogLevel::Error;
|
2016-09-19 18:39:47 +03:00
|
|
|
} else if (prefValue.LowerCaseEqualsLiteral("warning")) {
|
2015-12-23 02:09:41 +03:00
|
|
|
logLevel = LogLevel::Warning;
|
2016-09-19 18:39:47 +03:00
|
|
|
} else if (prefValue.LowerCaseEqualsLiteral("info")) {
|
2015-12-23 02:09:41 +03:00
|
|
|
logLevel = LogLevel::Info;
|
2016-09-19 18:39:47 +03:00
|
|
|
} else if (prefValue.LowerCaseEqualsLiteral("debug")) {
|
2015-12-23 02:09:41 +03:00
|
|
|
logLevel = LogLevel::Debug;
|
2016-09-19 18:39:47 +03:00
|
|
|
} else if (prefValue.LowerCaseEqualsLiteral("verbose")) {
|
2015-12-23 02:09:41 +03:00
|
|
|
logLevel = LogLevel::Verbose;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* moduleName = aName + strlen(kLoggingPrefPrefix);
|
|
|
|
LogModule::Get(moduleName)->SetLevel(logLevel);
|
|
|
|
}
|
|
|
|
|
2019-02-25 04:35:59 +03:00
|
|
|
static void LoadExistingPrefs() {
|
2016-08-10 22:48:55 +03:00
|
|
|
nsIPrefBranch* root = Preferences::GetRootBranch();
|
|
|
|
if (!root) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-07 04:01:12 +03:00
|
|
|
nsTArray<nsCString> names;
|
|
|
|
nsresult rv = root->GetChildList(kLoggingPrefPrefix, names);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
for (auto& name : names) {
|
|
|
|
LoadPrefValue(name.get());
|
2015-12-23 02:09:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 14:13:33 +03:00
|
|
|
LogModulePrefWatcher::LogModulePrefWatcher() = default;
|
2015-12-23 02:09:41 +03:00
|
|
|
|
|
|
|
void LogModulePrefWatcher::RegisterPrefWatcher() {
|
|
|
|
RefPtr<LogModulePrefWatcher> prefWatcher = new LogModulePrefWatcher();
|
|
|
|
Preferences::AddStrongObserver(prefWatcher, kLoggingPrefPrefix);
|
2016-09-19 18:39:47 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService && XRE_IsParentProcess()) {
|
|
|
|
observerService->AddObserver(prefWatcher,
|
|
|
|
"browser-delayed-startup-finished", false);
|
|
|
|
}
|
|
|
|
|
2015-12-23 02:09:41 +03:00
|
|
|
LoadExistingPrefs();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LogModulePrefWatcher::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
if (strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic) == 0) {
|
|
|
|
NS_LossyConvertUTF16toASCII prefName(aData);
|
|
|
|
LoadPrefValue(prefName.get());
|
2016-09-19 18:39:47 +03:00
|
|
|
} else if (strcmp("browser-delayed-startup-finished", aTopic) == 0) {
|
|
|
|
bool clear = Preferences::GetBool(kLoggingPrefClearOnStartup, true);
|
|
|
|
if (clear) {
|
|
|
|
ResetExistingPrefs();
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->RemoveObserver(this, "browser-delayed-startup-finished");
|
|
|
|
}
|
2015-12-23 02:09:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|