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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
/*
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package nsk.share;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.Vector;
import nsk.share.test.LazyFormatString;
/**
* This class helps to print test-execution trace messages
* and filter them when execution mode is not verbose.
* <p>
* Verbose mode if defined by providing <i>-verbose</i> command line
* option, handled by <code>ArgumentParser</code>. Use <code>verbose()</code>
* method to determine which mode is used.
* <p>
* <code>Log</code> provides with two main methods to print messages:
* <ul>
* <li> <code>complain(String)</code> - to print error message
* <li> <code>display(String)</code> - to print additional log message
* </ul>
* No other way to print messages to the log stream should be used.
* <p>
* Error messages appeares in log stream in all modes. Additional log massages,
* printed with <code>display()</code> method will be filtered out, if log mode
* is not verbose. In verbose log made messages of both types are printed.
* Additionally, in verbose mode a summary of all occured errors will be printed
* at the program exit, by automatically invoking method
* <code>printErrorsSummary()</code>.
* <p>
* To provide printing messages from different sources into one log
* with distinct prefixes use internal <code>Log.Logger</code> class.
*
* @see #verbose()
* @see #complain(String)
* @see #display(String)
* @see ArgumentParser
* @see Log.Logger
*/
public class Log extends FinalizableObject {
/**
* Report step-by-step activity to this stream.
*
* @deprecated Tests should not use this field directly.
*/
protected PrintStream out = null;
/**
* Is log-mode verbose?
* Default value is <code>false</code>.
*/
private boolean verbose = false;
/**
* Should log messages prefixed with timestamps?
* Default value is <code>false</code>.
*/
private boolean timestamp = false;
/**
* Names for trace levels
*/
public static final class TraceLevel {
public static final int TRACE_NONE = 0;
public static final int TRACE_IMPORTANT = 1;
public static final int TRACE_NORMAL = 2;
public static final int TRACE_VERBOSE = 3;
public static final int TRACE_DEBUG = 4;
public static final int DEFAULT = TRACE_IMPORTANT;
public static final Map<String, Integer> NAME_TO_LEVEL_MAP = new HashMap<String, Integer>();
static {
NAME_TO_LEVEL_MAP.put("none", TRACE_NONE);
NAME_TO_LEVEL_MAP.put("important", TRACE_IMPORTANT);
NAME_TO_LEVEL_MAP.put("info", TRACE_NORMAL);
NAME_TO_LEVEL_MAP.put("verbose", TRACE_VERBOSE);
NAME_TO_LEVEL_MAP.put("debug", TRACE_DEBUG);
NAME_TO_LEVEL_MAP.put("default", DEFAULT);
}
public static int nameToLevel(String value) throws IllegalArgumentException {
Integer level = NAME_TO_LEVEL_MAP.get(value.toLowerCase());
if ( level == null )
throw new IllegalArgumentException("Wrong trace level: " + value);
return level;
}
public static String getLevelsString() {
StringBuffer result = new StringBuffer();
for ( String s : NAME_TO_LEVEL_MAP.keySet() ) {
result.append(s).append(", ");
}
return result.substring(0, result.length() - 3);
}
}
/**
* Threshold value for printing trace messages for debugging purpose.
* Default value is <code>0</code> a.k.a. <code>TraceLevel.INFO</code>;
*/
private int traceLevel = TraceLevel.DEFAULT;
/**
* Is printing errors summary enabled? Default value is <code>true</code>;
*/
private boolean errorsSummaryEnabled = true;
/**
* Is printing saved verbose messages on error enabled? Default value is <code>true</code>;
*/
private boolean verboseOnErrorEnabled = true;
/**
* This <code>errosBuffer</code> will keep all messages printed via
* <code>complain()</code> method for final summary output.
* Ensure that buffer has enough room for messages to keep,
* to minimize probability or OutOfMemory error while keeping
* an error message in stress tests.
*/
private Vector<String> errorsBuffer = new Vector<String>(1000);
/**
* Most tests in nsk do not log exceptions, they only log an error message.
* This makes failure analysis harder.
* To solve this we will automatically generate Exceptions for error logs.
* To not log too many Exceptions, we try to log each unique error only once.
* <code>loggedExceptions</code> contains all messages that have already been logged.
*/
private Set<String> loggedExceptions = new HashSet<String>();
/**
* This <code>logBuffer</code> will keep all messages printed via
* <code>display()</code> method in non-verbose mode until
* swithching verbose mode on or invoking <code>complain()</code>.
* Ensure that buffer has enough room for messages to keep,
* to minimize probability or OutOfMemory error while keeping
* an error message in stress tests.
*/
private Vector<String> logBuffer = new Vector<String>(1000);
/**
* Did I already warned if output stream is not assigned?
*/
private boolean noOutWarned = false;
/////////////////////////////////////////////////////////////////
/**
* Create new Log's only with <code>Log(out)</code> or with
* <code>Log(out,argsHandler)</code> constructors.
*
* @deprecated Extending test class with Log is obsolete.
*/
protected Log() {
// install finalizer to print errors summary at exit
Finalizer finalizer = new Finalizer(this);
finalizer.activate();
// Don't log exceptions from this method. It would just add unnecessary logs.
loggedExceptions.add("nsk.share.jdi.SerialExecutionDebugger.executeTests");
}
/**
* Incarnate new Log for the given <code>stream</code> and
* for non-verbose mode.
*/
public Log(PrintStream stream) {
this();
out = stream;
}
/**
* Incarnate new Log for the given <code>stream</code>; and
* either for verbose or for non-verbose mode accordingly to
* the given <code>verbose</code> key.
*/
public Log(PrintStream stream, boolean verbose) {
this(stream);
this.verbose = verbose;
}
/**
* Incarnate new Log for the given <code>stream</code>; and
* either for verbose or for non-verbose mode accordingly to
* the given <code>argsHandler</code>.
*/
public Log(PrintStream stream, ArgumentParser argsParser) {
this(stream, argsParser.verbose());
traceLevel = argsParser.getTraceLevel();
timestamp = argsParser.isTimestamp();
}
/////////////////////////////////////////////////////////////////
/**
* Return <i>true</i> if log mode is verbose.
*/
public boolean verbose() {
return verbose;
}
/**
* Return <i>true</i> if printing errors summary at exit is enabled.
*/
public boolean isErrorsSummaryEnabled() {
return errorsSummaryEnabled;
}
/**
* Enable or disable printing errors summary at exit.
*/
public void enableErrorsSummary(boolean enable) {
errorsSummaryEnabled = enable;
}
/**
* Return <i>true</i> if printing saved verbose messages on error is enabled.
*/
public boolean isVerboseOnErrorEnabled() {
return errorsSummaryEnabled;
}
/**
* Enable or disable printing saved verbose messages on error.
*/
public void enableVerboseOnError(boolean enable) {
verboseOnErrorEnabled = enable;
}
/**
* Enable or disable verbose mode for printing messages.
*/
public void enableVerbose(boolean enable) {
if (!verbose) {
flushLogBuffer();
}
verbose = enable;
}
public int getTraceLevel() {
return traceLevel;
}
/**
* Set threshold for printing trace messages.
* Warning: trace level changes may NOT be observed by other threads immediately.
*/
public void setTraceLevel(int level) {
traceLevel = level;
}
/**
* Return output stream of this <code>Log</code> object.
*/
public PrintStream getOutStream() {
return out;
}
/**
* Returns a string that contains prefix concatenated
* with Throwable.printStackTrace() output.
*/
public static String printExceptionToString(Object prefix, Throwable exception) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(bos);
pw.println(prefix);
exception.printStackTrace(pw);
pw.close();
return bos.toString();
}
/**
* Print <code>message</code> to the assigned output stream.
*
* @deprecated Test ought to be quiet if log mode is non-verbose
* and there is no errors found by the test. Methods
* <code>display()</code> and <code>complain()</code>
* are enough for testing purposes.
*/
public synchronized void println(String message) {
doPrint(message);
if (!verbose() && isVerboseOnErrorEnabled()) {
keepLog(composeLine(message));
}
}
/**
* Print <code>message</code> to the assigned output stream,
* if log mode is <i>non</i>-verbose.
*
* @deprecated Test ought to be quiet if log mode is non-verbose
* and there is no errors found by the test. Methods
* <code>display()</code> and <code>complain()</code>
* are enough for testing purposes.
*/
public synchronized void comment(String message) {
if (!verbose()) {
doPrint(message);
}
}
/**
* Print trace <code>message</code> to the assigned output stream,
* only if specified <code>level</code> is less or equal for the
* trace level specified in command line by <code>-trace.level</code>
* option.
*/
public void trace(int level, Object message) {
if (level <= traceLevel) {
synchronized ( this ) {
doPrint("### TRACE " + level + ": " + message);
}
}
}
/**
* Print trace <code>message</code> and <code>exception</code> to
* the assigned output stream,
* only if specified <code>level</code> is less or equal for the
* trace level specified in command line by <code>-trace.level</code>
* option.
*/
public void trace(int level, Object message, Throwable exception) {
if (level <= traceLevel) {
trace(level, printExceptionToString(message, exception));
}
}
/**
* Print <code>message</code> to the assigned output stream,
* if log mode is verbose. The <code>message</code> will be lost,
* if execution mode is non-verbose, and there is no error messages
* printed.
*/
public synchronized void display(Object message) {
if (verbose()) {
doPrint(message.toString());
} else if (isVerboseOnErrorEnabled()) {
keepLog(composeLine(message.toString()));
} else {
// ignore
}
}
/**
* Print error <code>message</code> to the assigned output stream
* (or to stderr if output is not specified) and keep the message
* into <code>errorsBuffer</code>.
*/
public synchronized void complain(Object message) {
if (!verbose() && isVerboseOnErrorEnabled()) {
PrintStream stream = findOutStream();
stream.println("#> ");
stream.println("#> WARNING: switching log to verbose mode,");
stream.println("#> because error is complained");
stream.println("#> ");
stream.flush();
enableVerbose(true);
}
String msgStr = message.toString();
printError(msgStr);
if (isErrorsSummaryEnabled()) {
keepError(msgStr);
}
logExceptionForFailureAnalysis(msgStr);
}
/**
* Print error <code>message</code> and <code>exception</code>
* to the assigned output stream
* (or to stderr if output is not specified) and keep the message
* into <code>errorsBuffer</code>.
*/
public void complain(Object message, Throwable exception) {
if ( exception != null )
complain(printExceptionToString(message, exception));
else
complain(message);
}
/**
* Create an Exception and print the stack trace for an error msg.
* This makes it possible to detect a failure reason for this error.
*/
private void logExceptionForFailureAnalysis(String msg) {
// Some error messages are formatted in multiple lines and with tabs.
// We clean the messages to help parse the stack traces for failure analysis.
// We keep at most 2 lines, otherwise the error message may be too long.
String[] lines = msg.split("[\r\n]+");
msg = lines.length >= 2 ? lines[0] + " " + lines[1] : lines[0];
msg = msg.replaceAll("\t", " ");
// Create a dummy exception just so we can print the stack trace.
TestFailure e = new TestFailure(msg);
StackTraceElement[] elements = e.getStackTrace();
final int callerIndex = 2; // 0=this function, 1=complain(), 2=caller
if (elements.length <= callerIndex) {
return;
}
// Only log the first complain message from each function.
// The reason is that some functions splits an error message
// into multiple lines and call complain() many times.
// We do not want a RULE for each of those calls.
// This means that we may miss some rules, but that
// is better than to create far too many rules.
String callerClass = elements[callerIndex].getClassName();
String callerMethod = elements[callerIndex].getMethodName();
String callerKey = callerClass + "." + callerMethod;
boolean isAlreadyLogged = loggedExceptions.contains(msg) || loggedExceptions.contains(callerKey);
if (!isAlreadyLogged) {
PrintStream stream = findOutStream();
stream.println("The following stacktrace is for failure analysis.");
e.printStackTrace(stream);
}
loggedExceptions.add(callerKey);
loggedExceptions.add(msg);
}
/////////////////////////////////////////////////////////////////
/**
* Redirect log to the given <code>stream</code>, and switch
* log mode to verbose.
* Prints errors summary to current stream, cancel current stream
* and switches to new stream. Turns on verbose mode for new stream.
*
* @deprecated This method is obsolete.
*/
protected synchronized void logTo(PrintStream stream) {
finalize(); // flush older log stream
out = stream;
verbose = true;
}
/////////////////////////////////////////////////////////////////
/**
* Print all messages from log buffer which were hidden because
* of non-verbose mode,
*/
private synchronized void flushLogBuffer() {
if (!logBuffer.isEmpty()) {
PrintStream stream = findOutStream();
for (int i = 0; i < logBuffer.size(); i++) {
stream.println(logBuffer.elementAt(i));
}
stream.flush();
}
}
/**
* Return <code>out</code> stream if defined or <code>Sytem.err<code> otherwise;
* print a warning message when <code>System.err</code> is used first time.
*/
private synchronized PrintStream findOutStream() {
PrintStream stream = out;
if (stream == null) {
stream = System.err;
if (!noOutWarned) {
noOutWarned = true;
stream.println("#> ");
stream.println("#> WARNING: switching log stream to stderr,");
stream.println("#> because no output stream is assigned");
stream.println("#> ");
};
};
stream.flush();
return stream;
}
/**
* Compose line to print possible prefixing it with timestamp.
*/
private String composeLine(String message) {
if (timestamp) {
long time = System.currentTimeMillis();
long ms = time % 1000;
time /= 1000;
long secs = time % 60;
time /= 60;
long mins = time % 60;
time /= 60;
long hours = time % 24;
return "[" + hours + ":" + mins + ":" + secs + "." + ms + "] " + message;
}
return message;
}
/**
* Print the given <code>message</code> either to <code>out</code>
* stream, or to <code>System.err</code> if <code>out</code>
* stream is not specified.
*/
private synchronized void doPrint(String message) {
PrintStream stream = findOutStream();
stream.println(composeLine(message));
stream.flush();
}
/**
* Print the given error <code>message</code> either to <code>out</code>
* stream, or to <code>System.err</code> if <code>out</code>
* stream is not specified.
*/
private synchronized void printError(String message) {
// Print each line with the ERROR prefix:
BufferedReader br = new BufferedReader(new StringReader(message));
for (String line; ; ) {
try {
line = br.readLine();
if (line == null)
break;
doPrint("# ERROR: " + line);
} catch (IOException e) {
throw new TestBug("Exception in Log.printError(): " + e);
};
}
}
/**
* Keep the given log <code>message</code> into <code>logBuffer</code>.
*/
private synchronized void keepLog(String message) {
logBuffer.addElement(message);
}
/**
* Keep the given error <code>message</code> into <code>errorsBuffer</code>.
*/
private synchronized void keepError(String message) {
errorsBuffer.addElement(message);
}
/**
* Print errors messages summary from errors buffer if any;
* print a warning message first.
*/
private synchronized void printErrorsSummary() {
if (errorsBuffer.size() <= 0)
return;
PrintStream stream = findOutStream();
stream.println();
stream.println();
stream.println("#> ");
stream.println("#> SUMMARY: Following errors occured");
stream.println("#> during test execution:");
stream.println("#> ");
stream.flush();
for (Enumeration e = errorsBuffer.elements(); e.hasMoreElements(); ) {
printError((String) e.nextElement());
}
}
/**
* Print errors summary if mode is verbose, flush and cancel output stream.
*/
protected void finalize() {
if (verbose() && isErrorsSummaryEnabled()) {
printErrorsSummary();
}
if (out != null)
out.flush();
out = null;
}
/**
* Perform finalization at the exit.
*/
public void finalizeAtExit() {
finalize();
}
/**
* This class can be used as a base for each class that use <code>Log</code>
* for print messages and errors.
* <code>Logger</code> provides with ability to print such messages with
* specified prefix to make it possible to distinct messages printed from
* different sources.
*
* @see Log
*/
public static class Logger {
/**
* Default prefix for messages.
*/
public static final String LOG_PREFIX = "";
/**
* Log to print messages to.
*/
protected Log log = null;
/**
* Prefix for messages.
*/
protected String logPrefix = LOG_PREFIX;
/**
* Make <code>Logger</code> object with empty <code>Log</code> and
* default prefix.
* This method may be used only in derived class, that should specify
* the used <code>Log</code> object further and assign it to <code>log</code>.
*
* @see #log
* @see #setLogPrefix
*/
protected Logger() {
}
/**
* Make <code>Logger</code> object for specified <code>Log</code>
* with default prefix.
*
* @see #setLogPrefix
*/
public Logger(Log log) {
this.log = log;
}
/**
* Make <code>Logger</code> object for specified <code>Log</code> with
* given messages prefix.
*/
public Logger(Log log, String prefix) {
this.log = log;
this.logPrefix = prefix;
}
/**
* Return <code>Log</code> object.
*/
public Log getLog() {
return log;
}
/**
* Return output stream of this <code>Log</code> object.
*/
public PrintStream getOutStream() {
return log.getOutStream();
}
/**
* Set prefix for printed messages.
*/
public void setLogPrefix(String prefix) {
logPrefix = prefix;
}
/**
* Make printable message by adding <code>logPrefix<code> to it.
*/
public String makeLogMessage(String message) {
return logPrefix + message;
}
/**
* Print trace message by invoking <code>Log.trace()</code>.
*
* @see Log#trace
*/
public void trace(int level, String message) {
log.trace(level, makeLogMessage(message));
}
/**
* Print message by invoking <code>Log.println()</code>.
*
* @see Log#println
*/
public void println(String message) {
log.println(makeLogMessage(message));
}
/**
* Print message by invoking <code>Log.display()</code>.
*
* @see Log#display
*/
public void display(String message) {
log.display(makeLogMessage(message));
}
/**
* Complain about an error by invoking <code>Log.complain()</code> method.
*
* @see Log#complain
*/
public void complain(String message) {
log.complain(makeLogMessage(message));
}
}
}
|