Skip to content

Commit

Permalink
Logging refactoring #1 Subscription and Content Blocking (#3091)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1205842942115003/1206750146033742/f
Tech Design URL:
https://app.asana.com/0/1200194497630846/1206777133329590/f

**Description**:

Step 1 of the Logging refactoring
Subscription
Content Blocking
User Scripts
SecureVault
History
Remote messages
General (removed and create local UserDefaultCache logger)
  • Loading branch information
federicocappelli authored Aug 20, 2024
1 parent 2fafe3c commit 94409e4
Show file tree
Hide file tree
Showing 33 changed files with 47 additions and 140 deletions.
7 changes: 0 additions & 7 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ custom_rules:
- keyword
message: "Classes should be `final` by default, use explicit `internal` or `public` for non-final classes."
severity: error
enforce_os_log_wrapper:
included: ".*\\.swift"
name: "Use `import Common` for os_log instead of `import os.log`"
regex: "^(import (?:os\\.log|os|OSLog))$"
capture_group: 0
message: "os_log wrapper ensures log args are @autoclosures (computed when needed) and to be able to use String Interpolation."
severity: error

# Rule Config
cyclomatic_complexity:
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13453,7 +13453,7 @@
repositoryURL = "https://github.com/duckduckgo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 184.0.3;
version = 185.0.0;
};
};
9FF521422BAA8FF300B9819B /* XCRemoteSwiftPackageReference "lottie-spm" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/BrowserServicesKit",
"state" : {
"revision" : "f166fbddeb8c368f82b0c150a7fa0ff06031196d",
"version" : "184.0.3"
"revision" : "2efee14db4d30ac73916794de4d262b745a5f413",
"version" : "185.0.0"
}
},
{
Expand Down Expand Up @@ -75,7 +75,7 @@
{
"identity" : "lottie-spm",
"kind" : "remoteSourceControl",
"location" : "https://github.com/airbnb/lottie-spm",
"location" : "https://github.com/airbnb/lottie-spm.git",
"state" : {
"revision" : "1d29eccc24cc8b75bff9f6804155112c0ffc9605",
"version" : "4.4.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1530"
version = "1.8">
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
Expand Down
6 changes: 2 additions & 4 deletions DuckDuckGo/Common/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
// limitations under the License.
//

import Common
import Foundation
import os.log // swiftlint:disable:this enforce_os_log_wrapper
import os.log

extension OSLog {

Expand All @@ -29,7 +28,6 @@ extension OSLog {
case fire = "Fire"
case dataImportExport = "Data Import/Export"
case pixel = "Pixel"
case contentBlocking = "Content Blocking"
case httpsUpgrade = "HTTPS Upgrade"
case favicons = "Favicons"
case autoLock = "Auto-Lock"
Expand All @@ -48,7 +46,7 @@ extension OSLog {

enum AllCategories {
static var allCases: [String] {
Categories.allCases.map(\.rawValue) + AppCategories.allCases.map(\.rawValue)
AppCategories.allCases.map(\.rawValue)
}
}

Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/Configuration/ConfigurationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ final class ConfigurationManager {
private var lastRefreshCheckTime: Date = Date()

private lazy var fetcher = ConfigurationFetcher(store: ConfigurationStore.shared,
log: .config,
eventMapping: Self.configurationDebugEvents)

static let configurationDebugEvents = EventMapping<ConfigurationDebugEvents> { event, error, _, _ in
Expand Down
3 changes: 1 addition & 2 deletions DuckDuckGo/ContentBlocker/ContentBlocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ final class AppContentBlocking {
contentBlockingManager = ContentBlockerRulesManager(rulesSource: contentBlockerRulesSource,
exceptionsSource: exceptionsSource,
cache: ContentBlockingRulesCache(),
errorReporting: Self.debugEvents,
log: .contentBlocking)
errorReporting: Self.debugEvents)
userContentUpdating = UserContentUpdating(contentBlockerRulesManager: contentBlockingManager,
privacyConfigurationManager: privacyConfigurationManager,
trackerDataManager: trackerDataManager,
Expand Down
5 changes: 3 additions & 2 deletions DuckDuckGo/History/Services/EncryptedHistoryStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import CoreData
import Combine
import History
import PixelKit
import os.log

final class EncryptedHistoryStore: HistoryStoring {

Expand Down Expand Up @@ -90,7 +91,7 @@ final class EncryptedHistoryStore: HistoryStoring {
for entry in entriesToDelete {
context.delete(entry)
}
os_log("%d items cleaned from history", log: .history, entriesToDelete.count)
Logger.history.debug("\(entriesToDelete.count) items cleaned from history")
} catch {
PixelKit.fire(DebugEvent(GeneralPixel.historyRemoveFailed, error: error))
self.context.reset()
Expand All @@ -114,7 +115,7 @@ final class EncryptedHistoryStore: HistoryStoring {
fetchRequest.returnsObjectsAsFaults = false
do {
let historyEntries = try context.fetch(fetchRequest)
os_log("%d entries loaded from history", log: .history, historyEntries.count)
Logger.history.debug("\(historyEntries.count) entries loaded from history")
let history = BrowsingHistory(historyEntries: historyEntries)
return .success(history)
} catch {
Expand Down
82 changes: 1 addition & 81 deletions DuckDuckGo/Menus/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import BrowserServicesKit
import Cocoa
import Common
import Combine
import OSLog // swiftlint:disable:this enforce_os_log_wrapper
import OSLog
import SwiftUI
import WebKit
import Configuration
Expand Down Expand Up @@ -408,7 +408,6 @@ final class MainMenu: NSMenu {
updateHomeButtonMenuItem()
updateBookmarksBarMenuItem()
updateShortcutMenuItems()
updateLoggingMenuItems()
updateInternalUserItem()
updateRemoteConfigurationInfo()
updateAutofillDebugScriptMenuItem()
Expand Down Expand Up @@ -664,23 +663,6 @@ final class MainMenu: NSMenu {
private func setupLoggingMenu() -> NSMenu {
let menu = NSMenu(title: "")

menu.addItem(NSMenuItem(title: "Enable All", action: #selector(enableAllLogsMenuItemAction), target: self))
menu.addItem(NSMenuItem(title: "Disable All", action: #selector(disableAllLogsMenuItemAction), target: self))
menu.addItem(.separator())

for category in OSLog.AllCategories.allCases.sorted() {
let menuItem = NSMenuItem(title: category, action: #selector(loggingMenuItemAction), target: self)
menuItem.identifier = .init(category)
menu.addItem(menuItem)
}

menu.addItem(autofillDebugScriptMenuItem
.targetting(self))

menu.addItem(.separator())
let debugLoggingMenuItem = NSMenuItem(title: OSLog.isRunningInDebugEnvironment ? "Disable DEBUG level logging…" : "Enable DEBUG level logging…", action: #selector(debugLoggingMenuItemAction), target: self)
menu.addItem(debugLoggingMenuItem)

if #available(macOS 12.0, *) {
let exportLogsMenuItem = NSMenuItem(title: "Save Logs…", action: #selector(exportLogs), target: self)
menu.addItem(exportLogsMenuItem)
Expand All @@ -694,17 +676,6 @@ final class MainMenu: NSMenu {
internalUserItem.title = NSApp.delegateTyped.internalUserDecider.isInternalUser ? "Remove Internal User State" : "Set Internal User State"
}

private func updateLoggingMenuItems() {
guard let loggingMenu else { return }

let enabledCategories = OSLog.loggingCategories
for item in loggingMenu.items {
guard let category = item.identifier.map(\.rawValue) else { continue }

item.state = enabledCategories.contains(category) ? .on : .off
}
}

private func updateAutofillDebugScriptMenuItem() {
autofillDebugScriptMenuItem.state = AutofillPreferences().debugScriptEnabled ? .on : .off
}
Expand All @@ -721,63 +692,12 @@ final class MainMenu: NSMenu {
customConfigurationUrlMenuItem.title = "Configuration URL: \(AppConfigurationURLProvider().url(for: .privacyConfiguration).absoluteString)"
}

@objc private func loggingMenuItemAction(_ sender: NSMenuItem) {
guard let category = sender.identifier?.rawValue else { return }

if case .on = sender.state {
OSLog.loggingCategories.remove(category)
} else {
OSLog.loggingCategories.insert(category)
}
}

@objc private func enableAllLogsMenuItemAction(_ sender: NSMenuItem) {
OSLog.loggingCategories = Set(OSLog.AllCategories.allCases)
}

@objc private func disableAllLogsMenuItemAction(_ sender: NSMenuItem) {
OSLog.loggingCategories = []
}

@objc private func toggleAutofillScriptDebugSettingsAction(_ sender: NSMenuItem) {
AutofillPreferences().debugScriptEnabled = !AutofillPreferences().debugScriptEnabled
NotificationCenter.default.post(name: .autofillScriptDebugSettingsDidChange, object: nil)
updateAutofillDebugScriptMenuItem()
}

@objc private func debugLoggingMenuItemAction(_ sender: NSMenuItem) {
#if APPSTORE
if !OSLog.isRunningInDebugEnvironment {
let alert = NSAlert()
alert.messageText = "Restart with DEBUG logging Enabled not supported for AppStore build"
alert.informativeText = """
Open terminal and run:
export \(ProcessInfo.Constants.osActivityMode)=\(ProcessInfo.Constants.debug)
"\(Bundle.main.executablePath!)"
"""
alert.runModal()

return
}
#endif

let alert = NSAlert()
alert.messageText = "Restart with DEBUG logging \(OSLog.isRunningInDebugEnvironment ? "Disabled" : "Enabled")?"
alert.addButton(withTitle: "Restart").tag = NSApplication.ModalResponse.OK.rawValue
alert.addButton(withTitle: "Cancel").tag = NSApplication.ModalResponse.cancel.rawValue
guard case .OK = alert.runModal() else { return }

let config = NSWorkspace.OpenConfiguration()
config.createsNewApplicationInstance = true
config.environment = [ProcessInfo.Constants.osActivityMode: (OSLog.isRunningInDebugEnvironment ? "" : ProcessInfo.Constants.debug)]

NSWorkspace.shared.openApplication(at: Bundle.main.bundleURL, configuration: config)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
NSApp.terminate(nil)
}
}

@available(macOS 12.0, *)
@objc private func exportLogs(_ sender: NSMenuItem) {
let displayName = Bundle.main.displayName!.replacingOccurrences(of: " ", with: "")
Expand Down
10 changes: 5 additions & 5 deletions DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Combine
import Common
import BrowserServicesKit
import PixelKit

import os.log
import NetworkProtection
import NetworkProtectionIPC
import NetworkProtectionUI
Expand Down Expand Up @@ -866,23 +866,23 @@ final class NavigationBarViewController: NSViewController {
let autofillPreferences = AutofillPreferences()

if autofillPreferences.askToSaveUsernamesAndPasswords, let credentials = data.credentials {
os_log("Presenting Save Credentials popover", log: .passwordManager)
Logger.passwordManager.debug("Presenting Save Credentials popover")
popovers.displaySaveCredentials(credentials,
automaticallySaved: data.automaticallySavedCredentials,
usingView: passwordManagementButton,
withDelegate: self)
} else if autofillPreferences.askToSavePaymentMethods, let card = data.creditCard {
os_log("Presenting Save Payment Method popover", log: .passwordManager)
Logger.passwordManager.debug("Presenting Save Payment Method popover")
popovers.displaySavePaymentMethod(card,
usingView: passwordManagementButton,
withDelegate: self)
} else if autofillPreferences.askToSaveAddresses, let identity = data.identity {
os_log("Presenting Save Identity popover", log: .passwordManager)
Logger.passwordManager.debug("Presenting Save Identity popover")
popovers.displaySaveIdentity(identity,
usingView: passwordManagementButton,
withDelegate: self)
} else {
os_log("Received save autofill data call, but there was no data to present", log: .passwordManager)
Logger.passwordManager.error("Received save autofill data call, but there was no data to present")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Foundation
import Networking
import NetworkExtension
import NetworkProtectionProxy
import os.log // swiftlint:disable:this enforce_os_log_wrapper
import os.log
import PixelKit

final class MacTransparentProxyProvider: TransparentProxyProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import Foundation
import NetworkProtection
// swiftlint:disable:next enforce_os_log_wrapper
import OSLog
import os.log

/// Logger for the VPN
///
Expand Down
5 changes: 3 additions & 2 deletions DuckDuckGo/RemoteMessaging/ActiveRemoteMessageModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Common
import Foundation
import PixelKit
import RemoteMessaging
import os.log

/**
* This is used to feed a remote message to the home page view.
Expand Down Expand Up @@ -137,12 +138,12 @@ final class ActiveRemoteMessageModel: ObservableObject {
guard let remoteMessage, let store = store() else {
return
}
os_log("Remote message shown: %s", log: .remoteMessaging, type: .info, remoteMessage.id)
Logger.remoteMessaging.info("Remote message shown: \(remoteMessage.id, privacy: .public)")
if remoteMessage.isMetricsEnabled {
PixelKit.fire(GeneralPixel.remoteMessageShown, withAdditionalParameters: ["message": remoteMessage.id])
}
if !store.hasShownRemoteMessage(withID: remoteMessage.id) {
os_log("Remote message shown for first time: %s", log: .remoteMessaging, type: .info, remoteMessage.id)
Logger.remoteMessaging.info("Remote message shown for first time: \(remoteMessage.id, privacy: .public)")
if remoteMessage.isMetricsEnabled {
PixelKit.fire(GeneralPixel.remoteMessageShownUnique, withAdditionalParameters: ["message": remoteMessage.id])
}
Expand Down
4 changes: 1 addition & 3 deletions DuckDuckGo/RemoteMessaging/RemoteMessagingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ struct DefaultRemoteMessagingStoreProvider: RemoteMessagingStoreProviding {
database: database,
notificationCenter: .default,
errorEvents: RemoteMessagingStoreErrorHandling(),
remoteMessagingAvailabilityProvider: availabilityProvider,
log: .remoteMessaging
remoteMessagingAvailabilityProvider: availabilityProvider
)
}
}
Expand Down Expand Up @@ -98,7 +97,6 @@ final class RemoteMessagingClient: RemoteMessagingProcessing {
configurationFetcher: ConfigurationFetcher(
store: configurationStore,
urlSession: .session(),
log: .remoteMessaging,
eventMapping: ConfigurationManager.configurationDebugEvents
),
configurationStore: ConfigurationStore.shared
Expand Down
3 changes: 1 addition & 2 deletions DuckDuckGo/Sync/SyncCredentialsAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ final class SyncCredentialsAdapter {
databaseCleaner = CredentialsDatabaseCleaner(
secureVaultFactory: secureVaultFactory,
secureVaultErrorReporter: SecureVaultReporter.shared,
errorEvents: CredentialsCleanupErrorHandling(),
log: .passwordManager
errorEvents: CredentialsCleanupErrorHandling()
)
}

Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/Tab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import UserScript
import WebKit
import History
import PixelKit
import os.log

protocol TabDelegate: ContentOverlayUserScriptDelegate {
func tabWillStartNavigation(_ tab: Tab, isUserInitiated: Bool)
Expand Down Expand Up @@ -1012,7 +1013,7 @@ extension Tab: UserContentControllerDelegate {

@MainActor
func userContentController(_ userContentController: UserContentController, didInstallContentRuleLists contentRuleLists: [String: WKContentRuleList], userScripts: UserScriptsProvider, updateEvent: ContentBlockerRulesManager.UpdateEvent) {
os_log("didInstallContentRuleLists", log: .contentBlocking, type: .info)
Logger.contentBlocking.info("didInstallContentRuleLists")
guard let userScripts = userScripts as? UserScripts else { fatalError("Unexpected UserScripts") }

userScripts.debugScript.instrumentation = instrumentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Common
import ContentBlocking
import Foundation
import Navigation
import os.log

struct DetectedTracker {
enum TrackerType {
Expand Down Expand Up @@ -109,7 +110,7 @@ extension ContentBlockingTabExtension: NavigationResponder {
// Ensure Content Blocking Assets (WKContentRuleList&UserScripts) are installed
if userContentController?.contentBlockingAssetsInstalled == false
&& privacyConfigurationManager.privacyConfig.isEnabled(featureKey: .contentBlocking) {
os_log("%d: tabWillWaitForRulesCompilation", log: .contentBlocking, identifier)
Logger.contentBlocking.log("\(self.identifier) tabWillWaitForRulesCompilation")
cbaTimeReporter?.tabWillWaitForRulesCompilation(identifier)

disableLongDecisionMakingChecks()
Expand All @@ -118,7 +119,7 @@ extension ContentBlockingTabExtension: NavigationResponder {
}

await userContentController?.awaitContentBlockingAssetsInstalled()
os_log("%d: Rules Compilation done", log: .contentBlocking, identifier)
Logger.contentBlocking.log("\(self.identifier) Rules Compilation done")
cbaTimeReporter?.reportWaitTimeForTabFinishedWaitingForRules(identifier)
} else {
cbaTimeReporter?.reportNavigationDidNotWaitForRules()
Expand Down
Loading

0 comments on commit 94409e4

Please sign in to comment.