GithubHelp home page GithubHelp logo

wetransfer / diagnostics Goto Github PK

View Code? Open in Web Editor NEW
938.0 14.0 54.0 3.94 MB

Allow users to easily share Diagnostics with your support team to improve the flow of fixing bugs.

License: MIT License

Objective-C 0.43% Swift 94.73% CSS 2.85% Ruby 0.64% JavaScript 1.34%
swift wt-branch-protection-two-approvals wt-branch-protection-exempt

diagnostics's Introduction

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
  • Possibility to filter out sensitive data using a DiagnosticsReportFilter
  • A custom DiagnosticsLogger to add your own logs
  • Smart insights like "⚠️ User is low on storage" and "✅ User is using the latest app version"
  • Flexible setup to add your own smart insights
  • Flexible setup to add your own custom diagnostics
  • Native cross-platform support, e.g. iOS, iPadOS and macOS

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(["[email protected]"])
        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)
    }
}

On macOS you could send the report by using the NSSharingService:

import AppKit
import Diagnostics

func send(report: DiagnosticsReport) {
    let service = NSSharingService(named: NSSharingService.Name.composeEmail)!
    service.recipients = ["[email protected]"]
    service.subject = "Diagnostics Report"
            
    let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("Diagnostics-Report.html")
    
    // remove previous report
    try? FileManager.default.removeItem(at: url)

    do {
        try report.data.write(to: url)
    } catch {
        print("Failed with error: \(error)")
    }

    service.perform(withItems: [url])
}

Using a UserDefaultsReporter

In order to use UserDefaultsReporter, you need to specify the desired UserDefaults instance together with all the keys you would like to read, and use it in DiagnosticsReporter.create(filename:using:filters:smartInsightsProvider) to create a DiagnosticsReport.

let userDefaultsReporter = UserDefaultsReporter(
    userDefaults: UserDefaults(suiteName: "a.userdefaults.instance"),
    keys: ["key_1"]
)

let diagnosticsReport = DiagnosticsReporter.create(using: [userDefaultsReporter])

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 a directory tree report

It's possible to add a directory tree report for a given set of URL, resulting in the following output:

└── Documents
    +-- contents
    |   +-- B3F2F9AD-AB8D-4825-8369-181DEAAFF940.png
    |   +-- 5B9C090E-6CE1-4A2F-956B-15897AB4B0A1.png
    |   +-- 739416EF-8FF8-4502-9B36-CEB778385BBF.png
    |   +-- 27A3C96B-1813-4553-A6B7-436E6F3DBB20.png
    |   +-- 8F176CEE-B28F-49EB-8802-CC0438879FBE.png
    |   +-- 340C2371-A81A-4188-8E04-BC19E94F9DAE.png
    |   +-- E63AFEBC-B7E7-46D3-BC92-E34A53C0CE0A.png
    |   +-- 6B363F44-AB69-4A60-957E-710494381739.png
    |   +-- 9D31CA40-D152-45D9-BDCE-9BB09CCB825E.png
    |   +-- 304E2E41-9697-4F9A-9EE0-8D487ED60C45.jpeg
    |   └── 7 more file(s)
    +-- diagnostics_log.txt
    +-- Okapi.sqlite
    +-- Library
    |   +-- Preferences
    |   |   └── group.com.wetransfer.app.plist
    |   └── Caches
    |       └── com.apple.nsurlsessiond
    |           └── Downloads
    |               └── com.wetransfer
    +-- Coyote.sqlite-shm
    +-- Coyote.sqlite
    +-- Coyote.sqlite-wal
    +-- Okapi.sqlite-shm
    +-- Okapi.sqlite-wal
    └── 1 more file(s)

You can do this by adding the DirectoryTreesReporter:

var reporters = DiagnosticsReporter.DefaultReporter.allReporters
let documentsURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let directoryTreesReporter = DirectoryTreesReporter(
    directories: [
        documentsURL
    ]
)
reporters.insert(directoryTreesReporter, at: 1)

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)

Smart Insights

By default, standard Smart Insights are provided:

  • UpdateAvailableInsight uses your bundle identifier to fetch the latest available app version. An insight will be shown whether an update is available to the user or not.
  • DeviceStorageInsight shows whether the user is out of storage or not

Adding your own custom insights

It's possible to provide your own custom insights based on the chapters in the report. A common example is to parse the errors and show a smart insight about an occurred error:

struct SmartInsightsProvider: SmartInsightsProviding {
    func smartInsights(for chapter: DiagnosticsChapter) -> [SmartInsightProviding] {
        guard let html = chapter.diagnostics as? HTML else { return [] }
        if html.errorLogs.contains(where: { $0.contains("AppDelegate.ExampleLocalizedError") }) {
            return [
                SmartInsight(
                    name: "Localized data",
                    result: .warn(message: "An error was found regarding missing localisation.")
                )
            ]
        }
        return []
    }
}

The example project provides the above sample code for you to try out. You can make use of html.errorLogs, .debugLogs, and .systemLogs to quickly access specific logs from the report.

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

Swift Package Manager

The Swift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.

Manifest File

Add Diagnostics as a package to your Package.swift file and then specify it as a dependency of the Target in which you wish to use it.

import PackageDescription

let package = Package(
    name: "MyProject",
    platforms: [
       .macOS(.v10_15)
    ],
    dependencies: [
        .package(url: "https://github.com/WeTransfer/Diagnostics.git", .upToNextMajor(from: "1.8.0"))
    ],
    targets: [
        .target(
            name: "MyProject",
            dependencies: ["Diagnostics"]),
        .testTarget(
            name: "MyProjectTests",
            dependencies: ["MyProject"]),
    ]
)

Xcode

To add Diagnostics as a dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter the repository URL: https://github.com/WeTransfer/Diagnostics.git.

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 folder into the Project Navigator of your application's Xcode project. This will add the SPM package as a local package.

    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.

  • 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.

diagnostics's People

Contributors

amirdew avatar antranapp avatar avdlee avatar davidsteppenbeck avatar dilipptdr avatar edorphy avatar entunidotdeb avatar intitni avatar juliankahnert avatar kairadiagne avatar mariusc avatar mikakruschel avatar mikemee avatar pgorrindo avatar raphkoebraam avatar rubenfer avatar sairamkotha avatar wetransferplatform avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

diagnostics's Issues

About assertionFailure

Is there necessary to use assertionFailure on unexpected condition at DiagnosticLogger.swift. May be use print more friendly.

Add support for users without the Apple Mail app

If you don't have the Apple Mail app installed, the MFMailComposeViewController does nothing and reporting an issue might not work.

For implementers of this framework, it would be really useful to have an easy way of presenting an email with support for 3rd party mail clients as well.

Diagnostics.xcodeproj is Missing

I'm trying to update to the latest version of this library but the Diagnostics.xcodeproj file is missing from the repo. Was this deliberate? If so can the installation instructions be updated please?

Crash when trying to load the CSS

I added this framework to a demo app, and I am getting the following crash

        let bundle = Bundle(for: DiagnosticsLogger.self)
        let cssFile = bundle.url(forResource: "style", withExtension: "css")! // <- crash
        let css = try! String(contentsOf: cssFile, encoding: .utf8).minifiedCSS()

How to replicate the crash:
1- Create an iOS app
2- Use CocoaPods
3- just use the template code in the Readme

Build Reporter inside a closure

Hi!

I'm basically struggling for this issue, this is maybe a feature.

I'm trying to build a new DiagnosticsChapter which would call an Endpoint of my API and have it on its own for the report.

Do you have an input?

Best regards,
Romain

[Reports] Smart reporting

be smart about reporting what is likely happening. E.g.: “This might be caused because the user user was low on storage” or “This error happened in the user’s latest session: ”

Ideas for Transfer apps:

  • this might be a stretch, but is it possible to be able to see if the user closed the app during upload? or just closed/exited it at all?
  • Upload failure reasons

Create a common structure to report diagnostics in

It would be really nice to add all kinds of logs to the diagnostics. Each app is different and needs to report different stuff.

Features to think of:

  • Create a new chapter of data
  • Add a dictionary of key-values to log

Support for Apple Watch App Logs

Hi,

I'm starting to use this library in my app and I think it's pretty sweet! I extended the Diagnostic Logger piece to also support Apple Watch app logs by writing a log file on the watch and periodically transferring the file over to the iPhone, where it can be included in a report alongside the iPhone logs.

Is that something ya'll would want me to put up a PR for? Just figured I would ask first.

Thanks,

  • Conrad

Support for iOS 10.0

Thanks for open sourcing Diagnostics.
Any specific reason for making it compatible for iOS 11 above?
I cloned and made minimum target as iOS 10.0 & I found only one error it is sortedKeys for json.
Do you mind if I raise PR to support for iOS 10 & above?

Only Include Subset of Logs

Hi there, loving using the library, but had a question about how to provide less data. Every time a user sends me their diagnostics, they're sending me diagnostics for every session they've ever had. This makes parsing the report cumbersome and unnecessarily large.

I was wondering if it'd be possible to have an option to receive only the last N session logs, via some sort of API in Diagnostics.

Thanks a lot!

Question: Workaround for users who don't use Apple Mail

This is not a bug report but rather a question related to your project where I'm interested if you, or anyone else reading this, found a good workaround.

We are also using MFMailComposeViewController in our app to let users send feedback. However, we ran into the issue that when you don't use Apple Mail (I don't) you cannot use MFMailComposeViewController. canSendMail will return false in this case.

We will now probably just expose an endpoint in our backend to receive user feedback and don't use the builtin mail composer. However, we would prefer to stick to the current solution. Since this project is also heavily impacted by this, I would be curious if you also ran into this issue and might have a creative idea how to get around it.

Crash happening when there is no space left on the device

*** -[NSConcreteFileHandle writeData:]: No space left on device

This is happening inside the DiagnosticsLogger on line 146, inside the log method. This method can be called from many places.

We basically need to check whether there's enough space left to write the logs.

Create default logs to add

As a start, it would be great to have a basic logging approach. Simply add the framework and call the method for creating the email and the HTML report should already contain basic stuff.

Things to include:

  • Device information
  • UserDefaults
  • Crash information
  • Reported errors
  • App metadata
  • App name as title of the HTML file

Ideally, it would be possible to opt-out for specific things using an enum in the configuration file.

Encode Logging for HTML so object descriptions are visible

Would be useful to include object description strings in the logs, but it seems they aren't logged when passed as part of the message argument in the static func DiagnosticsLogger.log(message:).

For example, if I try to log self.description where self is of type UIViewController by calling DiagnosticsLogger.log(message: "This is the view controller description: \(self.description).") what I see in the log is something like "2020-01-27 16:00:00 | ViewController.swift:L100 | This is the view controller description: ."

Include device product names

I thought it might be convenient to include something that contains device product names so what appears in the final report is more understandable - for example, we'd see:

Device iPhone12,1 (iPhone 11)

rather than just:

Device iPhone12,1

in the attached HTML file. Would probably be enough to just include the most recent iPhones for now, to keep it compact. Devices not included would just display as they are currently.

Support different platforms/APIs

👋 Love the idea of this project.

I was wondering would you be open to the idea of support sending these via other channels other than email?

I was thinking maybe supporting a pluggable API to support different APIs for reporting issues.

Happy to explore the idea and open a PR but wanted to see if it was inline with your vision for this?

Is it possible to separate Custom Logs & System Logs

Hi Guys,

As far as I have tried there is no way to separate the custom log messages which are clubbed together in automatically created logs.

So is it possible to create a separate report for the Custom Logs ? As the "SYSTEM:" prefixed logs have too much debug prints from different frameworks that I don't want.

I also tried the filtering for the report, but the logs report is just on big string.

Let me know if I missing something that I haven't tried 😅

No public read access to var `DiagnosticsLogger.standard.isSetup`

There is currently no public read access to var DiagnosticsLogger.standard.isSetup (file DiagnosticsLogger.swift line 40), which could be useful when developing and debugging. I suggest adding public read access using:

/// Whether the logger is setup and ready to use.
public static func isSetUp() -> Bool {
    return standard.isSetup
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.