Log (v2+)

The API

This component mimics most of the android.util.Log class public members, like: Log.d(TAG, "my trace");.

To adapt your existing java sources, all you have to do is just replacing any:
import android.util.Log;
by:
import fr.maxcom.util.Log;

Without a configuration from the Manager, any call to the component is just propagated to the Android layer, as is. So a full compatibility is guaranteed.

The Manager

The interesting feature of the Log component comes with some configuration options, to be set with a manager. Two actions are mandatory: a) set a target where to route the messages ; b) turn on the rerouting.

Setting a target

You define the target, i.e. the receiver of the log messages, by one of the variants in the setTarget() family. Choices are:

  • A local file
  • A remote UDP server

Depending on the hardware of the device, a local file may be a valid option or not. If you have no means to access to the LogCat via ADB, chances are that you won't be able to explore the internal file systeme neither. But some devices are equipped with a connector to plug an external USB Mass Storage part. In that case, this additional disk can be accessed through a mounting point. The Storage components of the library may be a convenient helper for this purpose. After the collect, you will move the USB disk to another hardware for viewing the traces.

The most confortable solution is to use a receiver server: traces can be viewed immediately, as simply as a console output.

Turning on and off the capture

Once a target is set, you may switch on and off the log capturing at any time, by calling setInterceptor().

Examples

Local file

import fr.maxcom.util.Log;
import fr.maxcom.util.LogManager;
// ...
LogManager.setTarget(new File(Environment.getExternalStorageDirectory(), "traces.log"));
LogManager.setInterceptor(true);
Log.d(TAG, "Hello log file!");

Remote server

import fr.maxcom.util.Log;
import fr.maxcom.util.LogManager;
// ...
LogManager.setTarget("192.168.0.10", 9696));
LogManager.setInterceptor(true);
Log.d(TAG, "Hello log server!");

How to set up a UDP server?

With only a few lines of code, you can easily set up a server to receive the log messages and print them on the console. Here are some code samples.

Python code

Py 2.7 version

  1. import SocketServer
  2. class MyUDPHandler(SocketServer.BaseRequestHandler):
  3. def handle(self):
  4. print "{}\t{}".format(self.client_address[0], self.request[0].strip())
  5. if __name__ == "__main__":
  6. HOST, PORT = "0.0.0.0", 9696
  7. server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
  8. server.serve_forever()

Py 3.4 version

  1. import socketserver
  2. class MyUDPHandler(socketserver.BaseRequestHandler):
  3. def handle(self):
  4. print("{}\t{}".format(self.client_address[0], str(self.request[0].strip(), 'utf-8')))
  5. if __name__ == "__main__":
  6. HOST, PORT = "0.0.0.0", 9696
  7. server = socketserver.UDPServer((HOST, PORT), MyUDPHandler)
  8. server.serve_forever()

node.js code

  1. var util = require('util');
  2. require('dgram').createSocket("udp4", function (msg, rinfo) {
  3. util.print(util.format("%s\t%s", rinfo.address , msg));
  4. }).bind(9696);

Here is a sample of the console output:

192.168.0.12    D       03-25 22:17:32.099      MainActivity    onStart
192.168.0.12    D       03-25 22:17:32.099      MainActivity    onResume
192.168.0.12    D       03-25 22:18:04.028      MainActivity    onPause
192.168.0.12    D       03-25 22:18:04.562      MainActivity    onStop
192.168.0.12    D       03-25 22:18:04.564      MainActivity    onDestroy