📄 debugtrace5.java
字号:
/**
*
*/
package com.fishelf.debug;
import com.fishelf.util.Directory;
import com.fishelf.util.PropertiesManager;
/**
* @author fishelf
*
*/
public class DebugTrace
{
// 当前DebugTrace对象
private static DebugTrace currentDT = null;
//当前jvm运行时堆栈小于3时提示
private static final String UNKNOW_SOURCE = "unknow source";
//默认配置文件路径
private static final String PROPERTITES_FILE_DIRECTORY = "WEB-INF/Debugger.propertites";
//配置文件中字段名
private static final String PROPERTY_KEY = "is_print_model_on";
//jvm堆栈number因子
private static final int jvmStackNum = 3;
//是否打印测试信息
private boolean isPrintModelOn = true;
private DebugTrace()// 私有constructor
{
}
/**
* 通过StackTraceElement类获得当前jvm运行堆栈再获得调用者具体信息
*
* @return String 取得调用DEBUG具体位置
*/
protected String getCaller()
{
String result = "";
int[] int4Exp = new int[2];
try
{
@SuppressWarnings("unused")
int tempInt = int4Exp[2];
}
catch (IndexOutOfBoundsException iobe)
{
// iobe.printStackTrace();
StackTraceElement[] ste = iobe.getStackTrace();
if (ste.length < jvmStackNum)
{
return UNKNOW_SOURCE;
}
// 1.当前getCaller方法; 2.lazy单态工厂
result = "caller: " + ste[jvmStackNum - 1].getClassName() + "."
+ ste[jvmStackNum - 1].getMethodName() + " (line "
+ ste[jvmStackNum - 1].getLineNumber() + " [file: "
+ ste[jvmStackNum - 1].getFileName() + "])";
}
return result;
}
/**
* DebugTrace的lazy singleton工厂
*
* @return DebugTrace 返回唯一实例,必须实现线程安全
*/
public static synchronized DebugTrace DebugTraceFactory()
{
if (currentDT == null)
{
currentDT = new DebugTrace();
String propertiesRealPath = Directory
.GetProjectRealPath(Directory.class);
propertiesRealPath = propertiesRealPath
.concat(PROPERTITES_FILE_DIRECTORY);
String IsPrintModelOnString = PropertiesManager
.getProperties(propertiesRealPath,
PROPERTY_KEY);
currentDT.setIsPrintModelOn(IsPrintModelOnString
.equals("true") ? true : false);
}
return currentDT;
}
/**
* 私有setter 设置print model
*
* @param isPrintModelOn
*/
protected void setIsPrintModelOn(boolean isPrintModelOn)
{
this.isPrintModelOn = isPrintModelOn;
}
public void printMsg(String msg)
{
if (this.isPrintModelOn == false)
{
return;
}
else
{
String aimStr = this.getCaller() + "\n" + "message: " + msg;
char[] marks = new char[((aimStr.length() - msg.length()) < 80
? (aimStr.length() - msg.length()) : 80)];
for (int i = 0; i < marks.length; i++)
{
marks[i] = '=';
}
System.err.println(marks);
System.err.println(aimStr);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -