package xin.glue.ui.common.component; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class PosGlueLogReader { private static StringBuffer sb = new StringBuffer(); private static int index = 0, curIdx = 0; public static String dateTime, sqlQuery, params; public static void readLog(String filename) { readLog(filename, null); } public static void readLog(String filename, String byDateTime) { try { InputStream is = new FileInputStream(filename); byte[] buffer = new byte[is.available()]; sb.setLength(0); sb.append(new String(buffer, 0, is.read(buffer))); is.close(); go(1, byDateTime); } catch (Exception e) { e.printStackTrace(); } } public static void go(int serial) { go(serial, null); } public static void go(int serial, String byDateTime) { index = 0; int idx = 0; int cnt = 0; while (++cnt < serial) { if (byDateTime != null && !"".equals(byDateTime)) { idx = sb.indexOf(byDateTime, idx); if (idx == -1) return; } idx = sb.indexOf("com.posdata.glue.dao.PosJdbcTemplate||Executing SQL", ++idx); if (idx < index) break; index = idx; } next(); curIdx = cnt; } public static void next() { index = sb.indexOf("com.posdata.glue.dao.PosJdbcTemplate||Executing SQL", index); if (index == -1) return; int cnt = 5; while (!sb.substring(index - cnt, index - cnt + 5).equals("DEBUG")) cnt++; dateTime = sb.substring(index - cnt - 24, index - cnt - 1); // System.out.println(dateTime); int startPos = sb.indexOf("[", index); int endPos = sb.indexOf("]", startPos); sqlQuery = sb.substring(startPos + 1, endPos).replaceAll("\n[\t]+", "\n"); // System.out.println(sqlQuery); index = endPos; List param = new ArrayList(); while (true) { endPos += 2; // ×Ö·û£º"]\n" startPos = sb.indexOf("parameter value [", endPos); endPos = sb.indexOf("\n", endPos); if (endPos < startPos || startPos < index) break; param.add(sb.substring(startPos + 17, sb.indexOf("]", startPos))); index = endPos; } params = ""; for (int i = 0, size = param.size(); i < size; i++) { params += "parameter " + (i+1) + ": " + param.get(i).toString() + "\n"; } // System.out.println(params); curIdx++; } public static void next(String byDateTime) { if (byDateTime != null && !"".equals(byDateTime)) index = sb.indexOf(byDateTime, index); next(); } public static void prior() { go(curIdx - 1, null); } public static void prior(String byDateTime) { go(curIdx - 1, byDateTime); } public static void main(String[] args) { readLog("F:\\glue_app_debug.log"); go(8); prior(); } }