Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/Log.java
40948 views
1
/*
2
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package nsk.share;
25
26
import java.io.BufferedReader;
27
import java.io.ByteArrayOutputStream;
28
import java.io.IOException;
29
import java.io.PrintStream;
30
import java.io.PrintWriter;
31
import java.io.StringReader;
32
import java.util.Enumeration;
33
import java.util.HashMap;
34
import java.util.Map;
35
import java.util.Set;
36
import java.util.HashSet;
37
import java.util.Vector;
38
39
import nsk.share.test.LazyFormatString;
40
41
/**
42
* This class helps to print test-execution trace messages
43
* and filter them when execution mode is not verbose.
44
* <p>
45
* Verbose mode if defined by providing <i>-verbose</i> command line
46
* option, handled by <code>ArgumentParser</code>. Use <code>verbose()</code>
47
* method to determine which mode is used.
48
* <p>
49
* <code>Log</code> provides with two main methods to print messages:
50
* <ul>
51
* <li> <code>complain(String)</code> - to print error message
52
* <li> <code>display(String)</code> - to print additional log message
53
* </ul>
54
* No other way to print messages to the log stream should be used.
55
* <p>
56
* Error messages appeares in log stream in all modes. Additional log massages,
57
* printed with <code>display()</code> method will be filtered out, if log mode
58
* is not verbose. In verbose log made messages of both types are printed.
59
* Additionally, in verbose mode a summary of all occured errors will be printed
60
* at the program exit, by automatically invoking method
61
* <code>printErrorsSummary()</code>.
62
* <p>
63
* To provide printing messages from different sources into one log
64
* with distinct prefixes use internal <code>Log.Logger</code> class.
65
*
66
* @see #verbose()
67
* @see #complain(String)
68
* @see #display(String)
69
* @see ArgumentParser
70
* @see Log.Logger
71
*/
72
public class Log extends FinalizableObject {
73
/**
74
* Report step-by-step activity to this stream.
75
*
76
* @deprecated Tests should not use this field directly.
77
*/
78
@Deprecated
79
protected PrintStream out = null;
80
81
/**
82
* Is log-mode verbose?
83
* Default value is <code>false</code>.
84
*/
85
private boolean verbose = false;
86
87
/**
88
* Should log messages prefixed with timestamps?
89
* Default value is <code>false</code>.
90
*/
91
private boolean timestamp = false;
92
93
/**
94
* Names for trace levels
95
*/
96
public static final class TraceLevel {
97
public static final int TRACE_NONE = 0;
98
public static final int TRACE_IMPORTANT = 1;
99
public static final int TRACE_NORMAL = 2;
100
public static final int TRACE_VERBOSE = 3;
101
public static final int TRACE_DEBUG = 4;
102
103
public static final int DEFAULT = TRACE_IMPORTANT;
104
105
public static final Map<String, Integer> NAME_TO_LEVEL_MAP = new HashMap<String, Integer>();
106
static {
107
NAME_TO_LEVEL_MAP.put("none", TRACE_NONE);
108
NAME_TO_LEVEL_MAP.put("important", TRACE_IMPORTANT);
109
NAME_TO_LEVEL_MAP.put("info", TRACE_NORMAL);
110
NAME_TO_LEVEL_MAP.put("verbose", TRACE_VERBOSE);
111
NAME_TO_LEVEL_MAP.put("debug", TRACE_DEBUG);
112
NAME_TO_LEVEL_MAP.put("default", DEFAULT);
113
}
114
115
public static int nameToLevel(String value) throws IllegalArgumentException {
116
Integer level = NAME_TO_LEVEL_MAP.get(value.toLowerCase());
117
if ( level == null )
118
throw new IllegalArgumentException("Wrong trace level: " + value);
119
120
return level;
121
}
122
123
public static String getLevelsString() {
124
StringBuffer result = new StringBuffer();
125
for ( String s : NAME_TO_LEVEL_MAP.keySet() ) {
126
result.append(s).append(", ");
127
}
128
return result.substring(0, result.length() - 3);
129
}
130
}
131
132
/**
133
* Threshold value for printing trace messages for debugging purpose.
134
* Default value is <code>0</code> a.k.a. <code>TraceLevel.INFO</code>;
135
*/
136
private int traceLevel = TraceLevel.DEFAULT;
137
138
/**
139
* Is printing errors summary enabled? Default value is <code>true</code>;
140
*/
141
private boolean errorsSummaryEnabled = true;
142
143
/**
144
* Is printing saved verbose messages on error enabled? Default value is <code>true</code>;
145
*/
146
private boolean verboseOnErrorEnabled = true;
147
148
/**
149
* This <code>errosBuffer</code> will keep all messages printed via
150
* <code>complain()</code> method for final summary output.
151
* Ensure that buffer has enough room for messages to keep,
152
* to minimize probability or OutOfMemory error while keeping
153
* an error message in stress tests.
154
*/
155
private Vector<String> errorsBuffer = new Vector<String>(1000);
156
157
/**
158
* Most tests in nsk do not log exceptions, they only log an error message.
159
* This makes failure analysis harder.
160
* To solve this we will automatically generate Exceptions for error logs.
161
* To not log too many Exceptions, we try to log each unique error only once.
162
* <code>loggedExceptions</code> contains all messages that have already been logged.
163
*/
164
private Set<String> loggedExceptions = new HashSet<String>();
165
166
/**
167
* This <code>logBuffer</code> will keep all messages printed via
168
* <code>display()</code> method in non-verbose mode until
169
* swithching verbose mode on or invoking <code>complain()</code>.
170
* Ensure that buffer has enough room for messages to keep,
171
* to minimize probability or OutOfMemory error while keeping
172
* an error message in stress tests.
173
*/
174
private Vector<String> logBuffer = new Vector<String>(1000);
175
176
/**
177
* Did I already warned if output stream is not assigned?
178
*/
179
private boolean noOutWarned = false;
180
181
/////////////////////////////////////////////////////////////////
182
183
/**
184
* Create new Log's only with <code>Log(out)</code> or with
185
* <code>Log(out,argsHandler)</code> constructors.
186
*
187
* @deprecated Extending test class with Log is obsolete.
188
*/
189
@Deprecated
190
protected Log() {
191
// install finalizer to print errors summary at exit
192
Finalizer finalizer = new Finalizer(this);
193
finalizer.activate();
194
195
// Don't log exceptions from this method. It would just add unnecessary logs.
196
loggedExceptions.add("nsk.share.jdi.SerialExecutionDebugger.executeTests");
197
}
198
199
/**
200
* Incarnate new Log for the given <code>stream</code> and
201
* for non-verbose mode.
202
*/
203
public Log(PrintStream stream) {
204
this();
205
out = stream;
206
}
207
208
/**
209
* Incarnate new Log for the given <code>stream</code>; and
210
* either for verbose or for non-verbose mode accordingly to
211
* the given <code>verbose</code> key.
212
*/
213
public Log(PrintStream stream, boolean verbose) {
214
this(stream);
215
this.verbose = verbose;
216
}
217
218
/**
219
* Incarnate new Log for the given <code>stream</code>; and
220
* either for verbose or for non-verbose mode accordingly to
221
* the given <code>argsHandler</code>.
222
*/
223
public Log(PrintStream stream, ArgumentParser argsParser) {
224
this(stream, argsParser.verbose());
225
traceLevel = argsParser.getTraceLevel();
226
timestamp = argsParser.isTimestamp();
227
}
228
229
/////////////////////////////////////////////////////////////////
230
231
/**
232
* Return <i>true</i> if log mode is verbose.
233
*/
234
public boolean verbose() {
235
return verbose;
236
}
237
238
/**
239
* Return <i>true</i> if printing errors summary at exit is enabled.
240
*/
241
public boolean isErrorsSummaryEnabled() {
242
return errorsSummaryEnabled;
243
}
244
245
/**
246
* Enable or disable printing errors summary at exit.
247
*/
248
public void enableErrorsSummary(boolean enable) {
249
errorsSummaryEnabled = enable;
250
}
251
252
/**
253
* Return <i>true</i> if printing saved verbose messages on error is enabled.
254
*/
255
public boolean isVerboseOnErrorEnabled() {
256
return errorsSummaryEnabled;
257
}
258
259
/**
260
* Enable or disable printing saved verbose messages on error.
261
*/
262
public void enableVerboseOnError(boolean enable) {
263
verboseOnErrorEnabled = enable;
264
}
265
266
/**
267
* Enable or disable verbose mode for printing messages.
268
*/
269
public void enableVerbose(boolean enable) {
270
if (!verbose) {
271
flushLogBuffer();
272
}
273
verbose = enable;
274
}
275
276
public int getTraceLevel() {
277
return traceLevel;
278
}
279
280
/**
281
* Set threshold for printing trace messages.
282
* Warning: trace level changes may NOT be observed by other threads immediately.
283
*/
284
public void setTraceLevel(int level) {
285
traceLevel = level;
286
}
287
288
/**
289
* Return output stream of this <code>Log</code> object.
290
*/
291
public PrintStream getOutStream() {
292
return out;
293
}
294
295
/**
296
* Returns a string that contains prefix concatenated
297
* with Throwable.printStackTrace() output.
298
*/
299
public static String printExceptionToString(Object prefix, Throwable exception) {
300
ByteArrayOutputStream bos = new ByteArrayOutputStream();
301
PrintWriter pw = new PrintWriter(bos);
302
pw.println(prefix);
303
exception.printStackTrace(pw);
304
pw.close();
305
return bos.toString();
306
}
307
308
/**
309
* Print <code>message</code> to the assigned output stream.
310
*
311
* @deprecated Test ought to be quiet if log mode is non-verbose
312
* and there is no errors found by the test. Methods
313
* <code>display()</code> and <code>complain()</code>
314
* are enough for testing purposes.
315
*/
316
@Deprecated
317
public synchronized void println(String message) {
318
doPrint(message);
319
if (!verbose() && isVerboseOnErrorEnabled()) {
320
keepLog(composeLine(message));
321
}
322
}
323
324
/**
325
* Print <code>message</code> to the assigned output stream,
326
* if log mode is <i>non</i>-verbose.
327
*
328
* @deprecated Test ought to be quiet if log mode is non-verbose
329
* and there is no errors found by the test. Methods
330
* <code>display()</code> and <code>complain()</code>
331
* are enough for testing purposes.
332
*/
333
@Deprecated
334
public synchronized void comment(String message) {
335
if (!verbose()) {
336
doPrint(message);
337
}
338
}
339
340
/**
341
* Print trace <code>message</code> to the assigned output stream,
342
* only if specified <code>level</code> is less or equal for the
343
* trace level specified in command line by <code>-trace.level</code>
344
* option.
345
*/
346
public void trace(int level, Object message) {
347
if (level <= traceLevel) {
348
synchronized ( this ) {
349
doPrint("### TRACE " + level + ": " + message);
350
}
351
}
352
}
353
/**
354
* Print trace <code>message</code> and <code>exception</code> to
355
* the assigned output stream,
356
* only if specified <code>level</code> is less or equal for the
357
* trace level specified in command line by <code>-trace.level</code>
358
* option.
359
*/
360
public void trace(int level, Object message, Throwable exception) {
361
if (level <= traceLevel) {
362
trace(level, printExceptionToString(message, exception));
363
}
364
}
365
366
/**
367
* Print <code>message</code> to the assigned output stream,
368
* if log mode is verbose. The <code>message</code> will be lost,
369
* if execution mode is non-verbose, and there is no error messages
370
* printed.
371
*/
372
public synchronized void display(Object message) {
373
if (verbose()) {
374
doPrint(message.toString());
375
} else if (isVerboseOnErrorEnabled()) {
376
keepLog(composeLine(message.toString()));
377
} else {
378
// ignore
379
}
380
}
381
382
/**
383
* Print error <code>message</code> to the assigned output stream
384
* (or to stderr if output is not specified) and keep the message
385
* into <code>errorsBuffer</code>.
386
*/
387
public synchronized void complain(Object message) {
388
if (!verbose() && isVerboseOnErrorEnabled()) {
389
PrintStream stream = findOutStream();
390
stream.println("#> ");
391
stream.println("#> WARNING: switching log to verbose mode,");
392
stream.println("#> because error is complained");
393
stream.println("#> ");
394
stream.flush();
395
enableVerbose(true);
396
}
397
String msgStr = message.toString();
398
printError(msgStr);
399
if (isErrorsSummaryEnabled()) {
400
keepError(msgStr);
401
}
402
403
logExceptionForFailureAnalysis(msgStr);
404
}
405
406
/**
407
* Print error <code>message</code> and <code>exception</code>
408
* to the assigned output stream
409
* (or to stderr if output is not specified) and keep the message
410
* into <code>errorsBuffer</code>.
411
*/
412
public void complain(Object message, Throwable exception) {
413
if ( exception != null )
414
complain(printExceptionToString(message, exception));
415
else
416
complain(message);
417
}
418
419
/**
420
* Create an Exception and print the stack trace for an error msg.
421
* This makes it possible to detect a failure reason for this error.
422
*/
423
private void logExceptionForFailureAnalysis(String msg) {
424
// Some error messages are formatted in multiple lines and with tabs.
425
// We clean the messages to help parse the stack traces for failure analysis.
426
// We keep at most 2 lines, otherwise the error message may be too long.
427
String[] lines = msg.split("[\r\n]+");
428
msg = lines.length >= 2 ? lines[0] + " " + lines[1] : lines[0];
429
msg = msg.replaceAll("\t", " ");
430
431
// Create a dummy exception just so we can print the stack trace.
432
TestFailure e = new TestFailure(msg);
433
StackTraceElement[] elements = e.getStackTrace();
434
435
final int callerIndex = 2; // 0=this function, 1=complain(), 2=caller
436
if (elements.length <= callerIndex) {
437
return;
438
}
439
440
// Only log the first complain message from each function.
441
// The reason is that some functions splits an error message
442
// into multiple lines and call complain() many times.
443
// We do not want a RULE for each of those calls.
444
// This means that we may miss some rules, but that
445
// is better than to create far too many rules.
446
String callerClass = elements[callerIndex].getClassName();
447
String callerMethod = elements[callerIndex].getMethodName();
448
String callerKey = callerClass + "." + callerMethod;
449
boolean isAlreadyLogged = loggedExceptions.contains(msg) || loggedExceptions.contains(callerKey);
450
451
if (!isAlreadyLogged) {
452
PrintStream stream = findOutStream();
453
stream.println("The following stacktrace is for failure analysis.");
454
e.printStackTrace(stream);
455
}
456
457
loggedExceptions.add(callerKey);
458
loggedExceptions.add(msg);
459
}
460
461
/////////////////////////////////////////////////////////////////
462
463
/**
464
* Redirect log to the given <code>stream</code>, and switch
465
* log mode to verbose.
466
* Prints errors summary to current stream, cancel current stream
467
* and switches to new stream. Turns on verbose mode for new stream.
468
*
469
* @deprecated This method is obsolete.
470
*/
471
@Deprecated
472
protected synchronized void logTo(PrintStream stream) {
473
finalize(); // flush older log stream
474
out = stream;
475
verbose = true;
476
}
477
478
/////////////////////////////////////////////////////////////////
479
480
/**
481
* Print all messages from log buffer which were hidden because
482
* of non-verbose mode,
483
*/
484
private synchronized void flushLogBuffer() {
485
if (!logBuffer.isEmpty()) {
486
PrintStream stream = findOutStream();
487
for (int i = 0; i < logBuffer.size(); i++) {
488
stream.println(logBuffer.elementAt(i));
489
}
490
stream.flush();
491
}
492
}
493
494
/**
495
* Return <code>out</code> stream if defined or <code>Sytem.err<code> otherwise;
496
* print a warning message when <code>System.err</code> is used first time.
497
*/
498
private synchronized PrintStream findOutStream() {
499
PrintStream stream = out;
500
if (stream == null) {
501
stream = System.err;
502
if (!noOutWarned) {
503
noOutWarned = true;
504
stream.println("#> ");
505
stream.println("#> WARNING: switching log stream to stderr,");
506
stream.println("#> because no output stream is assigned");
507
stream.println("#> ");
508
};
509
};
510
stream.flush();
511
return stream;
512
}
513
514
/**
515
* Compose line to print possible prefixing it with timestamp.
516
*/
517
private String composeLine(String message) {
518
if (timestamp) {
519
long time = System.currentTimeMillis();
520
long ms = time % 1000;
521
time /= 1000;
522
long secs = time % 60;
523
time /= 60;
524
long mins = time % 60;
525
time /= 60;
526
long hours = time % 24;
527
return "[" + hours + ":" + mins + ":" + secs + "." + ms + "] " + message;
528
}
529
return message;
530
}
531
532
/**
533
* Print the given <code>message</code> either to <code>out</code>
534
* stream, or to <code>System.err</code> if <code>out</code>
535
* stream is not specified.
536
*/
537
private synchronized void doPrint(String message) {
538
PrintStream stream = findOutStream();
539
stream.println(composeLine(message));
540
stream.flush();
541
}
542
543
/**
544
* Print the given error <code>message</code> either to <code>out</code>
545
* stream, or to <code>System.err</code> if <code>out</code>
546
* stream is not specified.
547
*/
548
private synchronized void printError(String message) {
549
// Print each line with the ERROR prefix:
550
BufferedReader br = new BufferedReader(new StringReader(message));
551
for (String line; ; ) {
552
try {
553
line = br.readLine();
554
if (line == null)
555
break;
556
doPrint("# ERROR: " + line);
557
} catch (IOException e) {
558
throw new TestBug("Exception in Log.printError(): " + e);
559
};
560
}
561
}
562
563
/**
564
* Keep the given log <code>message</code> into <code>logBuffer</code>.
565
*/
566
private synchronized void keepLog(String message) {
567
logBuffer.addElement(message);
568
}
569
570
/**
571
* Keep the given error <code>message</code> into <code>errorsBuffer</code>.
572
*/
573
private synchronized void keepError(String message) {
574
errorsBuffer.addElement(message);
575
}
576
577
/**
578
* Print errors messages summary from errors buffer if any;
579
* print a warning message first.
580
*/
581
private synchronized void printErrorsSummary() {
582
if (errorsBuffer.size() <= 0)
583
return;
584
585
PrintStream stream = findOutStream();
586
stream.println();
587
stream.println();
588
stream.println("#> ");
589
stream.println("#> SUMMARY: Following errors occured");
590
stream.println("#> during test execution:");
591
stream.println("#> ");
592
stream.flush();
593
594
for (Enumeration e = errorsBuffer.elements(); e.hasMoreElements(); ) {
595
printError((String) e.nextElement());
596
}
597
}
598
599
/**
600
* Print errors summary if mode is verbose, flush and cancel output stream.
601
*/
602
protected void finalize() {
603
if (verbose() && isErrorsSummaryEnabled()) {
604
printErrorsSummary();
605
}
606
if (out != null)
607
out.flush();
608
out = null;
609
}
610
611
/**
612
* Perform finalization at the exit.
613
*/
614
public void finalizeAtExit() {
615
finalize();
616
}
617
618
/**
619
* This class can be used as a base for each class that use <code>Log</code>
620
* for print messages and errors.
621
* <code>Logger</code> provides with ability to print such messages with
622
* specified prefix to make it possible to distinct messages printed from
623
* different sources.
624
*
625
* @see Log
626
*/
627
public static class Logger {
628
629
/**
630
* Default prefix for messages.
631
*/
632
public static final String LOG_PREFIX = "";
633
634
/**
635
* Log to print messages to.
636
*/
637
protected Log log = null;
638
639
/**
640
* Prefix for messages.
641
*/
642
protected String logPrefix = LOG_PREFIX;
643
644
/**
645
* Make <code>Logger</code> object with empty <code>Log</code> and
646
* default prefix.
647
* This method may be used only in derived class, that should specify
648
* the used <code>Log</code> object further and assign it to <code>log</code>.
649
*
650
* @see #log
651
* @see #setLogPrefix
652
*/
653
protected Logger() {
654
}
655
656
/**
657
* Make <code>Logger</code> object for specified <code>Log</code>
658
* with default prefix.
659
*
660
* @see #setLogPrefix
661
*/
662
public Logger(Log log) {
663
this.log = log;
664
}
665
666
/**
667
* Make <code>Logger</code> object for specified <code>Log</code> with
668
* given messages prefix.
669
*/
670
public Logger(Log log, String prefix) {
671
this.log = log;
672
this.logPrefix = prefix;
673
}
674
675
/**
676
* Return <code>Log</code> object.
677
*/
678
public Log getLog() {
679
return log;
680
}
681
682
/**
683
* Return output stream of this <code>Log</code> object.
684
*/
685
public PrintStream getOutStream() {
686
return log.getOutStream();
687
}
688
689
/**
690
* Set prefix for printed messages.
691
*/
692
public void setLogPrefix(String prefix) {
693
logPrefix = prefix;
694
}
695
696
/**
697
* Make printable message by adding <code>logPrefix<code> to it.
698
*/
699
public String makeLogMessage(String message) {
700
return logPrefix + message;
701
}
702
703
/**
704
* Print trace message by invoking <code>Log.trace()</code>.
705
*
706
* @see Log#trace
707
*/
708
public void trace(int level, String message) {
709
log.trace(level, makeLogMessage(message));
710
}
711
712
/**
713
* Print message by invoking <code>Log.println()</code>.
714
*
715
* @see Log#println
716
*/
717
public void println(String message) {
718
log.println(makeLogMessage(message));
719
}
720
721
/**
722
* Print message by invoking <code>Log.display()</code>.
723
*
724
* @see Log#display
725
*/
726
public void display(String message) {
727
log.display(makeLogMessage(message));
728
}
729
730
/**
731
* Complain about an error by invoking <code>Log.complain()</code> method.
732
*
733
* @see Log#complain
734
*/
735
public void complain(String message) {
736
log.complain(makeLogMessage(message));
737
}
738
739
}
740
741
}
742
743