forked from dita-ot/dita-ot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultLogger.java
453 lines (415 loc) · 13.5 KB
/
DefaultLogger.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* This file is part of the DITA Open Toolkit project.
*
* Copyright 2013 Jarno Elovirta
*
* See the accompanying LICENSE file for applicable license.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dita.dost.invoker;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;
import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.DateUtils;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.StringUtils;
import org.dita.dost.exception.DITAOTException;
import org.dita.dost.log.AbstractLogger;
/**
* Writes build events to a PrintStream. Currently, it only writes which targets
* are being executed, and any messages that get logged.
*
*/
class DefaultLogger extends AbstractLogger implements BuildLogger {
/**
* Size of left-hand column for right-justified task name.
*
* @see #messageLogged(BuildEvent)
*/
private static final int LEFT_COLUMN_SIZE = 12;
// CheckStyle:VisibilityModifier OFF - bc
/** PrintStream to write non-error messages to */
private PrintStream out;
/** PrintStream to write error messages to */
private PrintStream err;
//** Lowest level of message to write out */
// private int msgOutputLevel = Project.MSG_ERR;
/** Time of the start of the build */
private long startTime = System.currentTimeMillis();
// CheckStyle:ConstantNameCheck OFF - bc
//** Line separator */
// protected static final String lSep = StringUtils.LINE_SEP;
// CheckStyle:ConstantNameCheck ON
/** Whether or not to use emacs-style output */
private boolean emacsMode = false;
// private boolean useColor = false;
// CheckStyle:VisibilityModifier ON
/**
* Sole constructor.
*/
public DefaultLogger() {
msgOutputLevel = Project.MSG_ERR;
}
/**
* Sets the highest level of message this logger should respond to.
* <p>
* Only messages with a message level lower than or equal to the given level
* should be written to the log.
* <p>
* Constants for the message levels are in the {@link Project Project}
* class. The order of the levels, from least to most verbose, is
* <code>MSG_ERR</code>, <code>MSG_WARN</code>, <code>MSG_INFO</code>,
* <code>MSG_VERBOSE</code>, <code>MSG_DEBUG</code>.
* <p>
* The default message level for DefaultLogger is Project.MSG_ERR.
*
* @param level the logging level for the logger.
*/
@Override
public void setMessageOutputLevel(final int level) {
msgOutputLevel = level;
}
/**
* Sets the output stream to which this logger is to send its output.
*
* @param output The output stream for the logger. Must not be
* <code>null</code>.
*/
@Override
public void setOutputPrintStream(final PrintStream output) {
out = new PrintStream(output, true);
}
/**
* Sets the output stream to which this logger is to send error messages.
*
* @param err The error stream for the logger. Must not be <code>null</code>
* .
*/
@Override
public void setErrorPrintStream(final PrintStream err) {
this.err = new PrintStream(err, true);
}
/**
* Sets this logger to produce emacs (and other editor) friendly output.
*
* @param emacsMode <code>true</code> if output is to be unadorned so that
* emacs and other editors can parse files names, etc.
*/
@Override
public void setEmacsMode(final boolean emacsMode) {
this.emacsMode = emacsMode;
}
public void useColor(final boolean useColor) {
this.useColor = useColor;
}
/**
* Responds to a build being started by just remembering the current time.
*
* @param event Ignored.
*/
@Override
public void buildStarted(final BuildEvent event) {
startTime = System.currentTimeMillis();
}
private static void throwableMessage(final StringBuilder m, final Throwable error, final boolean verbose) {
String msg = error.getMessage();
final int i = msg.indexOf(": ");
if (i != -1) {
msg = msg.substring(i + 1).trim();
}
m.append(msg);
// m.append(lSep);
}
/**
* Prints whether the build succeeded or failed, any errors the occurred
* during the build, and how long the build took.
*
* @param event An event with any relevant extra information. Must not be
* <code>null</code>.
*/
@Override
public void buildFinished(final BuildEvent event) {
Throwable error = event.getException();
for (var e = error; e != null; e = e.getCause()) {
if (e instanceof DITAOTException) {
error = e;
break;
}
}
final StringBuilder message = new StringBuilder();
if (error == null) {
if (msgOutputLevel >= Project.MSG_INFO) {
message.append(StringUtils.LINE_SEP);
if (useColor) {
message.append(ANSI_BOLD).append(ANSI_GREEN);
}
message.append(getBuildSuccessfulMessage());
if (useColor) {
message.append(ANSI_RESET);
}
message.append(" in ").append(formatTime(System.currentTimeMillis() - startTime));
}
} else {
if (useColor) {
message.append(ANSI_RED);
}
message.append(Main.locale.getString("error_msg").formatted(""));
if (useColor) {
message.append(ANSI_RESET);
}
if (error instanceof DITAOTException && msgOutputLevel < Project.MSG_INFO) {
message.append(Main.locale.getString("exception_msg").formatted(error.getMessage()));
} else {
try (var buf = new StringWriter(); var printWriter = new PrintWriter(buf)) {
error.printStackTrace(printWriter);
printWriter.flush();
message.append(Main.locale.getString("exception_msg").formatted(buf));
} catch (IOException e) {
// Failed to print stack trace
}
}
if (msgOutputLevel >= Project.MSG_INFO) {
message.append(StringUtils.LINE_SEP);
if (useColor) {
message.append(ANSI_BOLD).append(ANSI_RED);
}
message.append(getBuildFailedMessage());
if (useColor) {
message.append(ANSI_RESET);
}
message.append(" in ").append(formatTime(System.currentTimeMillis() - startTime));
}
}
// message.append(StringUtils.LINE_SEP);
// message.append("Total time: ");
// message.append(formatTime(System.currentTimeMillis() - startTime));
final String msg = message.toString();
if (error == null && !msg.trim().isEmpty()) {
out.println(msg);
} else if (!msg.isEmpty()) {
if (legacyFormat) {
err.println(msg);
} else {
err.println(removeLevelPrefix(message));
}
}
log(msg);
}
/**
* This is an override point: the message that indicates whether a build
* failed. Subclasses can change/enhance the message.
*
* @return The classic "BUILD FAILED"
*/
protected String getBuildFailedMessage() {
return "BUILD FAILED";
}
/**
* This is an override point: the message that indicates that a build
* succeeded. Subclasses can change/enhance the message.
*
* @return The classic "BUILD SUCCESSFUL"
*/
protected String getBuildSuccessfulMessage() {
return "BUILD SUCCESSFUL";
}
private boolean evaluate(final Project project, final String condition) {
final String value = project.replaceProperties(condition);
return switch (value) {
case "true" -> true;
case "false" -> false;
default -> project.getProperty(value) != null || project.getUserProperty(value) != null;
};
}
/**
* Logs a message to say that the target has started if this logger allows
* information-level messages.
*
* @param event An event with any relevant extra information. Must not be
* <code>null</code>.
*/
@Override
public void targetStarted(final BuildEvent event) {
if (event.getTarget().getIf() != null && !evaluate(event.getProject(), event.getTarget().getIf())) {
return;
}
if (event.getTarget().getUnless() != null && evaluate(event.getProject(), event.getTarget().getUnless())) {
return;
}
if (Project.MSG_INFO <= msgOutputLevel && !event.getTarget().getName().equals("")) {
final String msg;
if (event.getTarget().getDescription() == null) {
msg = null;
} else {
var buf = new StringBuilder().append(StringUtils.LINE_SEP);
if (useColor) {
buf.append(ANSI_BLUE);
}
buf.append("==> ");
if (useColor) {
buf.append(ANSI_RESET).append(ANSI_BOLD);
}
buf.append(event.getTarget().getDescription());
if (useColor) {
buf.append(ANSI_RESET);
}
msg = buf.toString();
}
if (msg != null) {
out.println(msg);
log(msg);
}
}
}
/**
* No-op implementation.
*
* @param event Ignored.
*/
@Override
public void targetFinished(final BuildEvent event) {}
/**
* No-op implementation.
*
* @param event Ignored.
*/
@Override
public void taskStarted(final BuildEvent event) {}
/**
* No-op implementation.
*
* @param event Ignored.
*/
@Override
public void taskFinished(final BuildEvent event) {}
/**
* Logs a message, if the priority is suitable. In non-emacs mode, task
* level messages are prefixed by the task name which is right-justified.
*
* @param event A BuildEvent containing message information. Must not be
* <code>null</code>.
*/
@Override
public void messageLogged(final BuildEvent event) {
final int priority = event.getPriority();
// Filter out messages based on priority
if (priority <= msgOutputLevel) {
final StringBuilder message = new StringBuilder();
if (event.getTask() != null && !emacsMode) {
// Print out the name of the task if we're in one
final String name = event.getTask().getTaskName();
String label = "[" + name + "] ";
final int size = LEFT_COLUMN_SIZE - label.length();
final StringBuilder tmp = new StringBuilder();
tmp.append(" ".repeat(Math.max(0, size)));
tmp.append(label);
label = tmp.toString();
BufferedReader r = null;
try {
r = new BufferedReader(new StringReader(event.getMessage()));
String line = r.readLine();
boolean first = true;
do {
if (first) {
if (line == null) {
message.append(label);
break;
}
} else {
message.append(StringUtils.LINE_SEP);
}
first = false;
message.append(label).append(line);
line = r.readLine();
} while (line != null);
} catch (final IOException e) {
// shouldn't be possible
message.append(label).append(event.getMessage());
} finally {
if (r != null) {
FileUtils.close(r);
}
}
} else {
// emacs mode or there is no task
message.append(event.getMessage());
}
final Throwable ex = event.getException();
if (Project.MSG_DEBUG <= msgOutputLevel && ex != null) {
message.append(StringUtils.getStackTrace(ex));
}
final String msg = message.toString();
final PrintStream dst = priority == Project.MSG_ERR ? err : out;
if (legacyFormat) {
dst.println(msg);
} else {
dst.println(removeLevelPrefix(new StringBuilder(msg)));
}
log(msg);
}
}
/**
* Convenience method to format a specified length of time.
*
* @param millis Length of time to format, in milliseconds.
*
* @return the time as a formatted string.
*
* @see DateUtils#formatElapsedTime(long)
*/
protected static String formatTime(final long millis) {
return DateUtils.formatElapsedTime(millis);
}
/**
* Empty implementation which allows subclasses to receive the same output
* that is generated here.
*
* @param message Message being logged. Should not be <code>null</code>.
*/
private void log(final String message) {}
/**
* Get the current time.
*
* @return the current time as a formatted string.
* @since Ant1.7.1
*/
protected String getTimestamp() {
final Date date = new Date(System.currentTimeMillis());
final DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
return formatter.format(date);
}
/**
* Get the project name or null
*
* @param event the event
* @return the project that raised this event
* @since Ant1.7.1
*/
protected String extractProjectName(final BuildEvent event) {
final Project project = event.getProject();
return (project != null) ? project.getName() : null;
}
@Override
public void log(String msg, Throwable t, int level) {
throw new UnsupportedOperationException();
}
}