From a10df3fb8ad4fd743c6a21da995e2107d497494a Mon Sep 17 00:00:00 2001 From: Jacob Sikorski Date: Tue, 7 Feb 2023 18:00:39 +0100 Subject: [PATCH] Add debugging logs --- ...rViewController+WKNavigationDelegate.swift | 4 ++ .../Browser/Helpers/LaunchHelper.swift | 9 ++++ .../WebFilters/AdBlockEngineManager.swift | 32 ++++++------ .../ContentBlockerManager.swift | 49 +++++++++---------- 4 files changed, 52 insertions(+), 42 deletions(-) diff --git a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+WKNavigationDelegate.swift b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+WKNavigationDelegate.swift index ecdc2dd087c..e63ed5963be 100644 --- a/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+WKNavigationDelegate.swift +++ b/Sources/Brave/Frontend/Browser/BrowserViewController/BrowserViewController+WKNavigationDelegate.swift @@ -129,6 +129,10 @@ extension BrowserViewController: WKNavigationDelegate { guard let url = navigationAction.request.url else { return (.cancel, preferences) } + + #if DEBUG + ContentBlockerManager.log.debug("Nav: \(url.absoluteString)") + #endif if InternalURL.isValid(url: url) { if navigationAction.navigationType != .backForward, navigationAction.isInternalUnprivileged { diff --git a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift index 987eb337eb6..02e4f6df029 100644 --- a/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift +++ b/Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift @@ -27,6 +27,10 @@ public actor LaunchHelper { // Otherwise prepare the services and await the task let task = Task { + #if DEBUG + let startTime = CFAbsoluteTimeGetCurrent() + #endif + // Load cached data // This is done first because compileResources and loadCachedRuleLists need their results async let loadBundledResources: Void = ContentBlockerManager.shared.loadBundledResources() @@ -39,6 +43,11 @@ public actor LaunchHelper { async let cachedRuleListLoad: Void = ContentBlockerManager.shared.loadCachedRuleLists() _ = await (compiledResourcesCompile, cachedRuleListLoad) + #if DEBUG + let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime + ContentBlockerManager.log.debug("Adblock loaded: \(timeElapsed)s") + #endif + // This one is non-blocking performPostLoadTasks(adBlockService: adBlockService) markAdBlockReady() diff --git a/Sources/Brave/WebFilters/AdBlockEngineManager.swift b/Sources/Brave/WebFilters/AdBlockEngineManager.swift index 002fd80cf20..975080878ca 100644 --- a/Sources/Brave/WebFilters/AdBlockEngineManager.swift +++ b/Sources/Brave/WebFilters/AdBlockEngineManager.swift @@ -187,38 +187,36 @@ public actor AdBlockEngineManager: Sendable { extension AdBlockEngineManager { /// A method that logs info on the given resources fileprivate func debug(compiledResults: [ResourceWithVersion: Result]) { - let resourcesString = compiledResults.sorted(by: { $0.key.order < $1.key.order }) - .map { (resourceWithVersion, compileResult) -> String in + log.debug("Loaded \(compiledResults.count, privacy: .public) (total) engine resources:") + + compiledResults.sorted(by: { $0.key.order < $1.key.order }) + .forEach { (resourceWithVersion, compileResult) in let resultString: String switch compileResult { case .success: - resultString = "success" + resultString = "✔︎" case .failure(let error): - resultString = error.localizedDescription + resultString = "\(error)" } let sourceDebugString = [ - resourceWithVersion.debugDescription, - "result: \(resultString)", - ].joined(separator: ", ") + "", resourceWithVersion.debugDescription, + "\(resultString)", + ].joined(separator: " ") - return ["{", sourceDebugString, "}"].joined() - }.joined(separator: ", ") - - log.debug("Loaded \(self.enabledResources.count, privacy: .public) (total) engine resources: \(resourcesString, privacy: .public)") + log.debug("\(sourceDebugString, privacy: .public)") + } } } extension AdBlockEngineManager.ResourceWithVersion: CustomDebugStringConvertible { public var debugDescription: String { return [ - "order: \(order)", - "fileName: \(fileURL.lastPathComponent)", - "source: \(resource.source.debugDescription)", - "version: \(version ?? "nil")", - "type: \(resource.type.debugDescription)" - ].joined(separator: ", ") + "#\(order)", + "\(resource.source.debugDescription).\(resource.type.debugDescription)", + "v\(version ?? "nil")", + ].joined(separator: " ") } } diff --git a/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift b/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift index b0b9b89495b..61587fff554 100644 --- a/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift +++ b/Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift @@ -394,45 +394,44 @@ final public class ContentBlockerManager: Sendable { #if DEBUG /// A method that logs info on the given resources private func debug(resources: [String: Resource]) { - let resourcesString = resources + Self.log.debug("Compiled \(resources.count, privacy: .public) block list resources:") + + resources .sorted(by: { lhs, rhs in lhs.value.url.absoluteString < rhs.value.url.absoluteString }) - .map { identifier, compiledResource -> String in - let sourceString: String - let versionString: String + .forEach { identifier, compiledResource in let resultString: String - switch compiledResource.sourceType { - case .bundled: - sourceString = "bundled" - versionString = "nil" - case .downloaded(let version): - sourceString = "downloaded" - versionString = version ?? "nil" - } - switch self.cachedCompileResults[identifier]?.result { case .failure(let error): resultString = error.localizedDescription case .success: - resultString = "success" + resultString = "✔︎" case .none: - resultString = "nil" + resultString = "?" } let resourcesDebugString = [ - "identifier: \(identifier)", - "fileName: \(compiledResource.url.lastPathComponent)", - "source: \(sourceString)", - "version: \(versionString)", - "result: \(resultString)" - ].joined(separator: ", ") + identifier, compiledResource.sourceType.debugDescription, + resultString + ].joined(separator: " ") - return ["{", resourcesDebugString, "}"].joined() - }.joined(separator: ", ") - - Self.log.debug("Compiled \(resources.count, privacy: .public) additional block list resources: \(resourcesString, privacy: .public))") + Self.log.debug(" \(resourcesDebugString)") + } } #endif } + +#if DEBUG +extension ContentBlockerManager.BlocklistSourceType: CustomDebugStringConvertible { + public var debugDescription: String { + switch self { + case .bundled: + return "bundled" + case .downloaded(let version): + return version ?? "nil" + } + } +} +#endif