-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogFormatter.java
160 lines (141 loc) · 4.25 KB
/
LogFormatter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package de.agent94ger.minecraft.LogFormatter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
/**
* A simple cli created to compile Minecraft-serverlogs into HTML-files.
*
* @author agent94ger
* @version Beta-1.0
*/
public class LogFormatter
{
private static String _file;
private static ArrayList<LogPage> _pages;
public static void main(String[] args)
{
_pages = new ArrayList<LogPage>();
// Get file location via args or input
_file = initFile(args);
// Parse the log
parseLog(_file);
_pages.get(0).setAsFirstPage();
_pages.get(_pages.size()-1).setAsLastPage();
for (LogPage page : _pages)
{
page.print();
}
System.out.println("Done.");
}
/**
* Initializes file location
*
* @param args Array of args
* @return File location
*/
private static String initFile(String[] args)
{
String file = "";
if (args.length > 0)
{
file = args[0];
}
else
{
System.out.print("Enter file location:" + System.lineSeparator());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
file = br.readLine();
}
catch (IOException ioe)
{
System.out.println("IO error trying to read the filename");
System.exit(1);
}
}
return file;
}
/**
* Reads the log line by line and calls the line parser
*
* @param filename The complete path to the wanted log
*/
private static void parseLog(String filename)
{
Path path = Paths.get(filename);
try (Scanner scanner = new Scanner(path, StandardCharsets.UTF_8.name()))
{
String output = "";
int linesPerPageInit = 100;
int linesPerPage = linesPerPageInit;
int lines = 0;
int pageNo = 0;
while (scanner.hasNextLine())
{
if (lines < linesPerPage)
{
lines++;
}
else
{
_pages.add(new LogPage(pageNo, output));
pageNo++;
output = "";
lines = 0;
linesPerPage = linesPerPageInit-1;
}
String line = scanner.nextLine();
line = parseLine(line);
output += line + System.lineSeparator();
}
if (!output.equals(""))
{
_pages.add(new LogPage(pageNo, output));
}
}
catch (Exception e)
{
System.out.println("Error: File not found, please use absolute path");
System.exit(1);
}
}
/**
* Parses the given line
*
* @param line The line to parse
*/
private static String parseLine(String line)
{
// Replace special characters
line = line.replace("&", "&");
line = line.replace("<", "<");
line = line.replace(">", ">");
line = line.replace("ä", "ä");
line = line.replace("Ä", "Ä");
line = line.replace("ö", "ö");
line = line.replace("Ö", "Ö");
line = line.replace("ü", "ü");
line = line.replace("Ü", "Ü");
line = line.replace("ß", "ß");
// Add colors and new lines
if (line.contains("WARN"))
{
line = "<span style=\"color:#FF0000\">" + line + "</span><br />";
}
else if (line.contains("INFO"))
{
line = "<span style=\"color:#FFFFFF\">" + line + "</span><br />";
}
else
{
line = "<span style=\"color:#FF0000\">" + line + "</span><br />";
}
return line;
}
}