Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Add debugging logs
Browse files Browse the repository at this point in the history
  • Loading branch information
cuba committed Feb 28, 2023
1 parent e44fd94 commit a10df3f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions Sources/Brave/Frontend/Browser/Helpers/LaunchHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
32 changes: 15 additions & 17 deletions Sources/Brave/WebFilters/AdBlockEngineManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void, Error>]) {
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: " ")
}
}

Expand Down
49 changes: 24 additions & 25 deletions Sources/Brave/WebFilters/ContentBlocker/ContentBlockerManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a10df3f

Please sign in to comment.