01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.util;
04:
05: import java.io.BufferedReader;
06: import java.io.InputStream;
07: import java.io.InputStreamReader;
08: import java.net.URL;
09: import java.net.URLConnection;
10: import java.util.zip.GZIPInputStream;
11:
12: public class CruiseControlLogParser {
13:
14: private static final String MARKER = "...]]></message>";
15:
16: private static final String TAG = " <message priority=\"info\"><![CDATA[";
17: private static final String CLOSE_TAG = "]]></message>";
18:
19: public static void main(String[] args) throws Exception {
20: if (args.length != 2) {
21: usage();
22: System.exit(1);
23: }
24:
25: String urlStr = args[0];
26: String testName = args[1];
27:
28: String failTestEnd = TAG + "ERROR Test " + testName
29: + " 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)
38: || urlStr.toLowerCase().endsWith(".gz")) {
39: response = new GZIPInputStream(response);
40: }
41:
42: try {
43: BufferedReader reader = new BufferedReader(
44: new InputStreamReader(response));
45: String line;
46:
47: while ((line = reader.readLine()) != null) {
48: linenum++;
49: if (inTest) {
50: if (line.endsWith("Test" + MARKER))
51: break; // start of another test
52:
53: if (line.startsWith(TAG)) {
54: output(line);
55: if (line.startsWith(failTestEnd)) {
56: break;
57: }
58: }
59: } else if (line.endsWith(testName + MARKER)) {
60: output(line);
61: inTest = true;
62: }
63: }
64: } catch (Exception e) {
65: System.err.println("***************** PARSER ERROR (line: "
66: + linenum + ") *****************");
67: e.printStackTrace();
68: }
69: }
70:
71: private static void output(String line) {
72: boolean err = (line.indexOf("ERROR") != -1 || line
73: .indexOf("WARN") != -1);
74: line = line.substring(TAG.length(), line.length()
75: - CLOSE_TAG.length());
76: line = line.replaceAll("^WARN( )?", "");
77: line = line.replaceAll("^INFO( )?", "");
78: line = line.replaceAll("^ERROR( )?", "");
79: line = line.replaceAll("<", "<");
80: line = line.replaceAll(">", ">");
81: if (err)
82: System.err.println(line);
83: else
84: System.out.println(line);
85: }
86:
87: private static void usage() {
88: System.err.println("usage: "
89: + CruiseControlLogParser.class.getName()
90: + " <log URL> <test-name>");
91: System.err.println();
92: System.err
93: .println(" example args: http://sand/long-strange-path-to-monkeylog.xml TwelveByteIntegerTest");
94: }
95: }
|