android上一个可追踪代码具体到函数某行的日志类
程序员文章站
2023-12-14 18:52:16
代码如下:复制代码 代码如下:package xiaogang.enif.utils; /** * the class logutils for log printing,...
代码如下:
package xiaogang.enif.utils;
/**
* the class logutils for log printing, which help us
* easy to trace our codes or logics in the project .
*
* @author zhao xiaogang
* @time 2011.4.12
*/
public class logutils {
private final static int verbose = 0;
private final static int debug = 1;
private final static int info = 2;
private final static int warn = 3;
private final static int error = 4;
private final static int default_level = -1;
private int level;
private final string clazz;
private static final string tag = "logutils";
public static logutils getdebuglog(class<?> clazz, int l) {
logutils log = new logutils(clazz);
log.level = l;
return log;
}
public static logutils getlog(class<?> clazz) {
return new logutils(clazz);
}
public logutils(class<?> clazz) {
this.clazz = "[" + clazz.getsimplename() + "] ";
level = default_level;
}
public void verbose(string message) {
verbose(message, null);
}
public void debug(string message) {
debug(message, null);
}
public void info(string message) {
info(message, null);
}
public void warn(string message) {
warn(message, null);
}
public void error(string message) {
error(message, null);
}
public void verbose(string message, throwable t) {
if (verbose < level)
return;
if (message != null)
android.util.log.v(tag, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.v(tag, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
public void debug(string message, throwable t) {
if (debug < level)
return;
if (message != null)
android.util.log.d(clazz, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.d(clazz, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
public void info(string message, throwable t) {
if (info < level)
return;
if (message != null)
android.util.log.i(tag, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.i(tag, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
public void warn(string message, throwable t) {
if (warn < level)
return;
if (message != null)
android.util.log.w(tag, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.w(tag, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
public void error(string message, throwable t) {
if (error < level)
return;
if (message != null)
android.util.log.e(tag, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.e(tag, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
private static int getlinenumber() {
return thread.currentthread().getstacktrace()[5].getlinenumber();
}
}
好用的话,记得给好评,嘿嘿!
复制代码 代码如下:
package xiaogang.enif.utils;
/**
* the class logutils for log printing, which help us
* easy to trace our codes or logics in the project .
*
* @author zhao xiaogang
* @time 2011.4.12
*/
public class logutils {
private final static int verbose = 0;
private final static int debug = 1;
private final static int info = 2;
private final static int warn = 3;
private final static int error = 4;
private final static int default_level = -1;
private int level;
private final string clazz;
private static final string tag = "logutils";
public static logutils getdebuglog(class<?> clazz, int l) {
logutils log = new logutils(clazz);
log.level = l;
return log;
}
public static logutils getlog(class<?> clazz) {
return new logutils(clazz);
}
public logutils(class<?> clazz) {
this.clazz = "[" + clazz.getsimplename() + "] ";
level = default_level;
}
public void verbose(string message) {
verbose(message, null);
}
public void debug(string message) {
debug(message, null);
}
public void info(string message) {
info(message, null);
}
public void warn(string message) {
warn(message, null);
}
public void error(string message) {
error(message, null);
}
public void verbose(string message, throwable t) {
if (verbose < level)
return;
if (message != null)
android.util.log.v(tag, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.v(tag, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
public void debug(string message, throwable t) {
if (debug < level)
return;
if (message != null)
android.util.log.d(clazz, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.d(clazz, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
public void info(string message, throwable t) {
if (info < level)
return;
if (message != null)
android.util.log.i(tag, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.i(tag, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
public void warn(string message, throwable t) {
if (warn < level)
return;
if (message != null)
android.util.log.w(tag, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.w(tag, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
public void error(string message, throwable t) {
if (error < level)
return;
if (message != null)
android.util.log.e(tag, clazz + " line: " + getlinenumber() + " : " + message);
if (t != null)
android.util.log.e(tag, clazz + " line: " + getlinenumber() + " : " + t.tostring());
}
private static int getlinenumber() {
return thread.currentthread().getstacktrace()[5].getlinenumber();
}
}
好用的话,记得给好评,嘿嘿!