forked from Anuken/Arc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.java
More file actions
134 lines (108 loc) · 3.83 KB
/
Log.java
File metadata and controls
134 lines (108 loc) · 3.83 KB
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
package arc.util;
import java.io.PrintWriter;
import java.io.StringWriter;
public class Log{
private static final Object[] empty = {};
public static boolean useColors = true;
public static LogLevel level = LogLevel.info;
public static LogHandler logger = new DefaultLogHandler();
public static LogFormatter formatter = new DefaultLogFormatter();
public static void log(LogLevel level, String text, Object... args){
if(Log.level.ordinal() > level.ordinal()) return;
logger.log(level, format(text, args));
}
public static void debug(String text, Object... args){
log(LogLevel.debug, text, args);
}
public static void debug(Object object){
debug(String.valueOf(object), empty);
}
public static void infoList(Object... args){
if(level.ordinal() > LogLevel.info.ordinal()) return;
StringBuilder build = new StringBuilder();
for(Object o : args){
build.append(o);
build.append(" ");
}
info(build.toString());
}
public static void infoTag(String tag, String text){
log(LogLevel.info, "[" + tag + "] " + text);
}
public static void info(String text, Object... args){
log(LogLevel.info, text, args);
}
public static void info(Object object){
info(String.valueOf(object), empty);
}
public static void warn(String text, Object... args){
log(LogLevel.warn, text, args);
}
public static void errTag(String tag, String text){
log(LogLevel.err, "[" + tag + "] " + text);
}
public static void err(String text, Object... args){
log(LogLevel.err, text, args);
}
public static void err(Throwable th){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
th.printStackTrace(pw);
err(sw.toString());
}
public static void err(String text, Throwable th){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
th.printStackTrace(pw);
err(text + ": " + sw);
}
public static String format(String text, Object... args){
return formatColors(text, useColors, args);
}
public static String formatColors(String text, boolean useColors, Object... args){
return formatter.format(text, useColors, args);
}
/** @deprecated Use {@link ColorCodes#apply(String, boolean)} instead. */
@Deprecated
public static String removeColors(String text){
return ColorCodes.apply(text, false);
}
/** @deprecated Use {@link ColorCodes#apply(String)} instead. */
@Deprecated
public static String addColors(String text){
return ColorCodes.apply(text, true);
}
public enum LogLevel{
debug,
info,
warn,
err,
none
}
public interface LogFormatter{
String format(String text, boolean useColors, Object... args);
}
public interface LogHandler{
void log(LogLevel level, String text);
}
public static class DefaultLogFormatter implements LogFormatter{
@Override
public String format(String text, boolean useColors, Object... args){
return ColorCodes.apply(Strings.format(text, args), useColors);
}
}
public static class DefaultLogHandler implements LogHandler{
@Override
public void log(LogLevel level, String text){
System.out.println(format((
level == LogLevel.debug ? "&lc&fb" :
level == LogLevel.info ? "&fb" :
level == LogLevel.warn ? "&ly&fb" :
level == LogLevel.err ? "&lr&fb" :
"") + text + "&fr"));
}
}
public static class NoopLogHandler implements LogHandler{
@Override public void log(LogLevel level, String text){}
}
}