Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to stream logcat output line by line ? #88

Answered by wherewhere
graysuit asked this question in Q&A
Discussion options

I want to stream logs line by line and close when I got specific line.

Currently, I manually open adb executable, but disadvantage, it kills adbclient.

Dim info As New ProcessStartInfo With {
 .FileName = AdbPath, 'Path.Combine(AdbPath, "adb.exe"),
 .Arguments = "-s emulator-5555 shell logcat -s LogInterceptor:V",
 .UseShellExecute = False,
 .RedirectStandardOutput = True,
 .CreateNoWindow = True
}
Dim P = Process.Start(info)
Using reader = P.StandardOutput
 While Not reader.EndOfStream
 Dim Line = reader.ReadLine()
 End While
End Using

How can I implement in AdvancedSharpAdbClient instead ?
Something like

Public Function RunAdb(client As AdbClient, device As DeviceData, cmd As String) As String
 Dim receiver As New ConsoleOutputReceiver()
 client.ExecuteRemoteCommand(cmd, device, receiver)
 Dim Out = receiver.ToString() 'receiver.ReadLine ?
 receiver.Flush()
 Return Out
End Function
You must be logged in to vote

It cannot use filter because the filter is not supported on logcat -B. You can filter it by yourself.

Replies: 4 comments 8 replies

Comment options

Comment options

how to stop receiving the logs

Comment options

CancellationToken cancellationToken

Comment options

AdbClient.RunLogServiceAsync(DeviceData device, Action<LogEntry> messageSink, CancellationToken cancellationToken, params LogId[] logNames)

Dim Client = New AdbClient()
Dim device = Client.GetDevices().FirstOrDefault()
Dim sink = Sub(L As LogEntry)
 Console.WriteLine(L)
End Sub
Client.RunLogService(device, sink, LogId.System)
Console.ReadLine()

Okay this works, it shows logging. Now two questions:

  1. Is there way I can change arguments ? e.g logcat -s LogInterceptor:V
  2. How to stop ?
You must be logged in to vote
1 reply
Comment options

It cannot use filter because the filter is not supported on logcat -B. You can filter it by yourself.

Answer selected by graysuit
Comment options

@wherewhere I pasted sample code for future reference.
It suppose to be like that, right ?

Sub WaitForServer()
Dim Source As New CancellationTokenSource()
Dim Token As CancellationToken = Source.Token
Dim sink = Sub(Log As Object)
 If Log.Tag.ToString() = "LogInterceptor" Then
 If Log.Message.Contains("Response body") Then
 Console.WriteLine(Log.Message)
 Source.Cancel()
 End If
 End If
End Sub
Dim Service = Client.RunLogServiceAsync(Device, sink, Token, LogId.Crash, LogId.Events, LogId.Main, LogId.Radio, LogId.System)
Service.Wait()
End Function

Thanks so much for the support. Cheers!

You must be logged in to vote
5 replies
Comment options

Not wait a Task, make async sync may cause thread locked...
In lester version I have add a in bool to notify cancellation in sync version. You can try it from GitHub Package.

Comment options

I see isCancelled bool. Is it replacement to cancellation token, right ?
We use it like this ?

Sub WaitForServer()
Dim isCancelled As Boolean = False
Dim sink = Sub(Log As Object)
 If Log.Tag.ToString() = "LogInterceptor" Then
 If Log.Message.Contains("Response body") Then
 Console.WriteLine(Log.Message)
 isCancelled = True
 End If
 End If
End Sub
Client.RunLogService(Device, sink, isCancelled, LogId.Crash, LogId.Events, LogId.Main, LogId.Radio, LogId.System)
End Function
Comment options

Is that isCanceled doesn't need in?

Comment options

I didn't tested, as not sure where to download that package ?
But most of times, vb net takes care itself. Like variable Dim VarA As String = 1 will automatically converted to string.
So if I remember correctly, vb net itself reference, when in/ref variable found.

Comment options

https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-nuget-registry#installing-a-package
It is so difficult to download nuget though GitHub Package. You can directly download the .nuget uploaded in action and use it as local nuget.

Comment options

Now you can using foreach to enumerate the output lines by IEnumerable<string> ExecuteRemoteCommand(string command, DeviceData device, Encoding encoding)
https://github.com/SharpAdb/AdvancedSharpAdbClient/blob/main/AdvancedSharpAdbClient%2FAdbClient.cs#L408-L507

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
help wanted Extra attention is needed

AltStyle によって変換されたページ (->オリジナル) /