Project

Profile

Help

Revision 63bd5956

Added by Michael Kay 7 months ago

Fix bug #4761 (Logger)

View differences:

latest10/hej/net/sf/saxon/Configuration.java
927 927
    }
928 928

  
929 929
    /**
930
     * Set the standard error output to be used in all cases where no more specific destination
931
     * is defined. This defaults to System.err.
932
     *
933
     * @param out the stream to be used for error output where no more specific destination
934
     *            has been supplied. The caller is responsible for closing this stream after use
935
     *            (if necessary).
936
     * @since 9.3
937
     */
938

  
939
    public void setStandardErrorOutput(PrintStream out) {
940
        if (!(traceOutput instanceof StandardLogger)) {
941
            traceOutput = new StandardLogger();
942
        }
943
        ((StandardLogger) traceOutput).setPrintStream(out);
944
    }
945

  
946

  
947
    /**
948 930
     * Register a new logger to be used in the Saxon event logging mechanism
949 931
     *
950 932
     * @param logger the Logger to be used as default. The caller is responsible for
......
957 939
        traceOutput = logger;
958 940
    }
959 941

  
942
    /**
943
     * Set the standard error output to be used in all cases where no more specific destination
944
     * is defined. This defaults to System.err.
945
     *
946
     * <p>The method has no effect unless the {@link Configuration#getLogger()} object
947
     * is an instance of {@link StandardLogger}. In that case it calls
948
     * {@link StandardLogger#setPrintStream(PrintStream)}</p>
949
     *
950
     * @param out the stream to be used for error output where no more specific destination
951
     *            has been supplied. The caller is responsible for closing this stream after use
952
     *            (if necessary).
953
     * @since 9.3
954
     */
955

  
956
    public void setStandardErrorOutput(PrintStream out) {
957
        if (traceOutput instanceof StandardLogger) {
958
            ((StandardLogger) traceOutput).setPrintStream(out);
959
        }
960
    }
960 961

  
961 962
    /**
962 963
     * Get the standard error output to be used in all cases where no more specific destination
963 964
     * is defined. This defaults to System.err.
964 965
     *
965
     * @return the stream to be used for error output where no more specific destination
966
     * has been supplied
966
     * @return If the {@link Configuration#getLogger()} object is an instance of {@link StandardLogger},
967
     * then the value of  {@link StandardLogger#getPrintStream()}; otherwise {@code System.err}.
967 968
     * @since 9.3
968 969
     */
969 970

  
......
971 972
        if (traceOutput instanceof StandardLogger) {
972 973
            return ((StandardLogger) traceOutput).getPrintStream();
973 974
        } else {
974
            return null;
975
            return System.err;
975 976
        }
976 977
    }
977 978

  
978

  
979 979
    /**
980 980
     * Set the XML version to be used by default for validating characters and names.
981 981
     * Note that source documents specifying xml version="1.0" or "1.1" are accepted
latest10/hej/net/sf/saxon/dotnet/DotNetPlatform.java
448 448
                    RegistryKey regKey = base.OpenSubKey(path + edition + "-N\\Settings", false);
449 449
                    if (regKey != null) {
450 450
                        if (config.isTiming()) {
451
                            config.getStandardErrorOutput().println("Found registry key at " + regKey.toString());
451
                            config.getLogger().info("Found registry key at " + regKey.toString());
452 452
                        }
453 453
                        String installPath = (String) regKey.GetValue("InstallPath");
454 454
                        if (config.isTiming()) {
455
                            config.getStandardErrorOutput().println("Software installation path: " + installPath);
455
                            config.getLogger().info("Software installation path: " + installPath);
456 456
                        }
457 457
                        return installPath;
458 458
                    }
latest10/hej/net/sf/saxon/event/Builder.java
248 248
            if (sysId == null) {
249 249
                sysId = "(unknown systemId)";
250 250
            }
251
            getConfiguration().getStandardErrorOutput().println(
251
            getConfiguration().getLogger().info(
252 252
                    "Building tree for " + sysId + " using " + getClass());
253 253
            startTime = System.nanoTime();
254 254
        }
......
259 259
    public void close() throws XPathException {
260 260
        if (timing && open) {
261 261
            long endTime = System.nanoTime();
262
            getConfiguration().getStandardErrorOutput().println(
262
            getConfiguration().getLogger().info(
263 263
                    "Tree built in " + CommandLineOptions.showExecutionTimeNano(endTime - startTime));
264 264
            if (currentRoot instanceof TinyDocumentImpl) {
265 265
                ((TinyDocumentImpl) currentRoot).showSize();
latest10/hej/net/sf/saxon/expr/instruct/ResultDocument.java
561 561
                    dest = result.getClass().getName();
562 562
                }
563 563
            }
564
            config.getStandardErrorOutput().println("Writing to " + dest);
564
            config.getLogger().info("Writing to " + dest);
565 565
        }
566 566
    }
567 567

  
latest10/hej/net/sf/saxon/lib/Logger.java
7 7

  
8 8
package net.sf.saxon.lib;
9 9

  
10
import org.jetbrains.annotations.NotNull;
11

  
10 12
import javax.xml.transform.stream.StreamResult;
13
import java.io.Writer;
11 14

  
12 15
/**
13 16
 * Interface to diagnostic event logging mechanism.
......
100 103
    }
101 104

  
102 105
    /**
103
     * Get a JAXP StreamResult object allowing serialized XML to be written to this Logger
106
     * Get a {@link Writer} whose effect is to send each line of written output as
107
     * a separate INFO message to this Logger
108
     * @return a suitable {@code Writer}
109
     */
110

  
111
    public Writer asWriter() {
112
        return new Writer() {
113
            StringBuilder builder = new StringBuilder();
114

  
115
            @Override
116
            public void write(@NotNull char[] cbuf, int off, int len) {
117
                for (int i = 0; i < len; i++) {
118
                    char ch = cbuf[off + i];
119
                    if (ch == '\n') {
120
                        println(builder.toString(), INFO);
121
                        builder.setLength(0);
122
                    } else {
123
                        builder.append(ch);
124
                    }
125
                }
126
            }
127

  
128
            @Override
129
            public void flush() {
130
                if (builder.length() > 0) {
131
                    println(builder.toString(), INFO);
132
                    builder.setLength(0);
133
                }
134
            }
135

  
136
            @Override
137
            public void close() {
138
                flush();
139
            }
140
        };
141
    }
142

  
143
    /**
144
     * Get a JAXP {@link StreamResult} object allowing serialized XML to be written to the
145
     * output destination of this Logger. The default implementation returns a
146
     * {@code StreamResult} wrapping a custom {@link Writer} that writes the supplied
147
     * text line by line using the {@link #println(String, int)} method.
148
     *
104 149
     * @return a StreamResult that serializes XML to this Logger
105 150
     */
106 151

  
107
    public abstract StreamResult asStreamResult();
152
    public StreamResult asStreamResult() {
153
        return new StreamResult(asWriter());
154
    }
108 155
}
109 156

  
latest10/hej/net/sf/saxon/lib/StandardEntityResolver.java
648 648

  
649 649
        // If this is a W3C URI, Saxon ought really to have a copy...
650 650
        if (systemId.startsWith("http://www.w3.org/") && config.isTiming()) {
651
            config.getStandardErrorOutput().println("Saxon does not have a local copy of PUBLIC " + publicId + " SYSTEM " + systemId);
651
            config.getLogger().warning("Saxon does not have a local copy of PUBLIC " + publicId + " SYSTEM " + systemId);
652 652
        }
653 653

  
654 654
        try {
latest10/hej/net/sf/saxon/lib/SystemLogger.java
1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2018-2020 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 java.util.logging.Level;
11

  
12
/**
13
 * An implementation of Saxon's {@code Logger} interface that writes all messages through to
14
 * a supplied {@code java.util.logging.Logger}.
15
 */
16
public class SystemLogger extends Logger {
17

  
18
    private java.util.logging.Logger log;
19

  
20
    /**
21
     * Create a {@code Logger} that wraps a Java {@code java.util.logging.Logger}
22
     */
23

  
24
    public SystemLogger(java.util.logging.Logger log) {
25
        this.log = log;
26
    }
27

  
28
    private Level getLevel(int severity) {
29
        switch (severity) {
30
            case INFO:
31
                return Level.INFO;
32
            case WARNING:
33
                return Level.WARNING;
34
            case ERROR:
35
            case DISASTER:
36
            default:
37
                return Level.SEVERE;
38
        }
39
    }
40

  
41
    /**
42
     * Log a message.
43
     *
44
     * @param message  The message to be output
45
     * @param severity The severity level. One of {@link Logger#INFO}, {@link Logger#WARNING}, {@link Logger#ERROR},
46
     *                 {@link Logger#DISASTER}
47
     */
48
    @Override
49
    public void println(String message, int severity) {
50
        log.log(getLevel(severity), message);
51
    }
52

  
53
}
54

  
latest10/hej/net/sf/saxon/s9api/XQueryEvaluator.java
400 400
            if (systemId == null) {
401 401
                systemId = "";
402 402
            }
403
            config.getStandardErrorOutput().println("Processing streamed input " + systemId);
403
            config.getLogger().info("Processing streamed input " + systemId);
404 404
        }
405 405
        try {
406 406
            SerializationProperties params = expression.getExecutable().getPrimarySerializationProperties();
latest10/hej/net/sf/saxon/serialize/MessageEmitter.java
34 34
        super.setPipelineConfiguration(pipelineConfiguration);
35 35
        if (writer == null && outputStream == null) {
36 36
            try {
37
                setOutputStream(getConfiguration().getStandardErrorOutput());
37
                setWriter(getConfiguration().getLogger().asWriter());
38 38
            } catch (XPathException e) {
39 39
                throw new AssertionError(e);
40 40
            }
latest10/hej/net/sf/saxon/style/PrincipalStylesheetModule.java
1405 1405
            if (compilation.getCompilerInfo().isGenerateByteCode() &&
1406 1406
                    !config.isDeferredByteCode(HostLanguage.XSLT)) {
1407 1407
                if (Compilation.TIMING) {
1408
                    config.getStandardErrorOutput().println("Generating byte code...");
1408
                    config.getLogger().info("Generating byte code...");
1409 1409
                }
1410 1410

  
1411 1411
                Optimizer opt = config.obtainOptimizer();
latest10/hej/net/sf/saxon/trans/XmlCatalogResolver.java
32 32
                @Override
33 33
                public void message(int level, String message) {
34 34
                    if (level <= getDebug()) {
35
                        config.getStandardErrorOutput().println(message);
35
                        config.getLogger().info(message);
36 36
                    }
37 37
                }
38 38

  
39 39
                @Override
40 40
                public void message(int level, String message, String spec) {
41 41
                    if (level <= getDebug()) {
42
                        config.getStandardErrorOutput().println(message + ": " + spec);
42
                        config.getLogger().info(message + ": " + spec);
43 43
                    }
44 44
                }
45 45

  
46 46
                @Override
47 47
                public void message(int level, String message, String spec1, String spec2) {
48 48
                    if (level <= getDebug()) {
49
                        config.getStandardErrorOutput().println(message + ": " + spec1);
50
                        config.getStandardErrorOutput().println("\t" + spec2);
49
                        config.getLogger().info(message + ": " + spec1);
50
                        config.getLogger().info("\t" + spec2);
51 51
                    }
52 52
                }
53 53
            };

Also available in: Unified diff