package example;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Java實現類似C/C++中的__FILE__、__FUNC__、__LINE__等,主要用於日誌等功能中。
*
* @version 1.0 2011-07-13
*
*/
public abstract class CommonFunction {
/**
* 打印日誌
*/
public static void log()
{
System.out.println(getFileLineMethod());
}
/**
* 打印日誌時獲取當前的程序文件名、行號、方法名 輸出格式爲:[FileName | LineNumber | MethodName]
*
* @return
*/
public static String getFileLineMethod() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[2];
StringBuffer toStringBuffer = new StringBuffer("[").append(
traceElement.getFileName()).append(" | ").append(
traceElement.getLineNumber()).append(" | ").append(
traceElement.getMethodName()).append("]");
return toStringBuffer.toString();
}
// 當前文件名
public static String _FILE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getFileName();
}
// 當前方法名
public static String _FUNC_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getMethodName();
}
// 當前行號
public static int _LINE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getLineNumber();
}
// 當前時間
public static String _TIME_() {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return sdf.format(now);
}
}
Java實現類似C/C++中的__FILE__、__FUNC__、__LINE__等,主要用於日誌等功能中。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.