Android Question Problem with LogCatData event

mauro vicamini

Active Member
Licensed User
Longtime User
Hi Everyone,
I've a problem with LogCat event.
I start the logcat using

B4X:
objLog.LogCatStart(Args, "objLog")

then in the objLog_LogCatData I would catch the logdata

B4X:
Sub objLog_LogCatData (Buffer() As Byte, Length As Int)
      Dim data As String
   data = BytesToString(Buffer,0,Length,"UTF-8")
   ' DO SOMTHING WITH DATA
End Sub

But when i start LogCatStart, I get the following error

Ignoring event: objlog_logcatdata. Raised from the wrong thread.


The goal for me is to log the errors raised from a Javascript console.log command in a webview, but when the javascript console.log is fired I get the above error even if I read the console.log string on the IDE Log window (so that mean that the setup of webchromeclient and webviewextra stuff is correct).

I've read in a post in the forum that the logcat event runs in a different thread, and that the solution is to use the Threading Library and that is needed to only declare and initialized a thread object to making it work. I've done that but the problem still remain .

Pls, someone could post an example on how to catch data from the LogCatData events correctly?

Many thanks.
 

mauro vicamini

Active Member
Licensed User
Longtime User
Hi Erel!
Thanks for your replay.
I'm using B4A 3.0.
I've tested a little application that write on a file the data arriving from logcat it in Relese mode and seems to work( i say "seems" because I catch a lot of messages but not the one related of my application, maybe I've not cathed for enough time).
But in this little application I've don't used the threading library. Is It needed or not?
I see that in the logcat data arrive a lot of messages not related to the application: is it possible to filter the messages capturing only messages related to the application?
For clearing any doubts could you pls post a little example with the correct use of logcat library?
If it could be usefull I post my one:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim objLog As LogCat
    Dim Args(1) As String
    Dim Button1 As Button
    Dim Button2 As Button
    Dim Button3 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub objLog_LogCatData (Buffer() As Byte, Length As Int)
   Dim data As String
   data = BytesToString(Buffer,0,Length,"UTF-8")
   WriteIni("data", data,File.DirRootExternal,"logcatxxx.ini")

End Sub
Sub WriteIni(mykey As String,myvalue As String,fPath As String,filename As String)
    Dim Map1 As Map
    Map1.Initialize
   If File.Exists(fPath,filename) Then
   Else
      File.WriteMap(fPath, filename, Map1)   'to create it if not exist
   End If
   Map1 = File.ReadMap(fPath,filename)
            Map1.Put(mykey, myvalue)
    File.WriteMap(fPath, filename, Map1)
End Sub

Sub Button1_Click
    objLog.LogCatStart(Args,"objLog")
End Sub
Sub Button2_Click
    Log("HELLO!")
End Sub
Sub Button3_Click
    objLog.LogCatStop
End Sub

Another question : Javascrit messages sent with console.log from a webview are marked with the same ID of the application that contains the webview?

Thanks a lot again for your replay.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You don't need to use the Threading library. Just make sure to remember that unlike all other events in B4A, this event is raised on a different thread.

Your code is very inefficient. You should use an TextWriter to append the data to an already open file and call flush from time to time.

LogCat will only capture messages from your app on Android 4.1+.
The optional arguments are documented here: http://developer.android.com/tools/help/logcat.html
 
Upvote 0

mauro vicamini

Active Member
Licensed User
Longtime User
Thanks a lot Erel.
I know that my code is inefficent but was only a quick example to see if I could catch the messages from logcat. I will consider your advice.
Bye.
 
Upvote 0
Top