| 1 | /*- | |
| 2 | * #%L | |
| 3 | * io.earcam.utilitarian.log.slf4j | |
| 4 | * %% | |
| 5 | * Copyright (C) 2017 earcam | |
| 6 | * %% | |
| 7 | * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT) | |
| 8 | * | |
| 9 | * You <b>must</b> choose to accept, in full - any individual or combination of | |
| 10 | * the following licenses: | |
| 11 | * <ul> | |
| 12 | * <li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li> | |
| 13 | * <li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li> | |
| 14 | * <li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li> | |
| 15 | * <li><a href="https://opensource.org/licenses/MIT">MIT</a></li> | |
| 16 | * </ul> | |
| 17 | * #L% | |
| 18 | */ | |
| 19 | package io.earcam.utilitarian.log.slf4j; | |
| 20 | ||
| 21 | import static io.earcam.utilitarian.log.slf4j.Constants.LOG_FILE_KEY; | |
| 22 | import static java.nio.charset.StandardCharsets.UTF_8; | |
| 23 | ||
| 24 | import java.io.ByteArrayOutputStream; | |
| 25 | import java.io.OutputStream; | |
| 26 | import java.io.PrintStream; | |
| 27 | import java.io.UnsupportedEncodingException; | |
| 28 | import java.nio.charset.Charset; | |
| 29 | ||
| 30 | import io.earcam.unexceptional.Exceptional; | |
| 31 | ||
| 32 | final class LoggingCapture { | |
| 33 | ||
| 34 | private LoggingCapture() | |
| 35 | {} | |
| 36 | ||
| 37 | ||
| 38 | public static String capture(LogCapturable runnable) | |
| 39 | { | |
| 40 | ByteArrayOutputStream capture = new ByteArrayOutputStream(); | |
| 41 |
1
1. capture : removed call to io/earcam/utilitarian/log/slf4j/LoggingCapture::capture → KILLED |
capture(runnable, capture); |
| 42 |
1
1. capture : mutated return of Object value for io/earcam/utilitarian/log/slf4j/LoggingCapture::capture to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new String(capture.toByteArray(), charset()); |
| 43 | } | |
| 44 | ||
| 45 | ||
| 46 | private static void capture(LogCapturable runnable, ByteArrayOutputStream capture) | |
| 47 | { | |
| 48 | PrintStream original = getPrintStream(); | |
| 49 | try { // NOSONAR | |
| 50 |
1
1. capture : removed call to io/earcam/utilitarian/log/slf4j/LoggingCapture::setPrintStream → KILLED |
setPrintStream(Exceptional.apply(LoggingCapture::newPrintStream, capture)); |
| 51 |
1
1. capture : removed call to io/earcam/utilitarian/log/slf4j/LoggingCapture::execute → KILLED |
execute(runnable); |
| 52 | } finally { | |
| 53 |
1
1. capture : removed call to io/earcam/utilitarian/log/slf4j/LoggingCapture::setPrintStream → SURVIVED |
setPrintStream(original); |
| 54 | } | |
| 55 | } | |
| 56 | ||
| 57 | ||
| 58 | private static PrintStream newPrintStream(OutputStream capture) throws UnsupportedEncodingException | |
| 59 | { | |
| 60 |
1
1. newPrintStream : mutated return of Object value for io/earcam/utilitarian/log/slf4j/LoggingCapture::newPrintStream to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return new PrintStream(capture, true, charset().toString()); |
| 61 | } | |
| 62 | ||
| 63 | ||
| 64 | private static void execute(LogCapturable runnable) | |
| 65 | { | |
| 66 |
1
1. execute : removed call to io/earcam/unexceptional/Exceptional::run → KILLED |
Exceptional.run(runnable::run); |
| 67 | } | |
| 68 | ||
| 69 | ||
| 70 | private static PrintStream getPrintStream() | |
| 71 | { | |
| 72 |
2
1. getPrintStream : negated conditional → SURVIVED 2. getPrintStream : mutated return of Object value for io/earcam/utilitarian/log/slf4j/LoggingCapture::getPrintStream to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return useStdOut() ? System.out : System.err; // NOSONAR no! |
| 73 | } | |
| 74 | ||
| 75 | ||
| 76 | static boolean useStdOut() | |
| 77 | { | |
| 78 | System.getProperty(LOG_FILE_KEY); | |
| 79 |
1
1. useStdOut : replaced return of integer sized value with (x == 0 ? 1 : 0) → KILLED |
return "System.out".equals(System.getProperty(LOG_FILE_KEY, "System.err")); |
| 80 | } | |
| 81 | ||
| 82 | ||
| 83 | private static void setPrintStream(PrintStream printStream) | |
| 84 | { | |
| 85 |
1
1. setPrintStream : negated conditional → KILLED |
if(useStdOut()) { |
| 86 |
1
1. setPrintStream : removed call to java/lang/System::setOut → KILLED |
System.setOut(printStream); |
| 87 | } else { | |
| 88 |
1
1. setPrintStream : removed call to java/lang/System::setErr → KILLED |
System.setErr(printStream); |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | ||
| 93 | private static Charset charset() | |
| 94 | { | |
| 95 |
1
1. charset : mutated return of Object value for io/earcam/utilitarian/log/slf4j/LoggingCapture::charset to ( if (x != null) null else throw new RuntimeException ) → KILLED |
return Charset.forName(System.getProperty("file.encoding", UTF_8.name())); |
| 96 | } | |
| 97 | } | |
Mutations | ||
| 41 |
1.1 |
|
| 42 |
1.1 |
|
| 50 |
1.1 |
|
| 51 |
1.1 |
|
| 53 |
1.1 |
|
| 60 |
1.1 |
|
| 66 |
1.1 |
|
| 72 |
1.1 2.2 |
|
| 79 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |
|
| 88 |
1.1 |
|
| 95 |
1.1 |