| 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 | |
| 12 | /** |
| 13 | * Interface to diagnostic event logging mechanism. |
| 14 | * A Logger can be registered at the level of the Saxon Configuration. |
| 15 | * The default implementation for the Java platform writes all messages to System.err |
| 16 | */ |
| 17 | public abstract class Logger { |
| 18 | |
| 19 | public static final int INFO = 0; |
| 20 | public static final int WARNING = 1; |
| 21 | public static final int ERROR = 2; |
| 22 | public static final int DISASTER = 3; |
| 23 | |
| 24 | private boolean unicodeAware = false; |
| 25 | |
| 26 | /** |
| 27 | * Log a message with level {@link Logger#INFO} |
| 28 | * @param message the message to be logged |
| 29 | */ |
| 30 | public void info(String message) { |
| 31 | println(message, INFO); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Log a message with level {@link Logger#WARNING} |
| 36 | * @param message the message to be logged |
| 37 | */ |
| 38 | public void warning(String message) { |
| 39 | println(message, WARNING); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Log a message with level {@link Logger#ERROR} |
| 44 | * @param message the message to be logged |
| 45 | */ |
| 46 | public void error(String message) { |
| 47 | println(message, ERROR); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Log a message with level {@link Logger#DISASTER} |
| 52 | * @param message the message to be logged |
| 53 | */ |
| 54 | public void disaster(String message) { |
| 55 | println(message, DISASTER); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Log a message. To be implemented in a concrete subclass |
| 60 | * @param message The message to be output |
| 61 | * @param severity The severity level. One of {@link Logger#INFO}, {@link Logger#WARNING}, {@link Logger#ERROR}, |
| 62 | * {@link Logger#DISASTER} |
| 63 | */ |
| 64 | |
| 65 | public abstract void println(String message, int severity); |
| 66 | |
| 67 | /** |
| 68 | * Close the logger, indicating that no further messages will be written |
| 69 | * and that underlying streams should be closed, if they were created by the Logger |
| 70 | * itself rather than by the user. |
| 71 | */ |
| 72 | |
| 73 | public void close() {} |
| 74 | |
| 75 | /** |
| 76 | * Ask whether the Logger is capable of handling the full Unicode character repertoire. |
| 77 | * By default this is false, because the default destination of console output is sensitive |
| 78 | * to the configured locale and is often limited to legacy character sets. |
| 79 | * @return true if the Logger can handle the full Unicode character repertoire. With the |
| 80 | * default value (false), the {@link StandardErrorListener} will output special characters |
| 81 | * using the notation C[xHHHH] where C is the character, and HHHH is its hexadecimal codepoint. |
| 82 | */ |
| 83 | |
| 84 | public boolean isUnicodeAware() { |
| 85 | return unicodeAware; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Say whether the Logger is capable of handling the full Unicode character repertoire. |
| 90 | * By default this is false, because the default destination of console output is sensitive |
| 91 | * to the configured locale and is often limited to legacy character sets. |
| 92 | * |
| 93 | * @param aware true if the Logger can handle the full Unicode character repertoire. With the |
| 94 | * default value (false), the {@link StandardErrorListener} will output special characters |
| 95 | * using the notation C[xHHHH] where C is the character, and HHHH is its hexadecimal codepoint. |
| 96 | */ |
| 97 | |
| 98 | public void setUnicodeAware(boolean aware) { |
| 99 | unicodeAware = aware; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get a JAXP StreamResult object allowing serialized XML to be written to this Logger |
| 104 | * @return a StreamResult that serializes XML to this Logger |
| 105 | */ |
| 106 | |
| 107 | public abstract StreamResult asStreamResult(); |
| 108 | } |
| 109 |