B4i Library KSCrash - simple and powerful crash reports framework

Erel

Administrator
Staff member
Licensed User
KSCrash is an open source crash reports framework: https://github.com/kstenerud/KSCrash

It is simple to use and looks like it works very good.
The app collects the crash reports and tries to send them when the app is started again. There are several ways to send the crash reports, including sending to a http server and sending an email.

The following code uses the email method.

1595924652055.png

The report includes a stack trace with the sub names.

How to use?

1.
B4X:
#AdditionalLib: KSCrash.framework.3
#AdditionalLib: MessageUI.framework
#AdditionalLib: SystemConfiguration.framework
#AdditionalLib: libc++.dylib
#AdditionalLib: libz.dylib
2.
In Process_Globals:
B4X:
Private xui As XUI
Private reporter As NativeObject
3. In Application_Start:
B4X:
#if RELEASE
If App.IsSimulator = False Then
    CreateReporter
    SendReportsIfNeeded
End If
#end if
As you can see in the code above, it will only be activated in release mode on a real device.

4.
B4X:
Sub SendReportsIfNeeded As ResumableSub
    Dim no As NativeObject
    no = no.Initialize("KSCrash").RunMethod("sharedInstance", Null)
    Dim reports As Int = no.GetField("reportCount").AsNumber
    Log($"Number of reports: ${reports}"$)
    'Page1.Title = reports
    If reports > 0 Then
        Sleep(0)
        Dim sf As Object = xui.Msgbox2Async("The app crashed last time it was launched. Please help us improve and send a crash report?", _
            "", "Yes", "No", "", Null)
        Wait For (sf) Msgbox_Result (Result As Int)
        If Result = xui.DialogResponse_Positive Then
            Dim nme As NativeObject = Me
            nme.RunMethod("sendReports:", Array(reporter))
        Else
            no.RunMethod("deleteAllReports", Null)
        End If
    End If
    Return True
End Sub

Sub CreateReporter
    reporter = reporter.Initialize("KSCrashInstallationEmail").RunMethod("sharedInstance", Null)
    Dim recipients As List = Array("ereluziel@gmail.com")
    reporter.SetField("recipients", recipients)
    reporter.SetField("reportStyle", 1) 'KSCrashEmailReportStyleApple
    reporter.SetField("subject", "Crash Report")
    reporter.SetField("message", "This Is a crash report")
    reporter.SetField("filenameFmt", "crash-report-%d.txt.gz")
    reporter.RunMethod("install", Null)
End Sub

#if OBJC
#import <KSCrash/KSCrashFramework.h>

- (void) sendReports:(KSCrashInstallation*)installation {
[installation sendAllReportsWithCompletion:^(NSArray* reports, BOOL completed, NSError* error)
     {
         if(completed)
         {
             NSLog(@"Sent %d reports", (int)[reports count]);
         }
         else
         {
             NSLog(@"Failed to send reports: %@", error);
         }
     }];
}
#End If
If you are using a local Mac then you need to download the framework and copy it to the libs folder: www.b4x.com/b4i/files/KSCrash.zip
 
Last edited:

Sandman

Well-Known Member
Licensed User
As you can see in the code above, it will only be activated in release mode on a real device
For development and testing purposes, would it still work as expected if one remove the #if RELEASE?
 

Andrew (Digitwell)

Active Member
Licensed User
A minor documentation update,

you need to define
B4X:
Private reporter as nativeobject
private xui as xui
in the Process_globals
 

Erel

Administrator
Staff member
Licensed User

CaptKronos

Active Member
Licensed User
Just tested and I received the expected crash report via email.:)
I understand the reports can be configured by modifying the KSCrash.h file. Can anyone offer some guidance on a configuration that would work well for B4i apps?
 

CaptKronos

Active Member
Licensed User
Yes, the default template is very useful. I was just wondering whether any of the other optional settings in KSCrash.h would be relevant to a b4i app.
 
Top