1 package net.sf.logdistiller.reports;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.io.Writer;
20 import java.text.DateFormat;
21 import java.util.Date;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.lang.SystemUtils;
25 import org.jdom.Element;
26 import org.jdom.output.Format;
27 import org.jdom.output.XMLOutputter;
28
29 import net.sf.logdistiller.LogDistillation;
30 import net.sf.logdistiller.LogDistiller;
31 import net.sf.logdistiller.ReportFormat;
32 import net.sf.logdistiller.util.FormatUtil;
33
34
35
36
37 public class TextReport
38 extends ReportFormat
39 {
40 private final static DateFormat DF = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.LONG );
41
42 public TextReport()
43 {
44 super( "txt" );
45 }
46
47 public String getContentType()
48 {
49 return "text/plain";
50 }
51
52 public String getFileExtension()
53 {
54 return "txt";
55 }
56
57 public void report( LogDistillation ld, Writer output )
58 throws IOException
59 {
60 LogDistiller definition = ld.getDefinition();
61 PrintWriter out = new PrintWriter( output );
62 out.println( "LogDistillation result (LogDistiller " + ld.getVersion() + ")" );
63 out.println();
64 out.println( "LogDistiller used: " + definition.getOutput().getId() + " - " + definition.getDescription() );
65 out.println( "log content processed: " + ( ( ld.getContent() == null ) ? "not specified" : ld.getContent() ) );
66 out.println( "distillation started on " + DF.format( new Date( ld.getBeginTime() ) ) + ", finished on "
67 + DF.format( new Date( ld.getEndTime() ) ) + ": "
68 + FormatUtil.formatPeriod( ld.getEndTime() - ld.getBeginTime() ) );
69 if ( ld.getSkippedEventCount() > 0 )
70 {
71 out.println( "skipped " + ld.getSkippedEventCount() + " events ("
72 + FormatUtil.formatSize( ld.getSkippedEventBytes() ) + ")" );
73 }
74 String logsUrl = definition.getOutput().getUrl();
75 if ( logsUrl != null )
76 {
77 out.println( "reports and logs url: " + logsUrl );
78 }
79 for ( String warning : definition.getWarnings() )
80 {
81 out.println( "WARNING: " + warning );
82 }
83 out.println();
84 if ( ld.getEventCount() == 0 )
85 {
86 out.println( "found 0 event to classify." );
87 }
88 else
89 {
90 out.println( "found " + ld.getEventCount() + " events (" + FormatUtil.formatSize( ld.getEventBytes() )
91 + ") classified as follow:" );
92
93 for ( LogDistillation.Category category : ld.listCategoriesToReport() )
94 {
95 int categoryEventCount = category.sumEventCount();
96 LogDistiller.Category categoryDefinition = category.getDefinition();
97 out.println( "*** " + categoryEventCount + " " + categoryDefinition.getId() + " ("
98 + FormatUtil.formatFraction( categoryEventCount, ld.getEventCount() ) + "): "
99 + categoryDefinition.getDescription() );
100 int countLength = String.valueOf( categoryEventCount ).length();
101
102 for ( LogDistillation.Group group : category.listGroupsToReport() )
103 {
104 String fraction = formatFraction( group.getEventCount(), categoryEventCount );
105 out.println( "*" + fraction + ' ' + formatGroup( group, countLength, ld.getEventCount() ) );
106 addPluginsGlobalReport( group, new TextPluginReport( out, "* ", group.getEventCount() ) );
107 }
108
109 out.println();
110 }
111
112
113 int countLength = String.valueOf( ld.getEventCount() ).length();
114 for ( LogDistillation.Group group : ld.listGroupsToReport() )
115 {
116 out.println( formatGroup( group, countLength, ld.getEventCount() ) );
117 addPluginsGlobalReport( group, new TextPluginReport( out, " ", group.getEventCount() ) );
118 }
119 }
120 out.flush();
121 }
122
123 private String formatGroup( LogDistillation.Group group, int countLength, int eventCount )
124 {
125 int groupEventCount = group.getEventCount();
126 LogDistiller.Group groupDefinition = group.getDefinition();
127 String sep = groupDefinition.continueProcessing() ? "+ " : "- ";
128 return sep + StringUtils.leftPad( String.valueOf( groupEventCount ), countLength ) + " ("
129 + formatFraction( groupEventCount, eventCount ) + ") " + groupDefinition.getId() + ": "
130 + groupDefinition.getDescription();
131 }
132
133 private String formatFraction( long fraction, long total )
134 {
135 return StringUtils.leftPad( FormatUtil.formatFraction( fraction, total ), 6 );
136 }
137
138 public void report( LogDistillation.Group group, Writer output )
139 throws IOException
140 {
141 LogDistiller.Group definition = group.getDefinition();
142 PrintWriter out = new PrintWriter( output );
143 out.println( "LogDistillation group result (LogDistiller " + LogDistiller.getVersion() + ")" );
144 out.println();
145 out.println( "LogDistiller group: " + definition.getId() + " - " + definition.getDescription() );
146 out.println( "for LogDistiller: " + definition.getLogdistiller().getOutput().getId() + " - "
147 + definition.getLogdistiller().getDescription() );
148 out.println( "log content processed: "
149 + ( ( group.getLogdistillation().getContent() == null ) ? "not specified"
150 : group.getLogdistillation().getContent() ) );
151 out.println( "found " + group.getEventCount() + " events (" + FormatUtil.formatSize( group.getBytes() )
152 + ") matching following definition:" );
153 Element elmt = new Element( "conditions" );
154 definition.dumpConditions( elmt );
155 if ( elmt.getContentSize() > 0 )
156 {
157 out.print( " " );
158 new XMLOutputter( Format.getPrettyFormat().setLineSeparator( SystemUtils.LINE_SEPARATOR + " " ) ).outputElementContent(
159 elmt,
160 output );
161 out.println();
162 }
163 else
164 {
165 out.println( " no condition specified: every log event accepted" );
166 }
167
168 String logsUrl = definition.getLogdistiller().getOutput().getUrl();
169 if ( definition.getSave() )
170 {
171 out.println( "log events saved in file: " + group.getLogFile().getAbsolutePath() );
172 out.print( "max save count: " );
173 if ( group.getMaxSaveCount() < 0 )
174 {
175 out.println( "none" );
176 }
177 else
178 {
179 out.println( String.valueOf( group.getMaxSaveCount() ) );
180 }
181 out.print( "max save size: " );
182 if ( group.getMaxSaveBytes() < 0 )
183 {
184 out.println( "none" );
185 }
186 else
187 {
188 out.println( String.valueOf( group.getMaxSaveBytes() / 1024 ) + " kb" );
189 }
190 if ( group.getSavedEventCount() < group.getEventCount() )
191 {
192 out.println( "save limit reached: " + group.getSavedEventCount() + " events" );
193 }
194 if ( logsUrl != null )
195 {
196 out.println( "log events url: "
197 + definition.getLogdistiller().getOutput().getUrl( group.getLogFile().getName() ) );
198 }
199 }
200 else
201 {
202 out.println( "log events not saved" );
203 }
204 if ( logsUrl != null )
205 {
206 out.println( "reports and logs url: " + logsUrl );
207 }
208 if ( definition.continueProcessing() )
209 {
210 out.println( "continueProcessing: true" );
211 }
212 out.println();
213 addPluginsGroupReport( group, new TextPluginReport( out, "", group.getEventCount() ) );
214 out.flush();
215 }
216
217 private static class TextPluginReport
218 extends PluginReport
219 {
220 private final PrintWriter out;
221
222 private final String prefix;
223
224 private final int groupEventCount;
225
226 private final int countLength;
227
228 public TextPluginReport( PrintWriter out, String prefix, int groupEventCount )
229 {
230 this.out = out;
231 this.prefix = prefix;
232 this.groupEventCount = groupEventCount;
233 countLength = String.valueOf( groupEventCount ).length();
234 }
235
236 public void beginPluginReport( LogDistillation.Plugin plugin, String description )
237 {
238 out.println( prefix + plugin.getDefinition().getType() + ": " + description );
239 }
240
241 public void addLink( String filename, String description )
242 {
243 out.println( prefix + "file " + filename + ": " + description );
244 }
245
246 public void addParam( String param, String value )
247 {
248 out.println( prefix + param + ": " + value );
249 }
250
251 public void addItem( int count, String description )
252 {
253 out.println( prefix + " # " + StringUtils.leftPad( String.valueOf( count ), countLength ) + " ("
254 + StringUtils.leftPad( FormatUtil.formatFraction( count, groupEventCount ), 6 ) + ") " + description );
255 }
256
257 public void endPluginReport()
258 {
259 out.println( prefix.trim() );
260 }
261 }
262 }