天天看點

Android 代碼裡 用log列印 該行的類名、檔案名、方法名、行号等資訊

//	Log.d("5555555555555", "資訊"+Tools.getMyStackTraceInfo(new Exception()));
//	Log.d("5555555555555", "資訊"+Tools.getStackTraceInfo(new Exception()));
	/**
	*得到Exception所在代碼的行資訊
	*如果沒有行資訊,傳回null
	*/
	public static String getMyStackTraceInfo(Exception e){
	StackTraceElement[] trace =e.getStackTrace();
	if(trace==null||trace.length==0) return null; 
	
	return trace[0].getFileName()+trace[0].getClassName()+trace[0].getMethodName()+trace[0].getLineNumber();
	}
	/**
	 *得到Exception所在代碼的行資訊
	 *如果沒有行資訊,傳回null
	 */
	public static String getStackTraceInfo(Exception e){
		StackTraceElement[] trace =e.getStackTrace();
		if(trace==null||trace.length==0) return null; 
		
		return trace[0].toString();
	}