Skip to content

Commit

Permalink
Adds export option for devices logs
Browse files Browse the repository at this point in the history
Issue yagiz#85 Adding a new option to export all intercepted http requests and responses content from a selected device
  • Loading branch information
jonathan-gomes committed Feb 3, 2022
1 parent 763aadb commit 383012d
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 62 deletions.
4 changes: 4 additions & 0 deletions mac/Bagel/Base/Extensions/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import Cocoa
extension Date {

private static let readableFormat = "dd/MM/yyyy HH:mm:ss"
private static let readableLogFormat = "yyyyMMdd-HHmmss"
var readable: String {
return self.format(dateFormat: Date.readableFormat)
}
var readableLog: String {
return self.format(dateFormat: Date.readableLogFormat)
}

func format(dateFormat: String) -> String {

Expand Down
33 changes: 32 additions & 1 deletion mac/Bagel/Components/Devices/DeviceTableCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Cocoa
import macOSThemeKit

class DeviceTableCellView: NSTableCellView {

@IBOutlet weak var backgroundBox: NSBox!
@IBOutlet weak var deviceNameTextField: NSTextField!
@IBOutlet weak var deviceDescriptionTextField: NSTextField!
Expand Down Expand Up @@ -39,5 +39,36 @@ class DeviceTableCellView: NSTableCellView {
self.deviceNameTextField.textColor = ThemeColor.secondaryLabelColor
}
}
@IBAction func newLogFile(_ sender: NSButton) {
guard let packets = BagelController.shared.selectedProjectController?.selectedDeviceController?.packets else {return}
var exportString = ""
var fileName = "project.log"
for packet in packets {
if let requestInfo = packet.requestInfo,
let rawString = OverviewRepresentation(requestInfo: requestInfo, showResponseHeaders: true).rawString {
exportString = exportString + rawString + "\n\n"
}
if let projectName = BagelController.shared.selectedProjectController?.projectName,
let devicename = BagelController.shared.selectedProjectController?.selectedDeviceController?.deviceName {
fileName = "\(projectName)-\(devicename)-\(Date().readableLog).log"
}
}

let fileManager = FileManager.default
do {
let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
try Data(exportString.utf8).write(to: documentDirectory.appendingPathComponent(fileName))
showAlert(style: .informational, title: "Log file exported!", message:"Log file exported to your documents directory")
} catch {
showAlert(style: .warning, title: "Error", message:"An error occurred while trying to save the log file to your documents directory")
}
}

private func showAlert(style: NSAlert.Style, title: String, message: String){
let alert = NSAlert()
alert.messageText = title
alert.informativeText = message
alert.alertStyle = style
alert.runModal()
}
}
Loading

0 comments on commit 383012d

Please sign in to comment.