1 | package clarin.cmdi.componentregistry.frontend; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileNotFoundException; |
---|
5 | import java.io.IOException; |
---|
6 | import java.io.RandomAccessFile; |
---|
7 | import java.text.NumberFormat; |
---|
8 | import org.apache.log4j.FileAppender; |
---|
9 | import org.apache.log4j.Logger; |
---|
10 | import org.apache.wicket.PageParameters; |
---|
11 | import org.apache.wicket.markup.html.basic.Label; |
---|
12 | import org.apache.wicket.markup.html.form.TextArea; |
---|
13 | import org.apache.wicket.markup.html.link.DownloadLink; |
---|
14 | import org.apache.wicket.markup.html.link.Link; |
---|
15 | import org.apache.wicket.model.Model; |
---|
16 | |
---|
17 | /** |
---|
18 | * |
---|
19 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
20 | * @author paucas |
---|
21 | */ |
---|
22 | public class ViewLogPage extends SecureAdminWebPage { |
---|
23 | |
---|
24 | public ViewLogPage(final PageParameters pageParameters) { |
---|
25 | super(pageParameters); |
---|
26 | addLinks(); |
---|
27 | addLogFileContent(); |
---|
28 | } |
---|
29 | |
---|
30 | private void addLinks() { |
---|
31 | add(new Link("home") { |
---|
32 | @Override |
---|
33 | public void onClick() { |
---|
34 | setResponsePage(AdminHomePage.class); |
---|
35 | } |
---|
36 | }); |
---|
37 | } |
---|
38 | |
---|
39 | private void addLogFileContent() { |
---|
40 | final int tailSize = 1000000; // Megabyte |
---|
41 | |
---|
42 | final NumberFormat numberFormat = NumberFormat.getIntegerInstance(); |
---|
43 | numberFormat.setGroupingUsed(true); |
---|
44 | |
---|
45 | final File logFile = getLogFile(); |
---|
46 | if (logFile != null) { |
---|
47 | try { |
---|
48 | final RandomAccessFile raLogFile = geFileTail(logFile, tailSize); |
---|
49 | try { |
---|
50 | final String content = getLogFileContent(raLogFile); |
---|
51 | |
---|
52 | add(new Label("logLabel", String.format("Showing final %s bytes (or less) of total %s in %s:", numberFormat.format(tailSize), numberFormat.format(raLogFile.length()), logFile))); |
---|
53 | add(new TextArea("logText", new Model(content))); |
---|
54 | |
---|
55 | add(new DownloadLink("logDownloadLink", logFile)); |
---|
56 | } finally { |
---|
57 | raLogFile.close(); |
---|
58 | } |
---|
59 | } catch (IOException ioEx) { |
---|
60 | add(new Label("logLabel", "Could not read from log file. See error message below.")); |
---|
61 | add(new TextArea("logText", new Model(ioEx.getMessage()))); |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | private String getLogFileContent(final RandomAccessFile randomAccessFile) throws IOException { |
---|
67 | String currentLine; |
---|
68 | StringBuilder contentBuilder = new StringBuilder(); |
---|
69 | while ((currentLine = randomAccessFile.readLine()) != null) { |
---|
70 | contentBuilder.append(currentLine).append("\n"); |
---|
71 | } |
---|
72 | String content = contentBuilder.toString(); |
---|
73 | return content; |
---|
74 | } |
---|
75 | |
---|
76 | private RandomAccessFile geFileTail(File logFile, int tailLength) throws IOException, FileNotFoundException { |
---|
77 | // Skip to tail of file |
---|
78 | final RandomAccessFile raLogFile = new RandomAccessFile(logFile, "r"); |
---|
79 | final long startPosition = raLogFile.length() - tailLength; |
---|
80 | if (startPosition > 0) { |
---|
81 | raLogFile.seek(startPosition); |
---|
82 | // Read until end of line so we don't end up halfway some random line |
---|
83 | raLogFile.readLine(); |
---|
84 | } |
---|
85 | return raLogFile; |
---|
86 | } |
---|
87 | |
---|
88 | private File getLogFile() { |
---|
89 | // Get file from appender |
---|
90 | final FileAppender appender = (FileAppender) Logger.getRootLogger().getAppender("ROOT"); |
---|
91 | if (appender != null) { |
---|
92 | return new File(appender.getFile()); |
---|
93 | } |
---|
94 | return null; |
---|
95 | } |
---|
96 | } |
---|