-
Notifications
You must be signed in to change notification settings - Fork 18
Native crumbs #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Native crumbs #111
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aaf7aa6
Remove Cassette dependency
melekr 1d3d5b1
Implement swift breadcrumbs
melekr 709c549
Update BacktraceBreadcrumbFile class
melekr 3d09e17
Synchronize addBreadcrumb operation
melekr d66875a
Add BreadcrumbRecord
melekr d8ece9d
Remove extra do & catch statements
melekr f592065
Code cleanup
melekr e851ab7
Update project.pbxproj
melekr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
Sources/Features/Breadcrumb/BacktraceBreadcrumbFile.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import Foundation | ||
|
|
||
| enum BacktraceBreadcrumbFileError: Error { | ||
| case invalidFormat | ||
| } | ||
|
|
||
| @objc class BacktraceBreadcrumbFile: NSObject { | ||
|
|
||
| private static let minimumQueueFileSizeBytes = 4096 | ||
| private let maximumIndividualBreadcrumbSize: Int | ||
| private let maxQueueFileSizeBytes: Int | ||
| private let queue: Queue<BreadcrumbRecord> | ||
| private let breadcrumbLogURL: URL | ||
| private let dispatchQueue = DispatchQueue(label: "io.backtrace.BacktraceBreadcrumbFile@\(UUID().uuidString)") | ||
|
|
||
| public init(_ breadcrumbSettings: BacktraceBreadcrumbSettings) throws { | ||
| self.breadcrumbLogURL = try breadcrumbSettings.getBreadcrumbLogPath() | ||
| self.queue = Queue<BreadcrumbRecord>() | ||
| self.maximumIndividualBreadcrumbSize = breadcrumbSettings.maxIndividualBreadcrumbSizeBytes | ||
| if breadcrumbSettings.maxQueueFileSizeBytes < BacktraceBreadcrumbFile.minimumQueueFileSizeBytes { | ||
| BacktraceLogger.warning("\(breadcrumbSettings.maxQueueFileSizeBytes) is smaller than the minimum of " + | ||
| "\(BacktraceBreadcrumbFile.minimumQueueFileSizeBytes)" + | ||
| ", ignoring value and overriding with minimum.") | ||
| self.maxQueueFileSizeBytes = BacktraceBreadcrumbFile.minimumQueueFileSizeBytes | ||
| } else { | ||
| self.maxQueueFileSizeBytes = breadcrumbSettings.maxQueueFileSizeBytes | ||
| } | ||
|
|
||
| super.init() | ||
| } | ||
|
|
||
| func addBreadcrumb(_ breadcrumb: [String: Any]) -> Bool { | ||
| guard let breadcrumbJsonData = try? JSONSerialization.data(withJSONObject: breadcrumb) else { | ||
| BacktraceLogger.warning("Error when converting breadcrumb to data") | ||
| return false | ||
| } | ||
| guard let breadcrumbJsonString = String(data: breadcrumbJsonData, encoding: .utf8) else { | ||
| BacktraceLogger.warning("Error when converting breadcrumb to string") | ||
| return false | ||
| } | ||
| let breadcrumbSize = breadcrumbJsonData.count | ||
| // Check if breadcrumb size is larger than the maximum specified | ||
| if breadcrumbSize > maximumIndividualBreadcrumbSize { | ||
| BacktraceLogger.warning( | ||
| "Discarding breadcrumb that was larger than the maximum specified (\(maximumIndividualBreadcrumbSize).") | ||
| return false | ||
| } | ||
| dispatchQueue.sync { | ||
| let queueBreadcrumb = BreadcrumbRecord(size: breadcrumbSize, json: breadcrumbJsonString) | ||
| queue.enqueue(queueBreadcrumb) | ||
| let queuedBreadcrumbs = queue.allElements() | ||
| let queueSize = queue.count | ||
| var breadcrumbsArray = [String]() | ||
| var size = 0 | ||
| for index in (0..<queueSize).reversed() { | ||
| guard index < queuedBreadcrumbs.count else { | ||
| continue | ||
| } | ||
| let queueBreadcrumb = queuedBreadcrumbs[index] | ||
| let breadcrumbSize = queueBreadcrumb.size | ||
| // Pop last element if size is greater than maxQueueFileSizeBytes | ||
| if size + breadcrumbSize > maxQueueFileSizeBytes && !queue.isEmpty { | ||
| while (index != 0) { | ||
| _ = queue.pop(at: index) | ||
| } | ||
| break | ||
| } | ||
| let breadcrumbJsonData = queueBreadcrumb.json | ||
| breadcrumbsArray.append(breadcrumbJsonData) | ||
| size += breadcrumbSize | ||
| } | ||
| let breadcrumbString = "[\(breadcrumbsArray.joined(separator: ","))]" | ||
| writeBreadcrumbToLogFile(breadcrumb: breadcrumbString, at: self.breadcrumbLogURL) | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| func clear() -> Bool { | ||
| dispatchQueue.sync { | ||
| queue.clear() | ||
| clearBreadcrumbLogFile(at:self.breadcrumbLogURL) | ||
| } | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| extension BacktraceBreadcrumbFile { | ||
|
|
||
| func writeBreadcrumbToLogFile(breadcrumb: String, at breadcrumbLogURL: URL) { | ||
| do { | ||
| try breadcrumb.write(to: breadcrumbLogURL, atomically: true, encoding: .utf8) | ||
| } catch { | ||
| BacktraceLogger.warning("Error writing breadcrumb to log file at: \(breadcrumbLogURL) - \(error.localizedDescription)") | ||
| } | ||
| } | ||
|
|
||
| func clearBreadcrumbLogFile(at breadcrumbLogURL: URL) { | ||
| do { | ||
| try "".write(to: breadcrumbLogURL, atomically: false, encoding: .utf8) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if someone deletes a file when you're generating breadcrumbs? |
||
| } catch { | ||
| BacktraceLogger.warning("Error clearing breadcrumb log file at: \(breadcrumbLogURL) - \(error.localizedDescription)") | ||
| } | ||
| } | ||
| } | ||
113 changes: 0 additions & 113 deletions
113
Sources/Features/Breadcrumb/BacktraceBreadcrumbFileHelper.swift
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import Foundation | ||
|
|
||
| struct BreadcrumbRecord { | ||
| let size: Int | ||
| let json: String | ||
|
|
||
| init(size: Int, json: String) { | ||
| self.size = size | ||
| self.json = json | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import Foundation | ||
|
|
||
| @objcMembers | ||
| public class Queue<T>: NSObject { | ||
| private var elements: [T] = [] | ||
|
|
||
| func enqueue(_ element: T) { | ||
| elements.append(element) | ||
| } | ||
|
|
||
| func dequeue() -> T? { | ||
| if elements.isEmpty { | ||
| return nil | ||
| } else { | ||
| return elements.removeFirst() | ||
| } | ||
| } | ||
|
|
||
| func peek() -> T? { | ||
| return elements.first | ||
| } | ||
|
|
||
| func remove(at index: Int) -> T? { | ||
| guard index < elements.count else { | ||
| return nil | ||
| } | ||
| return elements.remove(at: index) | ||
| } | ||
|
|
||
| func pop(at index: Int) -> T? { | ||
| guard !elements.isEmpty else { | ||
| return nil | ||
| } | ||
| return remove(at: index) | ||
| } | ||
|
|
||
| func element(at index: Int) -> T? { | ||
| guard index >= 0 && index < elements.count else { | ||
| return nil | ||
| } | ||
| return elements[index] | ||
| } | ||
|
|
||
| func pop() -> T? { | ||
| guard !elements.isEmpty else { | ||
| return nil | ||
| } | ||
| return elements.popLast() | ||
| } | ||
|
|
||
| public func allElements() -> [T] { | ||
| return elements | ||
| } | ||
|
|
||
| func clear() { | ||
| elements.removeAll() | ||
| } | ||
|
|
||
| var isEmpty: Bool { | ||
| return elements.isEmpty | ||
| } | ||
|
|
||
| var count: Int { | ||
| return elements.count | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if there's any superfluous
#if os(iOS) || os(OSX)left in prod/test code. I know there were a bunch of 'mThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take a look :)