Swiftpack.co - Package - WeTransfer/Diagnostics

| Example mail composer | Example Report | | --- | --- | | | |

Diagnostics is a library written in Swift which makes it really easy to share Diagnostics Reports to your support team.

Features

The library allows to easily attach the Diagnostics Report as an attachment to the MFMailComposeViewController.

  • ☑ Integrated with the MFMailComposeViewController
  • ☑ Default reporters include:
    • App metadata
    • System metadata
    • System logs divided per session
    • UserDefaults
  • ☑ Possibility to filter out sensitive data using a DiagnosticsReportFilter
  • ☑ A custom DiagnosticsLogger to add your own logs
  • ☑ Flexible setup to add your own custom diagnostics

Usage

The default report already contains a lot of valuable information and could be enough to get you going.

Make sure to set up the DiagnosticsLogger as early as possible to catch all the system logs, for example in the didLaunchWithOptions:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    do {
        try DiagnosticsLogger.setup()
    } catch {
        print("Failed to setup the Diagnostics Logger")
    }
    return true
}

Then, simply show the MFMailComposeViewController using the following code:

import UIKit
import MessageUI
import Diagnostics

class ViewController: UIViewController {

    @IBAction func sendDiagnostics(_ sender: UIButton) {
        /// Create the report.
        let report = DiagnosticsReporter.create()

        guard MFMailComposeViewController.canSendMail() else {
            /// For debugging purposes you can save the report to desktop when testing on the simulator.
            /// This allows you to iterate fast on your report.
            report.saveToDesktop()
            return
        }

        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = self
        mail.setToRecipients(["support@yourcompany.com"])
        mail.setSubject("Diagnostics Report")
        mail.setMessageBody("An issue in the app is making me crazy, help!", isHTML: false)

        /// Add the Diagnostics Report as an attachment.
        mail.addDiagnosticReport(report)

        present(mail, animated: true)
    }

}

extension ViewController: MFMailComposeViewControllerDelegate {
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
}

Using a custom UserDefaults type

Simply set your user defaults instance by making use of:

UserDefaultsReporter.userDefaults = ..

Filtering out sensitive data

It could be that your report is containing sensitive data. You can filter this out by creating a DiagnosticsReportFilter.

The example project contains an example of this:

struct DiagnosticsDictionaryFilter: DiagnosticsReportFilter {

    // This demonstrates how a filter can be used to filter out sensible data.
    static func filter(_ diagnostics: Diagnostics) -> Diagnostics {
        guard let dictionary = diagnostics as? [String: Any] else { return diagnostics }
        return dictionary.filter { keyValue -> Bool in
            if keyValue.key == "App Display Name" {
                // Filter out the key with the value "App Display Name"
                return false
            } else if keyValue.key == "AppleLanguages" {
                // Filter out a user defaults key.
                return false
            }
            return true
        }
    }
}

Which can be used by passing in the filter into the create(..) method:

let report = DiagnosticsReporter.create(using: reporters, filters: [DiagnosticsDictionaryFilter.self])

Adding your own custom logs

To make your own logs appear in the logs diagnostics you need to make use of the DiagnosticsLogger.

/// Support logging simple `String` messages.
DiagnosticsLogger.log(message: "Application started")

/// Support logging `Error` types.
DiagnosticsLogger.log(error: ExampleError.missingData)

The error logger will make use of the localized description if available which you can add by making your error conform to LocalizedError.

Adding your own custom report

To add your own report you need to make use of the DiagnosticsReporting protocol.

/// An example Custom Reporter.
struct CustomReporter: DiagnosticsReporting {
    static func report() -> DiagnosticsChapter {
        let diagnostics: [String: String] = [
            "Logged In": Session.isLoggedIn.description
        ]

        return DiagnosticsChapter(title: "My custom report", diagnostics: diagnostics)
    }
}

You can then add this report to the creation method:

var reporters = DiagnosticsReporter.DefaultReporter.allReporters
reporters.insert(CustomReporter.self, at: 1)
let report = DiagnosticsReporter.create(using: reporters)

Creating a custom HTML formatter for your report

You can make use of the HTMLFormatting protocol to customize the way the HTML is reported.

Simply pass in the formatter into the DiagnosticsChapter initialiser:

DiagnosticsChapter(title: "UserDefaults", diagnostics: userDefaults, formatter: <#HTMLFormatting.Type#>)

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Diagnostics into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'Diagnostics', '~> 1.0.0'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Diagnostics into your Xcode project using Carthage, specify it in your Cartfile:

github "WeTransfer/Diagnostics" ~> 1.00

Run carthage update to build the framework and drag the built Diagnostics.framework into your Xcode project.

Manually

If you prefer not to use any of the aforementioned dependency managers, you can integrate Diagnostics into your project manually.

Embedded Framework

  • Open up Terminal, cd into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:

    $ git init
    
  • Add Diagnostics as a git submodule by running the following command:

    $ git submodule add https://github.com/WeTransfer/Diagnostics.git
    
  • Open the new Diagnostics folder, and drag the Diagnostics.xcodeproj into the Project Navigator of your application's Xcode project.

    It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.

  • Select the Diagnostics.xcodeproj in the Project Navigator and verify the deployment target matches that of your application target.

  • Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.

  • In the tab bar at the top of that window, open the "General" panel.

  • Click on the + button under the "Embedded Binaries" section.

  • Select Diagnostics.framework.

  • And that's it!

    The Diagnostics.framework is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.


Release Notes

See CHANGELOG.md for a list of changes.

Authors

This library is created as part of the WeTransfer Hackathon. Process has been reported on Twitter.

Thanks to:

Also, a little shoutout to 1Password for inspiring us to create this library.

License

Diagnostics is available under the MIT license. See the LICENSE file for more info.

Github

link
Stars: 405

Dependencies

Used By

Total: 0

Releases

1.4.0 - 2020-01-28 16:37:47

  • Migrate to Bitrise & Danger-Swift (#52) via @AvdLee
  • Add charset utf-8 to html head (#50) via @AvdLee

1.3.0 - Bugfixes & iOS 10 support - 2020-01-03 13:05:28

  • Make sure to check whether there's enough space left to save new logs (#37)
  • Added public static method DiagnosticsLogger.isSetUp() (#41)
  • Changed log times to use 24H clock (#40)
  • Support for iOS 10.0 and update package.swift & Diagnostics.podspec to minimum deployment target iOS 10.0. #42 #47

1.2.0 - Re-enable default logging - 2019-12-18 10:12:38

This release implements the forwarding of the output of the DiagnosticsLogger to not disable default logging. Previous versions would catch all logging and disabled logging in, for example, Xcode.

It could also be that your test reports with tools like Danger didn't work anymore. Updating to this version should fix that as well!

1.1.0 - Filtering sensitive data - 2019-12-12 10:42:26

  • Added an HTMLFormatting type for custom formatting HTML inside reports
  • Added a DiagnosticsReportFilter type to filter out senstive data (#17, #19)
  • Dictionaries are now shown as Tables in the HTML report
  • You can now set a custom UserDefaults for the report by using UserDefaultsReporter.userDefaults = ..

1.0.2 - SPM Fix - 2019-12-09 14:00:22

  • Tests are no longer failing on CI (#18)
  • Remove the CSS resource file for SPM support (#28)
  • Saving to desktop makes sure the folder exists

1.0.1 - SPM & CocoaPods fixes - 2019-12-06 13:19:42

  • Fixed SPM build errors (#15)
  • Fixed CocoaPods build errors by including CSS file (#21)

1.0.0 - Initial Release - 2019-12-06 13:19:01

The initial release of Diagnostics 🎉