Search code examples
androidudplogcat

Read logs from Logcat without killing the tablet


I have to read the logs from Logcat and send them to a server through UDP.

For this task I have used this code: https://github.com/chemik/logcatudp

The main problem of that code is that the async Thread that is launched enters a while(true) loop that drains the tablet's battery on the long run.

Is there a way to get the logs in real time but without using a busy wait like that? Hopefully without adding some sleep(some_milliseconds) to reduce the problem?

It would be great to use some sort of event listener but I haven't found one. I have searched in every similar library but without any success.

The code is the following:

while (true) {
    String sendingLine = "";
    // assume that log writes whole lines
    if (bufferedReader.ready()) {
        logLine = bufferedReader.readLine();
        sendingLine += logLine + System.getProperty("line.separator");
        DatagramPacket packet = new DatagramPacket(sendingLine.getBytes(), sendingLine.length(),
                        InetAddress.getByName(mConfig.mDestServer), mConfig.mDestPort);
        try {
            mSocket.send(packet);
            ...

Any idea? Thanks.


Solution

  • Finally the answer was to put a Thread.sleep(10) in the while(true) loop.

    It may seem really strange, but also with only 10ms of sleep it reduces the battery usage from almost 40% to 1%.