79aeb63aab64eda5b081a7282811bc5ebb18b535.svn-base 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package xin.glue.ui.common.component;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class PosGlueLogReader {
  7. private static StringBuffer sb = new StringBuffer();
  8. private static int index = 0, curIdx = 0;
  9. public static String dateTime, sqlQuery, params;
  10. public static void readLog(String filename) {
  11. readLog(filename, null);
  12. }
  13. public static void readLog(String filename, String byDateTime) {
  14. try {
  15. InputStream is = new FileInputStream(filename);
  16. byte[] buffer = new byte[is.available()];
  17. sb.setLength(0);
  18. sb.append(new String(buffer, 0, is.read(buffer)));
  19. is.close();
  20. go(1, byDateTime);
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. public static void go(int serial) {
  26. go(serial, null);
  27. }
  28. public static void go(int serial, String byDateTime) {
  29. index = 0;
  30. int idx = 0;
  31. int cnt = 0;
  32. while (++cnt < serial) {
  33. if (byDateTime != null && !"".equals(byDateTime)) {
  34. idx = sb.indexOf(byDateTime, idx);
  35. if (idx == -1) return;
  36. }
  37. idx = sb.indexOf("com.posdata.glue.dao.PosJdbcTemplate||Executing SQL", ++idx);
  38. if (idx < index) break;
  39. index = idx;
  40. }
  41. next();
  42. curIdx = cnt;
  43. }
  44. public static void next() {
  45. index = sb.indexOf("com.posdata.glue.dao.PosJdbcTemplate||Executing SQL", index);
  46. if (index == -1) return;
  47. int cnt = 5;
  48. while (!sb.substring(index - cnt, index - cnt + 5).equals("DEBUG")) cnt++;
  49. dateTime = sb.substring(index - cnt - 24, index - cnt - 1);
  50. // System.out.println(dateTime);
  51. int startPos = sb.indexOf("[", index);
  52. int endPos = sb.indexOf("]", startPos);
  53. sqlQuery = sb.substring(startPos + 1, endPos).replaceAll("\n[\t]+", "\n");
  54. // System.out.println(sqlQuery);
  55. index = endPos;
  56. List param = new ArrayList();
  57. while (true) {
  58. endPos += 2; // ×Ö·û£º"]\n"
  59. startPos = sb.indexOf("parameter value [", endPos);
  60. endPos = sb.indexOf("\n", endPos);
  61. if (endPos < startPos || startPos < index) break;
  62. param.add(sb.substring(startPos + 17, sb.indexOf("]", startPos)));
  63. index = endPos;
  64. }
  65. params = "";
  66. for (int i = 0, size = param.size(); i < size; i++) {
  67. params += "parameter " + (i+1) + ": " + param.get(i).toString() + "\n";
  68. }
  69. // System.out.println(params);
  70. curIdx++;
  71. }
  72. public static void next(String byDateTime) {
  73. if (byDateTime != null && !"".equals(byDateTime))
  74. index = sb.indexOf(byDateTime, index);
  75. next();
  76. }
  77. public static void prior() {
  78. go(curIdx - 1, null);
  79. }
  80. public static void prior(String byDateTime) {
  81. go(curIdx - 1, byDateTime);
  82. }
  83. public static void main(String[] args) {
  84. readLog("F:\\glue_app_debug.log");
  85. go(8);
  86. prior();
  87. }
  88. }