'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
this will read always the complete log, true?This works fine for me, in the Starter Service:
B4X:Sub LogError(strErrorMsg As String, strStackTrace As String) str = File.ReadString(strAppDir, "AppLog.txt")
[URL='https://www.b4x.com/android/help/files.html#file_exists']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_exists'][URL='https://www.b4x.com/android/help/files.html#file_openoutput']OpenOutput with append[/URL][/URL]
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
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