1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.examples.stress;
18
19 import java.io.PrintStream;
20 import java.io.PrintWriter;
21 import java.io.StringWriter;
22
23
24
25
26
27
28 final class LogUtils {
29
30 private static LogLevel logLevel = LogLevel.INFO;
31
32
33
34
35 enum LogLevel {
36
37 ERROR(0),
38
39 INFO(1),
40
41 DEBUG(2);
42
43
44 private final String prefix;
45
46
47 private final int level;
48
49
50
51
52
53
54 LogLevel(int level) {
55
56 prefix = "[" + name() + "] ";
57 this.level = level;
58 }
59
60
61
62
63
64
65 String getPrefix() {
66 return prefix;
67 }
68
69
70
71
72
73
74 int getLevel() {
75 return level;
76 }
77 }
78
79
80
81
82 private LogUtils() {}
83
84
85
86
87
88
89 static void setLogLevel(LogLevel logLevel) {
90 LogUtils.logLevel = logLevel;
91 }
92
93
94
95
96
97
98
99 static boolean isLoggable(LogLevel level) {
100 return level.getLevel() <= LogUtils.logLevel.getLevel();
101 }
102
103
104
105
106
107
108 static void debug(String message) {
109 if (isLoggable(LogLevel.DEBUG)) {
110 println(System.out, LogLevel.DEBUG.getPrefix() + message);
111 }
112 }
113
114
115
116
117
118
119
120 static void debug(String format, Object... args) {
121 if (isLoggable(LogLevel.DEBUG)) {
122 printf(System.out, LogLevel.DEBUG.getPrefix() + format, args);
123 }
124 }
125
126
127
128
129
130 static void info(String message) {
131 if (isLoggable(LogLevel.INFO)) {
132 println(System.out, LogLevel.INFO.getPrefix() + message);
133 }
134 }
135
136
137
138
139
140
141
142 static void info(String format, Object... args) {
143 if (isLoggable(LogLevel.INFO)) {
144 printf(System.out, LogLevel.INFO.getPrefix() + format, args);
145 }
146 }
147
148
149
150
151
152
153 static void error(String message) {
154 if (isLoggable(LogLevel.ERROR)) {
155 println(System.err, LogLevel.ERROR.getPrefix() + message);
156 }
157 }
158
159
160
161
162
163
164
165 static void error(String format, Object... args) {
166 if (isLoggable(LogLevel.ERROR)) {
167 printf(System.err, LogLevel.ERROR.getPrefix() + format, args);
168 }
169 }
170
171
172
173
174
175
176
177 static void error(Throwable thrown, String message) {
178 if (isLoggable(LogLevel.ERROR)) {
179 final StringWriter sw = new StringWriter();
180 try (PrintWriter pw = createErrorPrintWriter(sw)) {
181 pw.print(message);
182 addStackTrace(pw, thrown);
183 }
184 println(System.err, sw.toString());
185 }
186 }
187
188
189
190
191
192
193
194
195
196 static void error(Throwable thrown, String format, Object... args) {
197 if (isLoggable(LogLevel.ERROR)) {
198 final StringWriter sw = new StringWriter();
199 try (PrintWriter pw = createErrorPrintWriter(sw)) {
200 pw.printf(format, args);
201 addStackTrace(pw, thrown);
202 }
203 printf(System.err, sw.toString());
204 }
205 }
206
207
208
209
210
211
212
213
214 private static PrintWriter createErrorPrintWriter(StringWriter sw) {
215 final PrintWriter pw = new PrintWriter(sw);
216 pw.print(LogLevel.ERROR.getPrefix());
217 return pw;
218 }
219
220
221
222
223
224
225
226 private static void addStackTrace(PrintWriter pw, Throwable thrown) {
227
228 pw.println();
229 thrown.printStackTrace(pw);
230 }
231
232
233
234
235
236
237
238
239 private static void println(PrintStream out, String message) {
240 out.println(message);
241 }
242
243
244
245
246
247
248
249
250
251 private static void printf(PrintStream out, String format, Object... args) {
252
253 out.printf(format + "%n", args);
254 }
255 }