Android Question Is it possible to log Modle, Sub and error line of unhandled runtime error?

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Currently doing a simple error log for unhandled runtime errors:

B4X:
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
 
 LogError(Error.Message)
 Return True
 
End Sub

Sub LogError(strErrorMsg As String)
 
 Dim str As String
 Dim strCurrentDateTime As String
 Dim strDateFormat As String
 
 'keep the current date format
 strDateFormat = DateTime.DateFormat
 DateTime.DateFormat = "EEE, d/MMM/yyyy HH:mm:ss"
 strCurrentDateTime = DateTime.Date(DateTime.Now)
 
 str = File.ReadString(strAppDir, "AppLog.txt")
 
 If str.Length = 0 Then
  File.WriteString(strAppDir, "AppLog.txt", strCurrentDateTime & CRLF & strErrorMsg)
 Else
  File.WriteString(strAppDir, "AppLog.txt", str & CRLF & CRLF & strCurrentDateTime & CRLF & strErrorMsg)
 End If
 
 DateTime.DateFormat = strDateFormat
 
End Sub

Is it possible to add some more information, eg module, sub and error line?

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User

Thanks, will keep that in mind.
I think for now I can do it simpler by just parsing out the error Sub from the StackTrace as in
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean:

B4X:
Sub GetErrorSubFromStackTrace(strStackTrace As String) As String
 Dim iPos1 As Int
 Dim iPos2 As Int
 iPos1 = strStackTrace.IndexOf("b4a.sqlitelight1.") + 17
 iPos2 = strStackTrace.IndexOf2("(", iPos1)
 Return strStackTrace.SubString2(iPos1, iPos2).Replace("_", "")
End Sub

I take it it is not possible to get the B4A error line, from runtime data, unless you log every code line that runs,
but that would be too much trouble.

RBS
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
iPos1 = strStackTrace.IndexOf("b4a.sqlitelight1.") + 17

What is this reference to sqlitelight1 for?

Is this GetErrorSubFromStackTrace routine custom designed for you app and that's why it includes the sqlitelight1 reference?

Or is this reference needed for use with any b4a app?
 
Upvote 0
Top