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/. */
6 
7 #include "LogModulePrefWatcher.h"
8 
9 #include "mozilla/Logging.h"
10 #include "mozilla/Preferences.h"
11 #include "mozilla/Services.h"
12 #include "nsIObserverService.h"
13 #include "nsMemory.h"
14 #include "nsString.h"
15 #include "nsXULAppAPI.h"
16 #include "base/process_util.h"
17 
18 static const char kLoggingPrefPrefix[] = "logging.";
19 static const char kLoggingConfigPrefPrefix[] = "logging.config";
20 static const int kLoggingConfigPrefixLen = sizeof(kLoggingConfigPrefPrefix) - 1;
21 static const char kLoggingPrefClearOnStartup[] =
22     "logging.config.clear_on_startup";
23 static const char kLoggingPrefLogFile[] = "logging.config.LOG_FILE";
24 static const char kLoggingPrefAddTimestamp[] = "logging.config.add_timestamp";
25 static const char kLoggingPrefSync[] = "logging.config.sync";
26 
27 namespace mozilla {
28 
NS_IMPL_ISUPPORTS(LogModulePrefWatcher,nsIObserver)29 NS_IMPL_ISUPPORTS(LogModulePrefWatcher, nsIObserver)
30 
31 /**
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.
35  *
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.
38  */
39 static void ResetExistingPrefs() {
40   nsTArray<nsCString> names;
41   nsresult rv =
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());
47     }
48   }
49 }
50 
51 /**
52  * Loads the log level from the given pref and updates the corresponding
53  * LogModule.
54  */
LoadPrefValue(const char * aName)55 static void LoadPrefValue(const char* aName) {
56   LogLevel logLevel = LogLevel::Disabled;
57 
58   nsresult rv;
59   int32_t prefLevel = 0;
60   nsAutoCString prefValue;
61 
62   if (strncmp(aName, kLoggingConfigPrefPrefix, kLoggingConfigPrefixLen) == 0) {
63     nsAutoCString prefName(aName);
64 
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);
70         return;
71       }
72 
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);
76       }
77 
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     }
86     return;
87   }
88 
89   if (Preferences::GetInt(aName, &prefLevel) == NS_OK) {
90     logLevel = ToLogLevel(prefLevel);
91   } else if (Preferences::GetCString(aName, prefValue) == NS_OK) {
92     if (prefValue.LowerCaseEqualsLiteral("error")) {
93       logLevel = LogLevel::Error;
94     } else if (prefValue.LowerCaseEqualsLiteral("warning")) {
95       logLevel = LogLevel::Warning;
96     } else if (prefValue.LowerCaseEqualsLiteral("info")) {
97       logLevel = LogLevel::Info;
98     } else if (prefValue.LowerCaseEqualsLiteral("debug")) {
99       logLevel = LogLevel::Debug;
100     } else if (prefValue.LowerCaseEqualsLiteral("verbose")) {
101       logLevel = LogLevel::Verbose;
102     }
103   }
104 
105   const char* moduleName = aName + strlen(kLoggingPrefPrefix);
106   LogModule::Get(moduleName)->SetLevel(logLevel);
107 }
108 
LoadExistingPrefs()109 static void LoadExistingPrefs() {
110   nsIPrefBranch* root = Preferences::GetRootBranch();
111   if (!root) {
112     return;
113   }
114 
115   nsTArray<nsCString> names;
116   nsresult rv = root->GetChildList(kLoggingPrefPrefix, names);
117   if (NS_SUCCEEDED(rv)) {
118     for (auto& name : names) {
119       LoadPrefValue(name.get());
120     }
121   }
122 }
123 
124 LogModulePrefWatcher::LogModulePrefWatcher() = default;
125 
RegisterPrefWatcher()126 void LogModulePrefWatcher::RegisterPrefWatcher() {
127   RefPtr<LogModulePrefWatcher> prefWatcher = new LogModulePrefWatcher();
128   Preferences::AddStrongObserver(prefWatcher, kLoggingPrefPrefix);
129 
130   nsCOMPtr<nsIObserverService> observerService =
131       mozilla::services::GetObserverService();
132   if (observerService && XRE_IsParentProcess()) {
133     observerService->AddObserver(prefWatcher,
134                                  "browser-delayed-startup-finished", false);
135   }
136 
137   LoadExistingPrefs();
138 }
139 
140 NS_IMETHODIMP
Observe(nsISupports * aSubject,const char * aTopic,const char16_t * aData)141 LogModulePrefWatcher::Observe(nsISupports* aSubject, const char* aTopic,
142                               const char16_t* aData) {
143   if (strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic) == 0) {
144     NS_LossyConvertUTF16toASCII prefName(aData);
145     LoadPrefValue(prefName.get());
146   } else if (strcmp("browser-delayed-startup-finished", aTopic) == 0) {
147     bool clear = Preferences::GetBool(kLoggingPrefClearOnStartup, true);
148     if (clear) {
149       ResetExistingPrefs();
150     }
151     nsCOMPtr<nsIObserverService> observerService =
152         mozilla::services::GetObserverService();
153     if (observerService) {
154       observerService->RemoveObserver(this, "browser-delayed-startup-finished");
155     }
156   }
157 
158   return NS_OK;
159 }
160 
161 }  // namespace mozilla
162