| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.maven.shared.scriptinterpreter; |
| 20 |
|
| 21 |
import java.io.File; |
| 22 |
import java.io.FileOutputStream; |
| 23 |
import java.io.IOException; |
| 24 |
import java.io.OutputStream; |
| 25 |
import java.io.PrintStream; |
| 26 |
|
| 27 |
/** |
| 28 |
* <p>FileLogger class.</p> |
| 29 |
* |
| 30 |
*/ |
| 31 |
public class FileLogger implements ExecutionLogger, AutoCloseable { |
| 32 |
|
| 33 |
/** |
| 34 |
* The path to the log file. |
| 35 |
*/ |
| 36 |
private File file; |
| 37 |
|
| 38 |
/** |
| 39 |
* The underlying file stream this logger writes to. |
| 40 |
*/ |
| 41 |
private PrintStream stream; |
| 42 |
|
| 43 |
/** |
| 44 |
* Creates a new logger that writes to the specified file. |
| 45 |
* |
| 46 |
* @param outputFile The path to the output file, if null all message will be discarded. |
| 47 |
* @throws java.io.IOException If the output file could not be created. |
| 48 |
*/ |
| 49 |
public FileLogger(File outputFile) throws IOException { |
| 50 |
this(outputFile, null); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Creates a new logger that writes to the specified file and optionally mirrors messages. |
| 55 |
* |
| 56 |
* @param outputFile The path to the output file, if null all message will be discarded. |
| 57 |
* @param mirrorHandler The class which handle mirrored message, can be <code>null</code>. |
| 58 |
* @throws java.io.IOException If the output file could not be created. |
| 59 |
*/ |
| 60 |
public FileLogger(File outputFile, FileLoggerMirrorHandler mirrorHandler) throws IOException { |
| 61 |
this.file = outputFile; |
| 62 |
|
| 63 |
OutputStream outputStream; |
| 64 |
|
| 65 |
if (outputFile != null) { |
| 66 |
outputFile.getParentFile().mkdirs(); |
| 67 |
outputStream = new FileOutputStream(outputFile); |
| 68 |
} else { |
| 69 |
outputStream = new NullOutputStream(); |
| 70 |
} |
| 71 |
|
| 72 |
if (mirrorHandler != null) { |
| 73 |
stream = new PrintStream(new MirrorStreamWrapper(outputStream, mirrorHandler)); |
| 74 |
} else { |
| 75 |
stream = new PrintStream(outputStream); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets the path to the output file. |
| 81 |
* |
| 82 |
* @return The path to the output file, never <code>null</code>. |
| 83 |
*/ |
| 84 |
public File getOutputFile() { |
| 85 |
return file; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Gets the underlying stream used to write message to the log file. |
| 90 |
* |
| 91 |
* @return The underlying stream used to write message to the log file, never <code>null</code>. |
| 92 |
*/ |
| 93 |
@Override |
| 94 |
public PrintStream getPrintStream() { |
| 95 |
return stream; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Writes the specified line to the log file |
| 100 |
* and invoke {@link FileLoggerMirrorHandler#consumeOutput(String)} if is given. |
| 101 |
* |
| 102 |
* @param line The message to log. |
| 103 |
*/ |
| 104 |
@Override |
| 105 |
public void consumeLine(String line) { |
| 106 |
stream.println(line); |
| 107 |
stream.flush(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Closes the underlying file stream. |
| 112 |
*/ |
| 113 |
public void close() { |
| 114 |
if (stream != null) { |
| 115 |
stream.flush(); |
| 116 |
stream.close(); |
| 117 |
stream = null; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
private static class MirrorStreamWrapper extends OutputStream { |
| 122 |
private final OutputStream out; |
| 123 |
private final FileLoggerMirrorHandler mirrorHandler; |
| 124 |
|
| 125 |
private StringBuilder lineBuffer; |
| 126 |
|
| 127 |
MirrorStreamWrapper(OutputStream outputStream, FileLoggerMirrorHandler mirrorHandler) { |
| 128 |
this.out = outputStream; |
| 129 |
this.mirrorHandler = mirrorHandler; |
| 130 |
this.lineBuffer = new StringBuilder(); |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void write(int b) throws IOException { |
| 135 |
out.write(b); |
| 136 |
lineBuffer.append((char) (b)); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void write(byte[] b, int off, int len) throws IOException { |
| 141 |
out.write(b, off, len); |
| 142 |
lineBuffer.append(new String(b, off, len)); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public void flush() throws IOException { |
| 147 |
out.flush(); |
| 148 |
|
| 149 |
int len = lineBuffer.length(); |
| 150 |
if (len == 0) { |
| 151 |
// nothing to log |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
// remove line end for log |
| 156 |
while (len > 0 && (lineBuffer.charAt(len - 1) == '\n' || lineBuffer.charAt(len - 1) == '\r')) { |
| 157 |
len--; |
| 158 |
} |
| 159 |
lineBuffer.setLength(len); |
| 160 |
|
| 161 |
mirrorHandler.consumeOutput(lineBuffer.toString()); |
| 162 |
|
| 163 |
// clear buffer |
| 164 |
lineBuffer = new StringBuilder(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
private static class NullOutputStream extends OutputStream { |
| 169 |
@Override |
| 170 |
public void write(int b) { |
| 171 |
// do nothing |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void write(byte[] b, int off, int len) { |
| 176 |
// do nothing |
| 177 |
} |
| 178 |
} |
| 179 |
} |