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  
14  package gov.nasa.pds.tools.logging;
15  
16  import gov.nasa.pds.tools.label.Label;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  import java.util.logging.Formatter;
22  import java.util.logging.Handler;
23  import java.util.logging.LogRecord;
24  
25  public class MinimalLogFormatter extends Formatter {
26  	private int numPassed;
27  	private int numFailed;
28  	private int numSkipped;
29  	private int totalErrors;
30  	private int totalWarnings;
31  	private int totalInfos;
32  	private List records;
33  	private StringBuffer config;
34  	private StringBuffer parameters;
35  	private boolean headerPrinted;
36  	private static String padding = "      ";
37  	private static String lineFeed = System.getProperty("line.separator", "\n");
38  	private static String doubleLineFeed = System.getProperty("line.separator", "\n") + System.getProperty("line.separator", "\n");
39  	
40  	public MinimalLogFormatter() {
41  		numPassed = 0;
42  		numFailed = 0;
43  		numSkipped = 0;
44  		totalErrors = 0;
45  		totalWarnings = 0;
46  		totalInfos = 0;
47  		headerPrinted = false;
48  		records = new ArrayList();
49  		config = new StringBuffer();
50  		parameters = new StringBuffer("Parameter Settings:" + doubleLineFeed);
51  	}
52  
53  	/* (non-Javadoc)
54       * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
55       */
56  	public String format(LogRecord record) {
57  		ToolsLogRecord toolsRecord = (ToolsLogRecord) record;
58  		
59  		if (toolsRecord.getLevel() == ToolsLevel.CONFIGURATION) {
60  			config.append("  " + toolsRecord.getMessage() + lineFeed);
61  		} else if (toolsRecord.getLevel() == ToolsLevel.PARAMETER) {
62  			parameters.append("  " + toolsRecord.getMessage() + lineFeed);
63  		} else if (toolsRecord.getLevel() == ToolsLevel.NOTIFICATION && (Label.PASS.equals(toolsRecord.getMessage()) || 
64  				Label.SKIP.equals(toolsRecord.getMessage()) || Label.FAIL.equals(toolsRecord.getMessage()))) {
65  			return processRecords(toolsRecord);
66  		} else {
67  			records.add(toolsRecord);
68  		}
69  		
70  		return "";
71  	}
72  	
73  	private String processRecords(ToolsLogRecord record) {
74  		StringBuffer report = new StringBuffer();
75  		int numErrors = 0;
76  		int numWarnings = 0;
77  		int numInfos = 0;
78  
79  		if (!headerPrinted) {
80  			headerPrinted = true;
81  			report.append("PDS Validation Tool Report" + doubleLineFeed);
82  			report.append(config);
83  			report.append(lineFeed);
84  			report.append(parameters);
85  			report.append(lineFeed);
86  			report.append("Message Counts:" + doubleLineFeed);
87  			report.append(" ERROR    WARN    INFO    FILE" + lineFeed);
88  		}
89  		
90  		if (record.getMessage().equals("PASS"))
91  			numPassed++;
92  		else if (record.getMessage().equals("FAIL"))
93  			numFailed++;
94  		else
95  			numSkipped++;
96  		
97  		for (Iterator i = records.iterator(); i.hasNext();) {
98  			ToolsLogRecord tlr = (ToolsLogRecord) i.next();
99  			if (tlr.getFile() != null && (record.getFile().equals(tlr.getFile()) || record.getFile().equals(tlr.getContext()))) {
100 				if (tlr.getLevel() == ToolsLevel.SEVERE)
101 					numErrors++;
102 				else if (tlr.getLevel() == ToolsLevel.WARNING)
103 					numWarnings++;
104 				else if (tlr.getLevel() == ToolsLevel.INFO)
105 					numInfos++;
106 			}
107 		}
108 		
109 		totalErrors += numErrors;
110 		totalWarnings += numWarnings;
111 		totalInfos += numInfos;
112 		
113 		String errors = "" + numErrors;
114 		String warnings = "" + numWarnings;
115 		String infos = "" + numInfos;
116 		
117 		report.append(padding.substring(errors.length()) + errors + "  ");
118 		report.append(padding.substring(warnings.length()) + warnings + "  ");
119 		report.append(padding.substring(infos.length()) + infos + "    ");
120 		report.append(record.getFile() + lineFeed);
121 		
122 		records = new ArrayList();
123 		return report.toString();
124 	}
125 
126 	public String getTail(Handler handler) {
127 		StringBuffer report = new StringBuffer();
128 		int totalFiles = numPassed + numFailed + numSkipped;
129 		int totalValidated = numPassed + numFailed;
130 		
131 		report.append("----------------------" + lineFeed);
132 		String errors = "" + totalErrors;
133 		String warnings = "" + totalWarnings;
134 		String infos = "" + totalInfos;
135 		
136 		report.append(padding.substring(errors.length()) + errors + "  ");
137 		report.append(padding.substring(warnings.length()) + warnings + "  ");
138 		report.append(padding.substring(infos.length()) + infos + doubleLineFeed);
139 		
140 		report.append(doubleLineFeed + "Summary:" + doubleLineFeed);
141 		report.append("  " + totalValidated + " of " + totalFiles + " validated, " + numSkipped + " skipped" + lineFeed);
142 		report.append("  " + numPassed + " of " + totalValidated + " passed" + doubleLineFeed);
143 		report.append("End of Report" + lineFeed);
144 		return report.toString();
145 	}
146 }