2014-06-12 19:28:56 +00:00
|
|
|
/*
|
2016-06-23 03:49:47 +00:00
|
|
|
* Minecraft Forge
|
2018-07-01 21:17:28 +00:00
|
|
|
* Copyright (c) 2016-2018.
|
2016-06-23 03:49:47 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2014-06-12 19:28:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-23 05:01:24 +00:00
|
|
|
package net.minecraftforge.fml.common;
|
2014-06-12 19:28:56 +00:00
|
|
|
|
|
|
|
import java.io.PrintStream;
|
|
|
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PrintStream which redirects it's output to a given logger.
|
2015-11-22 20:31:06 +00:00
|
|
|
*
|
2014-06-12 19:28:56 +00:00
|
|
|
* @author Arkan
|
|
|
|
*/
|
|
|
|
public class TracingPrintStream extends PrintStream {
|
2015-11-22 20:31:06 +00:00
|
|
|
|
2014-06-12 19:28:56 +00:00
|
|
|
private Logger logger;
|
2015-04-10 13:17:27 +00:00
|
|
|
private int BASE_DEPTH = 3;
|
2015-11-22 20:31:06 +00:00
|
|
|
|
2014-06-12 19:28:56 +00:00
|
|
|
public TracingPrintStream(Logger logger, PrintStream original) {
|
|
|
|
super(original);
|
|
|
|
this.logger = logger;
|
|
|
|
}
|
2015-11-22 20:31:06 +00:00
|
|
|
|
2014-06-12 19:28:56 +00:00
|
|
|
@Override
|
|
|
|
public void println(Object o) {
|
2017-06-23 05:33:11 +00:00
|
|
|
logger.info("{}{}", getPrefix(), o);
|
2014-06-12 19:28:56 +00:00
|
|
|
}
|
2015-11-22 20:31:06 +00:00
|
|
|
|
2014-06-12 19:28:56 +00:00
|
|
|
@Override
|
|
|
|
public void println(String s) {
|
2017-06-23 05:33:11 +00:00
|
|
|
logger.info("{}{}", getPrefix(), s);
|
2014-06-12 19:28:56 +00:00
|
|
|
}
|
2015-11-22 20:31:06 +00:00
|
|
|
|
2014-06-12 19:28:56 +00:00
|
|
|
private String getPrefix() {
|
|
|
|
StackTraceElement[] elems = Thread.currentThread().getStackTrace();
|
2015-04-10 13:17:27 +00:00
|
|
|
StackTraceElement elem = elems[BASE_DEPTH]; // The caller is always at BASE_DEPTH, including this call.
|
|
|
|
if (elem.getClassName().startsWith("kotlin.io.")) {
|
|
|
|
elem = elems[BASE_DEPTH + 2]; // Kotlins IoPackage masks origins 2 deeper in the stack.
|
2015-11-22 20:31:06 +00:00
|
|
|
} else if (elem.getClassName().startsWith("java.lang.Throwable")) {
|
2015-11-25 21:28:06 +00:00
|
|
|
elem = elems[BASE_DEPTH + 4];
|
2015-04-10 13:17:27 +00:00
|
|
|
}
|
2014-06-12 19:28:56 +00:00
|
|
|
return "[" + elem.getClassName() + ":" + elem.getMethodName() + ":" + elem.getLineNumber() + "]: ";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|