Android Question Anyway to save all errors?

hibrid0

Active Member
Licensed User
Longtime User
Hi, I want to know if is possible to save all errors on my app.

I have one bad solution and is on every catch write string to file.
But I have my code too long and dont like try catch on all my code.

Any suggestions are accepted.
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This works fine for me, in the Starter Service:

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, StackTrace)
 Return True
 
End Sub

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

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

There are some Public/Private variables in the code, but you will get the idea.


RBS
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User

this will read always the complete log, true?

there is also
Exists & [URL='https://www.b4x.com/android/help/files.html#file_size']Size [/URL]
[URL='https://www.b4x.com/android/help/files.html#file_size'][URL='https://www.b4x.com/android/help/files.html#file_openoutput']OpenOutput with append[/URL][/URL]
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
[URL='https://www.b4x.com/android/help/files.html#file_exists']

> this will read always the complete log, true?

Yes, I thought it was simpler to read the file, concatenate the new string then write back.
I suppose if the file gets large then this could be somewhat inefficient, but I think this is dubious.
If you want to use OpenOutput then the code should be something like this:

B4X:
Sub LogError(strErrorMsg As String, strStackTrace As String)
 Dim str As String
 Dim strCurrentDateTime As String
 Dim strDateFormat As String
 Dim strSub As String
 Dim OP_Stream As OutputStream
 Dim arrBytes() As Byte
 'keep the current date format
 strDateFormat = DateTime.DateFormat
 DateTime.DateFormat = "EEE, d/MMM/yyyy HH:mm:ss"
 strCurrentDateTime = DateTime.Date(DateTime.Now)
 strSub = GetErrorSubFromStackTrace(strStackTrace)
 If File.Size(strAppDir, "AppLog.txt") = 0 Then
  str = strCurrentDateTime & CRLF & strErrorMsg & " in " & strSub
 Else
  str = CRLF & CRLF & strCurrentDateTime & CRLF & strErrorMsg & " in " & strSub
 End If
 arrBytes = str.GetBytes("UTF8")
 OP_Stream = File.OpenOutput(strAppDir, "AppLog.txt", True)
 OP_Stream.WriteBytes(arrBytes, 0, arrBytes.Length)
 OP_Stream.Flush
 OP_Stream.close
 DateTime.DateFormat = strDateFormat
End Sub


RBS
[/URL]
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i think it can be shorter a little bit.
B4X:
Sub LogError(strErrorMsg As String, strStackTrace As String)

    'keep the current date format
    Dim strDateFormat As String
    strDateFormat = DateTime.DateFormat
    DateTime.DateFormat = "EEE, d/MMM/yyyy HH:mm:ss"

    Dim strSub As String
    strSub = GetErrorSubFromStackTrace(strStackTrace)

    Dim arrBytes() As Byte
    arrBytes = (DateTime.Date(DateTime.Now) & CRLF & strErrorMsg & " in " & strSub & CRLF & CRLF).GetBytes("UTF8")

    Dim OP_Stream As OutputStream
    OP_Stream = File.OpenOutput(File.DirInternalCache, "AppLog.txt", True)
    OP_Stream.WriteBytes(arrBytes, 0, arrBytes.Length)
    OP_Stream.Flush
    OP_Stream.close

    DateTime.DateFormat = strDateFormat
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…