View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id: MinimalLogFormatter.java 3461 2008-08-07 17:43:26Z pramirez $
14  
15  package gov.nasa.pds.tools.logging;
16  
17  import gov.nasa.pds.tools.label.Label;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.logging.Formatter;
23  import java.util.logging.Handler;
24  import java.util.logging.LogRecord;
25  
26  public class MinimalLogFormatter extends Formatter {
27  	private int numPassed;
28  	private int numFailed;
29  	private int numSkipped;
30  	private int totalErrors;
31  	private int totalWarnings;
32  	private int totalInfos;
33  	private List records;
34  	private StringBuffer config;
35  	private StringBuffer parameters;
36  	private boolean headerPrinted;
37  	private static String padding = "      ";
38  	private static String lineFeed = System.getProperty("line.separator", "\n");
39  	private static String doubleLineFeed = System.getProperty("line.separator", "\n") + System.getProperty("line.separator", "\n");
40  	
41  	public MinimalLogFormatter() {
42  		numPassed = 0;
43  		numFailed = 0;
44  		numSkipped = 0;
45  		totalErrors = 0;
46  		totalWarnings = 0;
47  		totalInfos = 0;
48  		headerPrinted = false;
49  		records = new ArrayList();
50  		config = new StringBuffer();
51  		parameters = new StringBuffer("Parameter Settings:" + doubleLineFeed);
52  	}
53  
54  	/* (non-Javadoc)
55       * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
56       */
57  	public String format(LogRecord record) {
58  		ToolsLogRecord toolsRecord = (ToolsLogRecord) record;
59  		
60  		if (toolsRecord.getLevel() == ToolsLevel.CONFIGURATION) {
61  			config.append("  " + toolsRecord.getMessage() + lineFeed);
62  		} else if (toolsRecord.getLevel() == ToolsLevel.PARAMETER) {
63  			parameters.append("  " + toolsRecord.getMessage() + lineFeed);
64  		} else if (toolsRecord.getLevel() == ToolsLevel.NOTIFICATION && (Label.PASS.equals(toolsRecord.getMessage()) || 
65  				Label.SKIP.equals(toolsRecord.getMessage()) || Label.FAIL.equals(toolsRecord.getMessage()))) {
66  			return processRecords(toolsRecord);
67  		} else {
68  			records.add(toolsRecord);
69  		}
70  		
71  		return "";
72  	}
73  	
74  	private String processRecords(ToolsLogRecord record) {
75  		StringBuffer report = new StringBuffer();
76  		int numErrors = 0;
77  		int numWarnings = 0;
78  		int numInfos = 0;
79  
80  		if (!headerPrinted) {
81  			headerPrinted = true;
82  			report.append("PDS Validation Tool Report" + doubleLineFeed);
83  			report.append(config);
84  			report.append(lineFeed);
85  			report.append(parameters);
86  			report.append(lineFeed);
87  			report.append("Message Counts:" + doubleLineFeed);
88  			report.append(" ERROR    WARN    INFO    FILE" + lineFeed);
89  		}
90  		
91  		if (record.getMessage().equals("PASS"))
92  			numPassed++;
93  		else if (record.getMessage().equals("FAIL"))
94  			numFailed++;
95  		else
96  			numSkipped++;
97  		
98  		for (Iterator i = records.iterator(); i.hasNext();) {
99  			ToolsLogRecord tlr = (ToolsLogRecord) i.next();
100 			if (tlr.getFile() != null && (record.getFile().equals(tlr.getFile()) || record.getFile().equals(tlr.getContext()))) {
101 				if (tlr.getLevel() == ToolsLevel.SEVERE)
102 					numErrors++;
103 				else if (tlr.getLevel() == ToolsLevel.WARNING)
104 					numWarnings++;
105 				else if (tlr.getLevel() == ToolsLevel.INFO)
106 					numInfos++;
107 			}
108 		}
109 		
110 		totalErrors += numErrors;
111 		totalWarnings += numWarnings;
112 		totalInfos += numInfos;
113 		
114 		String errors = "" + numErrors;
115 		String warnings = "" + numWarnings;
116 		String infos = "" + numInfos;
117 		
118 		report.append(padding.substring(errors.length()) + errors + "  ");
119 		report.append(padding.substring(warnings.length()) + warnings + "  ");
120 		report.append(padding.substring(infos.length()) + infos + "    ");
121 		report.append(record.getFile() + lineFeed);
122 		
123 		records = new ArrayList();
124 		return report.toString();
125 	}
126 
127 	public String getTail(Handler handler) {
128 		StringBuffer report = new StringBuffer();
129 		int totalFiles = numPassed + numFailed + numSkipped;
130 		int totalValidated = numPassed + numFailed;
131 		
132 		report.append("----------------------" + lineFeed);
133 		String errors = "" + totalErrors;
134 		String warnings = "" + totalWarnings;
135 		String infos = "" + totalInfos;
136 		
137 		report.append(padding.substring(errors.length()) + errors + "  ");
138 		report.append(padding.substring(warnings.length()) + warnings + "  ");
139 		report.append(padding.substring(infos.length()) + infos + doubleLineFeed);
140 		
141 		report.append(doubleLineFeed + "Summary:" + doubleLineFeed);
142 		report.append("  " + totalValidated + " of " + totalFiles + " validated, " + numSkipped + " skipped" + lineFeed);
143 		report.append("  " + numPassed + " of " + totalValidated + " passed" + doubleLineFeed);
144 		report.append("End of Report" + lineFeed);
145 		return report.toString();
146 	}
147 }