1 4 package com.tc.util; 5 6 import java.io.BufferedReader ; 7 import java.io.InputStream ; 8 import java.io.InputStreamReader ; 9 import java.net.URL ; 10 import java.net.URLConnection ; 11 import java.util.zip.GZIPInputStream ; 12 13 public class CruiseControlLogParser { 14 15 private static final String MARKER = "...]]></message>"; 16 17 private static final String TAG = " <message priority=\"info\"><![CDATA["; 18 private static final String CLOSE_TAG = "]]></message>"; 19 20 public static void main(String [] args) throws Exception { 21 if (args.length != 2) { 22 usage(); 23 System.exit(1); 24 } 25 26 String urlStr = args[0]; 27 String testName = args[1]; 28 29 String failTestEnd = TAG + "ERROR Test " + testName + " FAILED" + CLOSE_TAG; 30 31 int linenum = 0; 32 boolean inTest = false; 33 URL url = new URL (urlStr); 34 URLConnection connect = url.openConnection(); 35 String encoding = connect.getContentEncoding(); 36 InputStream response = connect.getInputStream(); 37 if ("x-gzip".equals(encoding) || urlStr.toLowerCase().endsWith(".gz")) { 38 response = new GZIPInputStream (response); 39 } 40 41 try { 42 BufferedReader reader = new BufferedReader (new InputStreamReader (response)); 43 String line; 44 45 while ((line = reader.readLine()) != null) { 46 linenum++; 47 if (inTest) { 48 if (line.endsWith("Test" + MARKER)) break; 50 if (line.startsWith(TAG)) { 51 output(line); 52 if (line.startsWith(failTestEnd)) { 53 break; 54 } 55 } 56 } else if (line.endsWith(testName + MARKER)) { 57 output(line); 58 inTest = true; 59 } 60 } 61 } catch (Exception e) { 62 System.err.println("***************** PARSER ERROR (line: " + linenum + ") *****************"); 63 e.printStackTrace(); 64 } 65 } 66 67 private static void output(String line) { 68 boolean err = (line.indexOf("ERROR") != -1 || line.indexOf("WARN") != -1); 69 line = line.substring(TAG.length(), line.length() - CLOSE_TAG.length()); 70 line = line.replaceAll("^WARN( )?", ""); 71 line = line.replaceAll("^INFO( )?", ""); 72 line = line.replaceAll("^ERROR( )?", ""); 73 line = line.replaceAll("<", "<"); 74 line = line.replaceAll(">", ">"); 75 if (err) System.err.println(line); 76 else System.out.println(line); 77 } 78 79 private static void usage() { 80 System.err.println("usage: " + CruiseControlLogParser.class.getName() + " <log URL> <test-name>"); 81 System.err.println(); 82 System.err.println(" example args: http://sand/long-strange-path-to-monkeylog.xml TwelveByteIntegerTest"); 83 } 84 } | Popular Tags |