In Android, all logs are written to /dev/log and logcat is just a process to read and print these logs. The synchronization logic (especially when multiple threads log) is taken care in the driver handling the log device. Applications don't have to deal with the device directly, instead use the convenient APIs (android.util.Log). But what about the Java's API System.out? Android supports this by registering for a custom implementation to trap these calls.
System.setOut(new AndroidPrintStream(Log.INFO, "System.out"));
System.setErr(new AndroidPrintStream(Log.WARN, "System.err"));
AndroidPrintStream is an extension of LoggingPrintStream which basically has a custom implementation for println() and thats how the logs meant for console are redirected to /dev/log.
class AndroidPrintStream extends LoggingPrintStream {
...
...
...
protected void log(String line) {
Log.println(priority, tag, line);
}
}
System.setOut(new AndroidPrintStream(Log.INFO, "System.out"));
System.setErr(new AndroidPrintStream(Log.WARN, "System.err"));
AndroidPrintStream is an extension of LoggingPrintStream which basically has a custom implementation for println() and thats how the logs meant for console are redirected to /dev/log.
class AndroidPrintStream extends LoggingPrintStream {
...
...
...
protected void log(String line) {
Log.println(priority, tag, line);
}
}
No comments:
Post a Comment