franta-hg@8
|
1 |
/**
|
franta-hg@8
|
2 |
* copy-image-resizer
|
franta-hg@8
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@8
|
4 |
*
|
franta-hg@8
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@8
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@8
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@8
|
8 |
* (at your option) any later version.
|
franta-hg@8
|
9 |
*
|
franta-hg@8
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@8
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@8
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@8
|
13 |
* GNU General Public License for more details.
|
franta-hg@8
|
14 |
*
|
franta-hg@8
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@8
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@8
|
17 |
*/
|
franta-hg@8
|
18 |
package cz.frantovo.copyImageResizer.logging;
|
franta-hg@8
|
19 |
|
franta-hg@8
|
20 |
import cz.frantovo.copyImageResizer.logging.ColorfulPrintWriter.TerminalColor;
|
franta-hg@8
|
21 |
import cz.frantovo.copyImageResizer.logging.ColorfulPrintWriter.TerminalStyle;
|
franta-hg@8
|
22 |
import java.io.StringWriter;
|
franta-hg@8
|
23 |
import java.util.logging.Formatter;
|
franta-hg@8
|
24 |
import java.util.logging.Level;
|
franta-hg@8
|
25 |
import java.util.logging.LogRecord;
|
franta-hg@8
|
26 |
|
franta-hg@8
|
27 |
/**
|
franta-hg@8
|
28 |
* For console/terminal log output. Log messages are printed in brief and colorful form.
|
franta-hg@8
|
29 |
*
|
franta-hg@8
|
30 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@8
|
31 |
*/
|
franta-hg@8
|
32 |
public class ColorfulConsoleFormatter extends Formatter {
|
franta-hg@8
|
33 |
|
franta-hg@8
|
34 |
private boolean printStacktrace = false;
|
franta-hg@8
|
35 |
|
franta-hg@8
|
36 |
@Override
|
franta-hg@8
|
37 |
public String format(LogRecord r) {
|
franta-hg@8
|
38 |
StringWriter sw = new StringWriter();
|
franta-hg@8
|
39 |
try (ColorfulPrintWriter out = new ColorfulPrintWriter(sw)) {
|
franta-hg@8
|
40 |
printLevel(out, r.getLevel());
|
franta-hg@8
|
41 |
printMessage(out, r);
|
franta-hg@8
|
42 |
printThrowable(out, r);
|
franta-hg@8
|
43 |
out.println();
|
franta-hg@8
|
44 |
}
|
franta-hg@8
|
45 |
return sw.toString();
|
franta-hg@8
|
46 |
}
|
franta-hg@8
|
47 |
|
franta-hg@8
|
48 |
private void printLevel(ColorfulPrintWriter out, Level l) {
|
franta-hg@8
|
49 |
TerminalColor color = TerminalColor.Magenta;
|
franta-hg@8
|
50 |
|
franta-hg@8
|
51 |
if (l == Level.SEVERE) {
|
franta-hg@8
|
52 |
color = TerminalColor.Red;
|
franta-hg@8
|
53 |
} else if (l == Level.WARNING) {
|
franta-hg@8
|
54 |
color = TerminalColor.Yellow;
|
franta-hg@8
|
55 |
}
|
franta-hg@8
|
56 |
|
franta-hg@8
|
57 |
out.print(color, rpad(l.getLocalizedName() + ": ", 10));
|
franta-hg@8
|
58 |
}
|
franta-hg@8
|
59 |
|
franta-hg@8
|
60 |
private void printMessage(ColorfulPrintWriter out, LogRecord r) {
|
franta-hg@8
|
61 |
out.print(formatMessage(r));
|
franta-hg@8
|
62 |
}
|
franta-hg@8
|
63 |
|
franta-hg@8
|
64 |
private void printThrowable(ColorfulPrintWriter out, LogRecord r) {
|
franta-hg@8
|
65 |
Throwable t = r.getThrown();
|
franta-hg@8
|
66 |
if (t != null) {
|
franta-hg@8
|
67 |
out.print(": ");
|
franta-hg@8
|
68 |
out.print(TerminalColor.Red, t.getClass().getSimpleName());
|
franta-hg@8
|
69 |
String message = t.getLocalizedMessage();
|
franta-hg@8
|
70 |
if (message != null) {
|
franta-hg@8
|
71 |
out.print(": ");
|
franta-hg@8
|
72 |
out.print(message);
|
franta-hg@8
|
73 |
}
|
franta-hg@8
|
74 |
if (printStacktrace) {
|
franta-hg@8
|
75 |
out.println();
|
franta-hg@8
|
76 |
out.setForegroundColor(TerminalColor.Yellow);
|
franta-hg@8
|
77 |
out.setStyle(TerminalStyle.Dim);
|
franta-hg@8
|
78 |
t.printStackTrace(out);
|
franta-hg@8
|
79 |
out.resetAll();
|
franta-hg@8
|
80 |
}
|
franta-hg@8
|
81 |
}
|
franta-hg@8
|
82 |
}
|
franta-hg@8
|
83 |
|
franta-hg@8
|
84 |
public boolean isPrintStacktrace() {
|
franta-hg@8
|
85 |
return printStacktrace;
|
franta-hg@8
|
86 |
}
|
franta-hg@8
|
87 |
|
franta-hg@8
|
88 |
public void setPrintStacktrace(boolean printStacktrace) {
|
franta-hg@8
|
89 |
this.printStacktrace = printStacktrace;
|
franta-hg@8
|
90 |
}
|
franta-hg@8
|
91 |
|
franta-hg@8
|
92 |
private static String rpad(String s, int n) {
|
franta-hg@8
|
93 |
if (n > 0) {
|
franta-hg@8
|
94 |
return String.format("%1$-" + n + "s", s);
|
franta-hg@8
|
95 |
} else {
|
franta-hg@8
|
96 |
return s;
|
franta-hg@8
|
97 |
}
|
franta-hg@8
|
98 |
}
|
franta-hg@8
|
99 |
|
franta-hg@8
|
100 |
}
|