| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.lib; |
| 9 | |
| 10 | import javax.xml.transform.stream.StreamResult; |
| 11 | import java.io.File; |
| 12 | import java.io.FileNotFoundException; |
| 13 | import java.io.PrintStream; |
| 14 | |
| 15 | /** |
| 16 | * The default Logger used by Saxon on the Java platform. All messages are written by default |
| 17 | * to System.err. The logger can be configured by setting a different output destination, and |
| 18 | * by setting a minimum threshold for the severity of messages to be output. |
| 19 | */ |
| 20 | public class StandardLogger extends Logger { |
| 21 | |
| 22 | private PrintStream out = System.err; |
| 23 | private int threshold = Logger.INFO; |
| 24 | private boolean mustClose = false; |
| 25 | |
| 26 | /** |
| 27 | * Create a Logger that wraps the System.err output stream |
| 28 | */ |
| 29 | |
| 30 | public StandardLogger() { |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Create a Logger that wraps the specified stream. Closing the Logger will |
| 35 | * not close the underlying stream; this remains the caller's responsibility |
| 36 | * |
| 37 | * @param stream the stream to which the Logger's output should be written |
| 38 | */ |
| 39 | |
| 40 | public StandardLogger(PrintStream stream) { |
| 41 | setPrintStream(stream); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Create a Logger that writes to a specified file |
| 46 | * |
| 47 | * @param fileName the file to which output should be written. When the logger is closed, |
| 48 | * output will be flushed to the file. |
| 49 | * @throws FileNotFoundException if the file is not accessible |
| 50 | */ |
| 51 | |
| 52 | public StandardLogger(File fileName) throws FileNotFoundException { |
| 53 | setPrintStream(new PrintStream(fileName)); |
| 54 | mustClose = true; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Set the output destination for messages |
| 59 | * |
| 60 | * @param stream the stream to which messages will be written. Defaults to System.err. The caller |
| 61 | * is responsible for closing the stream after use (it will not be closed by the |
| 62 | * close() method on the Logger) |
| 63 | */ |
| 64 | |
| 65 | public void setPrintStream(PrintStream stream) { |
| 66 | out = stream; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the output destination used for messages |
| 71 | * |
| 72 | * @return the stream to which messages are written |
| 73 | */ |
| 74 | |
| 75 | public PrintStream getPrintStream() { |
| 76 | return out; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Set the minimum threshold for the severity of messages to be output. Defaults to |
| 81 | * {@link Logger#INFO}. Messages whose severity is below this threshold will be ignored |
| 82 | * |
| 83 | * @param threshold the minimum severity of messages to be output. |
| 84 | */ |
| 85 | |
| 86 | public void setThreshold(int threshold) { |
| 87 | this.threshold = threshold; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the minimum threshold for the severity of messages to be output. Defaults to |
| 92 | * {@link Logger#INFO}. Messages whose severity is below this threshold will be ignored |
| 93 | * |
| 94 | * @return the minimum severity of messages to be output. |
| 95 | */ |
| 96 | |
| 97 | public int getThreshold() { |
| 98 | return threshold; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get a JAXP Result object allowing serialized XML to be written to this Logger |
| 103 | * |
| 104 | * @return a Result that serializes XML to this Logger |
| 105 | */ |
| 106 | @Override |
| 107 | public StreamResult asStreamResult() { |
| 108 | return new StreamResult(out); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Output a message with a specified severity. |
| 113 | * |
| 114 | * @param message The message to be output |
| 115 | * @param severity The severity: one of {@link Logger#INFO}, {@link Logger#WARNING}, {@link Logger#ERROR}, |
| 116 | * {@link Logger#DISASTER} |
| 117 | */ |
| 118 | |
| 119 | public void println(String message, int severity) { |
| 120 | if (severity >= threshold) { |
| 121 | out.println(message); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Close the logger, indicating that no further messages will be written |
| 127 | */ |
| 128 | @Override |
| 129 | public void close() { |
| 130 | if (mustClose) { |
| 131 | out.close(); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 |