1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "LogModulePrefWatcher.h"
9 #include "mozilla/Logging.h"
10 #include "mozilla/Preferences.h"
11 #include "mozilla/Services.h"
12 #include "nsIObserverService.h"
13 #include "NSPRLogModulesParser.h"
15 #include "nsXULAppAPI.h"
17 #include "base/process_util.h"
19 static const char kLoggingPrefPrefix
[] = "logging.";
20 static const char kLoggingConfigPrefPrefix
[] = "logging.config";
21 static const int kLoggingConfigPrefixLen
= sizeof(kLoggingConfigPrefPrefix
) - 1;
22 static const char kLoggingPrefClearOnStartup
[] =
23 "logging.config.clear_on_startup";
24 static const char kLoggingPrefLogFile
[] = "logging.config.LOG_FILE";
25 static const char kLoggingPrefAddTimestamp
[] = "logging.config.add_timestamp";
26 static const char kLoggingPrefSync
[] = "logging.config.sync";
27 static const char kLoggingPrefStacks
[] = "logging.config.profilerstacks";
28 static const char kLoggingPrefLogModules
[] = "logging.config.modules";
32 NS_IMPL_ISUPPORTS(LogModulePrefWatcher
, nsIObserver
)
35 * Resets all the preferences in the logging. branch
36 * This is needed because we may crash while logging, and this would cause us
37 * to log after restarting as well.
39 * If logging after restart is desired, set the logging.config.clear_on_startup
40 * pref to false, or use the MOZ_LOG_FILE and MOZ_LOG_MODULES env vars.
42 static void ResetExistingPrefs() {
43 nsTArray
<nsCString
> names
;
45 Preferences::GetRootBranch()->GetChildList(kLoggingPrefPrefix
, names
);
46 if (NS_SUCCEEDED(rv
)) {
47 for (auto& name
: names
) {
48 // Clearing the pref will cause it to reload, thus resetting the log level
49 Preferences::ClearUser(name
.get());
55 * Loads the log level from the given pref and updates the corresponding
58 static void LoadPrefValue(const char* aName
) {
59 LogLevel logLevel
= LogLevel::Disabled
;
62 int32_t prefLevel
= 0;
63 nsAutoCString prefValue
;
65 if (strncmp(aName
, kLoggingConfigPrefPrefix
, kLoggingConfigPrefixLen
) == 0) {
66 nsAutoCString
prefName(aName
);
68 if (prefName
.EqualsLiteral(kLoggingPrefLogFile
)) {
69 rv
= Preferences::GetCString(aName
, prefValue
);
70 // The pref was reset. Clear the user file.
71 if (NS_FAILED(rv
) || prefValue
.IsEmpty()) {
72 LogModule::SetLogFile(nullptr);
76 // If the pref value doesn't have a PID placeholder, append it to the end.
77 if (!strstr(prefValue
.get(), MOZ_LOG_PID_TOKEN
)) {
78 prefValue
.AppendLiteral(MOZ_LOG_PID_TOKEN
);
81 LogModule::SetLogFile(prefValue
.BeginReading());
82 } else if (prefName
.EqualsLiteral(kLoggingPrefAddTimestamp
)) {
83 bool addTimestamp
= Preferences::GetBool(aName
, false);
84 LogModule::SetAddTimestamp(addTimestamp
);
85 } else if (prefName
.EqualsLiteral(kLoggingPrefSync
)) {
86 bool sync
= Preferences::GetBool(aName
, false);
87 LogModule::SetIsSync(sync
);
88 } else if (prefName
.EqualsLiteral(kLoggingPrefStacks
)) {
89 bool captureStacks
= Preferences::GetBool(aName
, false);
90 LogModule::SetCaptureStacks(captureStacks
);
91 } else if (prefName
.EqualsLiteral(kLoggingPrefLogModules
)) {
92 // The content of the preference will be parsed as a MOZ_LOG string, then
93 // the corresponding log modules (if any) will be enabled, others will be
95 LogModule::DisableModules();
96 LogModule::SetCaptureStacks(false);
98 const char* modulesFromEnv
= PR_GetEnv("MOZ_LOG");
99 const bool hasModulesEnv
= modulesFromEnv
&& modulesFromEnv
[0];
101 rv
= Preferences::GetCString(aName
, prefValue
);
102 const bool hasModulesPref
= NS_SUCCEEDED(rv
) && !prefValue
.IsEmpty();
104 if (hasModulesEnv
|| hasModulesPref
) {
105 NSPRLogModulesParser(
106 hasModulesPref
? prefValue
.BeginReading() : modulesFromEnv
,
107 [](const char* aName
, LogLevel aLevel
, int32_t aValue
) mutable {
108 // Only the special string "profilerstacks" is taken into account,
109 // because we're especially interested in usage with the Firefox
111 if (strcmp(aName
, "profilerstacks") == 0) {
112 LogModule::SetCaptureStacks(true);
114 LogModule::Get(aName
)->SetLevel(aLevel
);
121 if (Preferences::GetInt(aName
, &prefLevel
) == NS_OK
) {
122 logLevel
= ToLogLevel(prefLevel
);
123 } else if (Preferences::GetCString(aName
, prefValue
) == NS_OK
) {
124 if (prefValue
.LowerCaseEqualsLiteral("error")) {
125 logLevel
= LogLevel::Error
;
126 } else if (prefValue
.LowerCaseEqualsLiteral("warning")) {
127 logLevel
= LogLevel::Warning
;
128 } else if (prefValue
.LowerCaseEqualsLiteral("info")) {
129 logLevel
= LogLevel::Info
;
130 } else if (prefValue
.LowerCaseEqualsLiteral("debug")) {
131 logLevel
= LogLevel::Debug
;
132 } else if (prefValue
.LowerCaseEqualsLiteral("verbose")) {
133 logLevel
= LogLevel::Verbose
;
137 const char* moduleName
= aName
+ strlen(kLoggingPrefPrefix
);
138 LogModule::Get(moduleName
)->SetLevel(logLevel
);
141 static void LoadExistingPrefs() {
142 nsIPrefBranch
* root
= Preferences::GetRootBranch();
147 nsTArray
<nsCString
> names
;
148 nsresult rv
= root
->GetChildList(kLoggingPrefPrefix
, names
);
149 if (NS_SUCCEEDED(rv
)) {
150 for (auto& name
: names
) {
151 LoadPrefValue(name
.get());
156 LogModulePrefWatcher::LogModulePrefWatcher() = default;
158 void LogModulePrefWatcher::RegisterPrefWatcher() {
159 RefPtr
<LogModulePrefWatcher
> prefWatcher
= new LogModulePrefWatcher();
160 Preferences::AddStrongObserver(prefWatcher
, kLoggingPrefPrefix
);
162 nsCOMPtr
<nsIObserverService
> observerService
=
163 mozilla::services::GetObserverService();
164 if (observerService
&& XRE_IsParentProcess()) {
165 observerService
->AddObserver(prefWatcher
,
166 "browser-delayed-startup-finished", false);
173 LogModulePrefWatcher::Observe(nsISupports
* aSubject
, const char* aTopic
,
174 const char16_t
* aData
) {
175 if (strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
, aTopic
) == 0) {
176 NS_LossyConvertUTF16toASCII
prefName(aData
);
177 LoadPrefValue(prefName
.get());
178 } else if (strcmp("browser-delayed-startup-finished", aTopic
) == 0) {
179 bool clear
= Preferences::GetBool(kLoggingPrefClearOnStartup
, true);
181 ResetExistingPrefs();
183 nsCOMPtr
<nsIObserverService
> observerService
=
184 mozilla::services::GetObserverService();
185 if (observerService
) {
186 observerService
->RemoveObserver(this, "browser-delayed-startup-finished");
193 } // namespace mozilla