Skip to content

#5 log fatal #12

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

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface ExceptionLogger {
fun setCustomValue(value: String, forKey: String)
fun recordException(throwable: Throwable)
fun log(message: String)
fun logFatal(throwable: Throwable)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/

package dev.icerock.moko.crashreporting.core

import kotlin.native.concurrent.freeze

fun setupUnhandledExceptionsHandler(logger: ExceptionLogger) {
val hook: ReportUnhandledExceptionHook = { exc: Throwable ->
logger.logFatal(throwable = exc)
}
setUnhandledExceptionHook(hook.freeze())
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public protocol FirebaseCrashlyticsReporter {
func setUserId(userId: String)
func setCustomValue(value: String, forKey: String)
func recordException(name: String, reason: String, stackTrace: [UInt])
func recordFatalException(name: String, reason: String, stackTrace: [UInt])
func log(message: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class MokoFirebaseCrashlytics: FirebaseCrashlyticsReporter {
Crashlytics.crashlytics().record(exceptionModel: exceptionModel)
}

public func recordFatalException(
name: String,
reason: String,
stackTrace: [UInt]
) {
recordException(name: name, reason: reason, stackTrace: stackTrace)
}

public func log(message: String) {
Crashlytics.crashlytics().log(message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ package dev.icerock.moko.crashreporting.crashlytics
import cocoapods.MCRCDynamicProxy.FirebaseCrashlyticsReporterProtocol
import cocoapods.MCRCDynamicProxy.FirebaseDynamicProxy
import dev.icerock.moko.crashreporting.core.CrashReportingCore
import dev.icerock.moko.crashreporting.core.setupUnhandledExceptionsHandler
import dev.icerock.moko.crashreporting.core.ExceptionLogger
import dev.icerock.moko.crashreporting.core.getStackTrace
import kotlin.native.concurrent.freeze

actual class CrashlyticsLogger actual constructor() : ExceptionLogger {
private val reporter: FirebaseCrashlyticsReporterProtocol = FirebaseDynamicProxy.reporter()
?: throw IllegalStateException("MokoFirebaseCrashlytics.setup() should be called in swift before creating CrashlyticsLogger")

private val reporter: FirebaseCrashlyticsReporterProtocol

init {
reporter = FirebaseDynamicProxy.reporter()
?: throw IllegalStateException("MokoFirebaseCrashlytics.setup() should be called in swift before creating CrashlyticsLogger")
freeze()
setupUnhandledExceptionsHandler(this)
}

override fun log(message: String) {
reporter.logWithMessage(message)
}

@ExperimentalUnsignedTypes
override fun recordException(throwable: Throwable) {
val crashReportingCore = CrashReportingCore
val name = crashReportingCore.getExceptionName(throwable)
val stackTrace = crashReportingCore.getStackTrace(throwable)
val name = CrashReportingCore.getExceptionName(throwable)
val stackTrace = CrashReportingCore.getStackTrace(throwable)

reporter.recordExceptionWithName(
name = name,
Expand All @@ -34,6 +42,17 @@ actual class CrashlyticsLogger actual constructor() : ExceptionLogger {
)
}

override fun logFatal(throwable: Throwable) {
val name = CrashReportingCore.getExceptionName(throwable)
val stackTrace = CrashReportingCore.getStackTrace(throwable)

reporter.recordFatalExceptionWithName(
name = name,
reason = throwable.message.orEmpty(),
stackTrace = stackTrace
)
}

override fun setUserId(userId: String) {
reporter.setUserIdWithUserId(userId)
}
Expand Down
27 changes: 23 additions & 4 deletions sample/ios-app/TestProj.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
287627FC1F319065007FA12B /* Frameworks */,
287627FD1F319065007FA12B /* Resources */,
3002F1129AFDA8934843C420 /* [CP] Embed Pods Frameworks */,
49EA9D27252EE521005FC8CC /* Firebase */,
49EA9D27252EE521005FC8CC /* Firebase Application submit */,
45A70C682599BD3F005CF12F /* Firebase MultiPlatformLibrary submit */,
);
buildRules = (
);
Expand Down Expand Up @@ -193,7 +194,25 @@
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-TestProj/Pods-TestProj-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
49EA9D27252EE521005FC8CC /* Firebase */ = {
45A70C682599BD3F005CF12F /* Firebase MultiPlatformLibrary submit */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
name = "Firebase MultiPlatformLibrary submit";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/FirebaseCrashlytics/upload-symbols\" -gsp \"${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/GoogleService-Info.plist\" -p ios \"${DWARF_DSYM_FOLDER_PATH}/MultiPlatformLibrary/MultiPlatformLibrary.framework.dSYM\" --debug\n";
};
49EA9D27252EE521005FC8CC /* Firebase Application submit */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
Expand All @@ -204,14 +223,14 @@
"${DWARF_DSYM_FOLDER_PATH}/${DWARF_DSYM_FILE_NAME}/Contents/Resources/DWARF/${TARGET_NAME}",
"$(SRCROOT)/$(BUILT_PRODUCTS_DIR)/$(INFOPLIST_PATH)",
);
name = Firebase;
name = "Firebase Application submit";
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/FirebaseCrashlytics/run\"\n\"${PODS_ROOT}/FirebaseCrashlytics/upload-symbols\" -gsp \"${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/GoogleService-Info.plist\" -p ios \"${DWARF_DSYM_FOLDER_PATH}/MultiPlatformLibrary.framework.dSYM\" \n";
shellScript = "\"${PODS_ROOT}/FirebaseCrashlytics/run\" --debug\n";
};
B8C78C58767A89D2A1A202F0 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
Expand Down
36 changes: 36 additions & 0 deletions sample/ios-app/src/GoogleService-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CLIENT_ID</key>
<string>149963383979-cdc8ptbcfqdofkk5jfdm4ln8m7ngv6p8.apps.googleusercontent.com</string>
<key>REVERSED_CLIENT_ID</key>
<string>com.googleusercontent.apps.149963383979-cdc8ptbcfqdofkk5jfdm4ln8m7ngv6p8</string>
<key>API_KEY</key>
<string>AIzaSyBnehCN7AvIrtno7WGpCjyHfY3LQZ6f-3w</string>
<key>GCM_SENDER_ID</key>
<string>149963383979</string>
<key>PLIST_VERSION</key>
<string>1</string>
<key>BUNDLE_ID</key>
<string>dev.icerock.moko.sample.crashReporting-test</string>
<key>PROJECT_ID</key>
<string>moko-crash-report</string>
<key>STORAGE_BUCKET</key>
<string>moko-crash-report.appspot.com</string>
<key>IS_ADS_ENABLED</key>
<false></false>
<key>IS_ANALYTICS_ENABLED</key>
<false></false>
<key>IS_APPINVITE_ENABLED</key>
<true></true>
<key>IS_GCM_ENABLED</key>
<true></true>
<key>IS_SIGNIN_ENABLED</key>
<true></true>
<key>GOOGLE_APP_ID</key>
<string>1:149963383979:ios:70426b87b87dee8ae543ee</string>
<key>DATABASE_URL</key>
<string>https://moko-crash-report.firebaseio.com</string>
</dict>
</plist>
21 changes: 14 additions & 7 deletions sample/ios-app/src/Resources/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097.2" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="TTy-86-aNs">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="TTy-86-aNs">
<device id="retina4_0" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="16087"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17703"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -20,36 +20,43 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="16" translatesAutoresizingMaskIntoConstraints="NO" id="qsZ-Wz-dMp">
<rect key="frame" x="99.5" y="200" width="121" height="168"/>
<rect key="frame" x="99.5" y="177" width="121" height="214"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="g51-Bc-9ch">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="g51-Bc-9ch">
<rect key="frame" x="0.0" y="0.0" width="121" height="30"/>
<state key="normal" title="Set UserId"/>
<connections>
<action selector="onSetUserId" destination="TTy-86-aNs" eventType="touchUpInside" id="LhL-7x-YEN"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XRw-SQ-H8d">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="XRw-SQ-H8d">
<rect key="frame" x="0.0" y="46" width="121" height="30"/>
<state key="normal" title="Set Custom value"/>
<connections>
<action selector="onSetCustomValue" destination="TTy-86-aNs" eventType="touchUpInside" id="7fb-Ce-GY4"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8UR-0W-NqD">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8UR-0W-NqD">
<rect key="frame" x="0.0" y="92" width="121" height="30"/>
<state key="normal" title="Send random log"/>
<connections>
<action selector="onSendRandomLog" destination="TTy-86-aNs" eventType="touchUpInside" id="w1I-5Q-3bh"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Onk-Mv-gs5">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Onk-Mv-gs5">
<rect key="frame" x="0.0" y="138" width="121" height="30"/>
<state key="normal" title="Send Exception"/>
<connections>
<action selector="onSendException" destination="TTy-86-aNs" eventType="touchUpInside" id="4xM-Uv-Rjt"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="vWM-VU-NBo">
<rect key="frame" x="0.0" y="184" width="121" height="30"/>
<state key="normal" title="Send Fatal"/>
<connections>
<action selector="onSendFatal" destination="TTy-86-aNs" eventType="touchUpInside" id="oSF-a8-Ws7"/>
</connections>
</button>
</subviews>
</stackView>
</subviews>
Expand Down
12 changes: 11 additions & 1 deletion sample/ios-app/src/TestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import MultiPlatformLibrary

class TestViewController: UIViewController {

let testing = Testing()
var testing: Testing!

override func viewDidLoad() {
super.viewDidLoad()
testing = Testing()
}

@IBAction
func onSetUserId() {
Expand All @@ -28,4 +33,9 @@ class TestViewController: UIViewController {
func onSendException() {
testing.logException()
}

@IBAction
func onSendFatal() {
testing.logFatalException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ class Testing {
Napier.e(message = "test is not a number", tag = "Non fatal", throwable = e)
}
}

fun logFatalException() {
"test".toInt()
}
}