android logging

This commit is contained in:
zzz
2009-03-10 22:30:33 +00:00
parent 14ce5a2432
commit 82045b3fde
3 changed files with 167 additions and 7 deletions

View File

@ -12,7 +12,7 @@ ant
ant install ant install
#then run the debugger #then run the debugger
$A/ddms & ../../android-sdk-linux_x86-1.1_r1/tools/ddms &
#to rebuild and reinstall to emulator: #to rebuild and reinstall to emulator:
ant reinstall ant reinstall

View File

@ -26,10 +26,10 @@
<os family="windows"/> <os family="windows"/>
</condition> </condition>
<property name="external-libs" value="lib" /> <property name="external-libs" value="libs" />
<condition property="external-libs-ospath" <condition property="external-libs-ospath"
value="..\pkg-temp\${external-libs}" value="${basedir}\${external-libs}"
else="../pkg-temp/${external-libs}" > else="${basedir}/${external-libs}" >
<os family="windows"/> <os family="windows"/>
</condition> </condition>
@ -105,6 +105,8 @@
<target name="clean"> <target name="clean">
<delete dir="${outdir}" /> <delete dir="${outdir}" />
<delete dir="${external-libs}" />
<delete dir="tmp/" />
<delete file="${outdir-r}/net/i2p/router/R.java" /> <delete file="${outdir-r}/net/i2p/router/R.java" />
</target> </target>
@ -144,13 +146,19 @@
destdir="${outdir-classes}" destdir="${outdir-classes}"
bootclasspath="${android-jar}"> bootclasspath="${android-jar}">
<classpath> <classpath>
<fileset dir="${external-libs-ospath}" includes="*.jar"/> <fileset dir="${external-libs}" includes="*.jar"/>
</classpath> </classpath>
</javac> </javac>
</target> </target>
<target name="buildrouter"> <target name="buildrouter">
<ant dir=".." target="prepupdateRouter" /> <ant dir=".." target="buildrouter" />
<copy file="../build/router.jar" todir="${external-libs}" />
<mkdir dir="tmp" />
<unjar src="../build/i2p.jar" dest="tmp/" />
<delete file="tmp/net/i2p/util/LogWriter.class" />
<!-- lots of unneeded stuff, like Log* -->
<jar destfile="${external-libs}/i2p.jar" basedir="tmp/" />
</target> </target>
<!-- Convert this project's .class files into .dex files. --> <!-- Convert this project's .class files into .dex files. -->
@ -162,7 +170,7 @@
<arg value="--dex" /> <arg value="--dex" />
<arg value="--output=${intermediate-dex-ospath}" /> <arg value="--output=${intermediate-dex-ospath}" />
<arg path="${outdir-classes-ospath}" /> <arg path="${outdir-classes-ospath}" />
<fileset dir="${external-libs-ospath}" includes="*.jar"/> <fileset dir="${external-libs}" includes="*.jar"/>
</apply> </apply>
</target> </target>

View File

@ -0,0 +1,152 @@
package net.i2p.util;
/*
* public domain
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
/**
* bridge to android logging
*
* @author zzz
*/
class LogWriter implements Runnable {
private final static long CONFIG_READ_ITERVAL = 10 * 1000;
private long _lastReadConfig = 0;
private long _numBytesInCurrentFile = 0;
private OutputStream _currentOut; // = System.out
private int _rotationNum = -1;
private String _logFilenamePattern;
private File _currentFile;
private LogManager _manager;
private boolean _write;
private LogWriter() { // nop
}
public LogWriter(LogManager manager) {
_manager = manager;
}
public void stopWriting() {
_write = false;
}
public void run() {
_write = true;
try {
while (_write) {
flushRecords();
rereadConfig();
}
System.err.println("Done writing");
} catch (Exception e) {
System.err.println("Error writing the logs: " + e.getMessage());
e.printStackTrace();
}
}
public void flushRecords() { flushRecords(true); }
public void flushRecords(boolean shouldWait) {
try {
List records = _manager._removeAll();
if (records == null) return;
for (int i = 0; i < records.size(); i++) {
LogRecord rec = (LogRecord) records.get(i);
writeRecord(rec);
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
if (shouldWait) {
try {
synchronized (this) {
this.wait(10*1000);
}
} catch (InterruptedException ie) { // nop
}
}
}
}
private void rereadConfig() {
long now = Clock.getInstance().now();
if (now - _lastReadConfig > CONFIG_READ_ITERVAL) {
_manager.rereadConfig();
_lastReadConfig = now;
}
}
private void writeRecord(LogRecord rec) {
if (rec.getThrowable() == null)
log(rec.getPriority(), rec.getSourceName(), null, rec.getThreadName(), rec.getMessage());
else
log(rec.getPriority(), rec.getSourceName(), null, rec.getThreadName(), rec.getMessage(), rec.getThrowable());
}
public void log(int priority, String className, String name, String threadName, String msg) {
if (className != null)
android.util.Log.println(toAndroidLevel(priority),
className,
threadName + ' ' + msg);
else if (name != null)
android.util.Log.println(toAndroidLevel(priority),
name,
threadName + ' ' + msg);
else
android.util.Log.println(toAndroidLevel(priority),
threadName, msg);
}
public void log(int priority, String className, String name, String threadName, String msg, Throwable t) {
if (className != null)
android.util.Log.println(toAndroidLevel(priority),
className,
threadName + ' ' + msg +
msg + ' ' + t.toString() + ' ' + android.util.Log.getStackTraceString(t));
else if (name != null)
android.util.Log.println(toAndroidLevel(priority),
name,
threadName + ' ' + msg +
msg + ' ' + t.toString() + ' ' + android.util.Log.getStackTraceString(t));
else
android.util.Log.println(toAndroidLevel(priority),
threadName,
msg + ' ' + t.toString() + ' ' + android.util.Log.getStackTraceString(t));
}
private static int toAndroidLevel(int level) {
switch (level) {
case Log.DEBUG:
return android.util.Log.DEBUG;
case Log.INFO:
return android.util.Log.INFO;
case Log.WARN:
return android.util.Log.WARN;
case Log.ERROR:
case Log.CRIT:
default:
return android.util.Log.ERROR;
}
}
private static final String replace(String pattern, int num) {
char c[] = pattern.toCharArray();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < c.length; i++) {
if ( (c[i] != '#') && (c[i] != '@') )
buf.append(c[i]);
else
buf.append(num);
}
return buf.toString();
}
}