Android Code Snippet Parsing your apps logs

Similar threads

Other B4A v5.80 BETA is released!
B4A Question LogCat in Phone Library
B4A Tutorial Android JSON tutorial
B4A Question Phone LogCat
B4A Tutorial Text files


Staff member
Licensed User
It is possible to access your apps logs with LogCat from the Phone library.
Note that it doesn't require any permission however you cannot use it to read the logs of other apps.
The LogCatData event is raised on a different thread. This makes it a bit more difficult to work with this event if you want to process the logs.

The following code shows how to collect the logs with StringBuilder and how to use a timer to parse them:
'Starter service
Sub Process_Globals
   Private LogCat As LogCat
   Public LogBuffer As StringBuilder
End Sub

Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
   Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
   #if RELEASE
   'only start it in release mode. Debug mode doesn't support running code on background threads
   LogCat.LogCatStart(Array As String(), "LogCat")
   #end if
End Sub

Sub LogCat_LogCatData (Buffer() As Byte, Length As Int)
   Dim b(Length) As Byte
   Bit.ArrayCopy(Buffer, 0, b, 0, Length)
   CallSubDelayed2(Me, "LogCatData", b)
End Sub

Sub LogCatData(buffer() As Byte)
       LogBuffer.Append(BytesToString(buffer, 0, buffer.Length, "utf8"))
       If LogBuffer.Length > 5000 Then
           LogBuffer.Remove(LogBuffer.Length - 4000, LogBuffer.Length)
       End If
   End Try
End Sub

'Main activity:
Sub Process_Globals
   Private Timer1 As Timer
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
       Timer1.Initialize("Timer1", 500)
   End If
End Sub

Sub Timer1_Tick
   Dim logs As String = Starter.LogBuffer.ToString
   If logs.Contains("Pause") Then
       Starter.LogBuffer.Remove(0, Starter.LogBuffer.Length)
   End If
End Sub

Sub Activity_Resume
   Timer1.Enabled = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   Timer1.Enabled = False
End Sub


Active Member
Licensed User
is it possible to print the log at a particular line e.g instead of
we print the exact line because the logs contains several lines and in my case i only want a particular line


Well-Known Member
Licensed User
Something like this?
Dim BeginIndex As Int
BeginIndex = logs.IndexOf("Pause")
If BeginIndex > -1 Then
End If