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"
14 #include "nsXULAppAPI.h"
15 #include "base/process_util.h"
17 static const char kLoggingPrefPrefix
[] = "logging.";
18 static const char kLoggingConfigPrefPrefix
[] = "logging.config";
19 static const int kLoggingConfigPrefixLen
= sizeof(kLoggingConfigPrefPrefix
) - 1;
20 static const char kLoggingPrefClearOnStartup
[] =
21 "logging.config.clear_on_startup";
22 static const char kLoggingPrefLogFile
[] = "logging.config.LOG_FILE";
23 static const char kLoggingPrefAddTimestamp
[] = "logging.config.add_timestamp";
24 static const char kLoggingPrefSync
[] = "logging.config.sync";
25 static const char kLoggingPrefStacks
[] = "logging.config.profilerstacks";
29 NS_IMPL_ISUPPORTS(LogModulePrefWatcher
, nsIObserver
)
32 * Resets all the preferences in the logging. branch
33 * This is needed because we may crash while logging, and this would cause us
34 * to log after restarting as well.
36 * If logging after restart is desired, set the logging.config.clear_on_startup
37 * pref to false, or use the MOZ_LOG_FILE and MOZ_LOG_MODULES env vars.
39 static void ResetExistingPrefs() {
40 nsTArray
<nsCString
> names
;
42 Preferences::GetRootBranch()->GetChildList(kLoggingPrefPrefix
, names
);
43 if (NS_SUCCEEDED(rv
)) {
44 for (auto& name
: names
) {
45 // Clearing the pref will cause it to reload, thus resetting the log level
46 Preferences::ClearUser(name
.get());
52 * Loads the log level from the given pref and updates the corresponding
55 static void LoadPrefValue(const char* aName
) {
56 LogLevel logLevel
= LogLevel::Disabled
;
59 int32_t prefLevel
= 0;
60 nsAutoCString prefValue
;
62 if (strncmp(aName
, kLoggingConfigPrefPrefix
, kLoggingConfigPrefixLen
) == 0) {
63 nsAutoCString
prefName(aName
);
65 if (prefName
.EqualsLiteral(kLoggingPrefLogFile
)) {
66 rv
= Preferences::GetCString(aName
, prefValue
);
67 // The pref was reset. Clear the user file.
68 if (NS_FAILED(rv
) || prefValue
.IsEmpty()) {
69 LogModule::SetLogFile(nullptr);
73 // If the pref value doesn't have a PID placeholder, append it to the end.
74 if (!strstr(prefValue
.get(), MOZ_LOG_PID_TOKEN
)) {
75 prefValue
.AppendLiteral(MOZ_LOG_PID_TOKEN
);
78 LogModule::SetLogFile(prefValue
.BeginReading());
79 } else if (prefName
.EqualsLiteral(kLoggingPrefAddTimestamp
)) {
80 bool addTimestamp
= Preferences::GetBool(aName
, false);
81 LogModule::SetAddTimestamp(addTimestamp
);
82 } else if (prefName
.EqualsLiteral(kLoggingPrefSync
)) {
83 bool sync
= Preferences::GetBool(aName
, false);
84 LogModule::SetIsSync(sync
);
85 } else if (prefName
.EqualsLiteral(kLoggingPrefStacks
)) {
86 bool captureStacks
= Preferences::GetBool(aName
, false);
87 LogModule::SetCaptureStacks(captureStacks
);
92 if (Preferences::GetInt(aName
, &prefLevel
) == NS_OK
) {
93 logLevel
= ToLogLevel(prefLevel
);
94 } else if (Preferences::GetCString(aName
, prefValue
) == NS_OK
) {
95 if (prefValue
.LowerCaseEqualsLiteral("error")) {
96 logLevel
= LogLevel::Error
;
97 } else if (prefValue
.LowerCaseEqualsLiteral("warning")) {
98 logLevel
= LogLevel::Warning
;
99 } else if (prefValue
.LowerCaseEqualsLiteral("info")) {
100 logLevel
= LogLevel::Info
;
101 } else if (prefValue
.LowerCaseEqualsLiteral("debug")) {
102 logLevel
= LogLevel::Debug
;
103 } else if (prefValue
.LowerCaseEqualsLiteral("verbose")) {
104 logLevel
= LogLevel::Verbose
;
108 const char* moduleName
= aName
+ strlen(kLoggingPrefPrefix
);
109 LogModule::Get(moduleName
)->SetLevel(logLevel
);
112 static void LoadExistingPrefs() {
113 nsIPrefBranch
* root
= Preferences::GetRootBranch();
118 nsTArray
<nsCString
> names
;
119 nsresult rv
= root
->GetChildList(kLoggingPrefPrefix
, names
);
120 if (NS_SUCCEEDED(rv
)) {
121 for (auto& name
: names
) {
122 LoadPrefValue(name
.get());
127 LogModulePrefWatcher::LogModulePrefWatcher() = default;
129 void LogModulePrefWatcher::RegisterPrefWatcher() {
130 RefPtr
<LogModulePrefWatcher
> prefWatcher
= new LogModulePrefWatcher();
131 Preferences::AddStrongObserver(prefWatcher
, kLoggingPrefPrefix
);
133 nsCOMPtr
<nsIObserverService
> observerService
=
134 mozilla::services::GetObserverService();
135 if (observerService
&& XRE_IsParentProcess()) {
136 observerService
->AddObserver(prefWatcher
,
137 "browser-delayed-startup-finished", false);
144 LogModulePrefWatcher::Observe(nsISupports
* aSubject
, const char* aTopic
,
145 const char16_t
* aData
) {
146 if (strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
, aTopic
) == 0) {
147 NS_LossyConvertUTF16toASCII
prefName(aData
);
148 LoadPrefValue(prefName
.get());
149 } else if (strcmp("browser-delayed-startup-finished", aTopic
) == 0) {
150 bool clear
= Preferences::GetBool(kLoggingPrefClearOnStartup
, true);
152 ResetExistingPrefs();
154 nsCOMPtr
<nsIObserverService
> observerService
=
155 mozilla::services::GetObserverService();
156 if (observerService
) {
157 observerService
->RemoveObserver(this, "browser-delayed-startup-finished");
164 } // namespace mozilla