1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| public static void Main(String args){
RedirectStream redirect = RedirectStream(new File(path));
redirect.CallRedirect();
Program.Call(args);
redirect.CallClose();
}
public class Program{
public static void Call(String[] args){
//Programme contenu dans un jar dans le projet
CommandLine c = CommandLine(args);
}
public class RedirectStream {
private PrintStream oldout;
private PrintSteam olderr;
private File f;
public RedirectStream(File f){
this.oldout=System.out;
this.olderr=System.err;
this.f=f;
}
public void CallRedirect(){
LogManager logManager = LogManager.getLogManager();
logManager.reset();
// log file max size 10K, 3 rolling files, append-on-open
Handler fileHandler = new FileHandler(this.f, 10000, 3, true);
fileHandler.setFormatter(new SimpleFormatter());
Logger.getLogger("").addHandler(fileHandler);
PrintStream stdout = System.out;
PrintStream stderr = System.err;
Logger logger;
LoggingOutputStream los;
logger = Logger.getLogger("stdout");
los = new LoggingOutputStream(logger, StdOutErrLevel.STDOUT);
System.setOut(new PrintStream(los, true));
logger = Logger.getLogger("stderr");
los= new LoggingOutputStream(logger, StdOutErrLevel.STDERR);
System.setErr(new PrintStream(los, true));
}
public void CloseRedirect(){
System.out.flush();
System.err.flush();
System.setOut(this.oldout);
System.setErr(this.olderr);
}
}
}
public class StdOutErrLevel extends Level {
private StdOutErrLevel(String name, int value) {
super(name, value);
}
public static Level STDOUT =
new StdOutErrLevel("STDOUT", Level.INFO.intValue()+53);
public static Level STDERR =
new StdOutErrLevel("STDERR", Level.INFO.intValue()+54);
protected Object readResolve()
throws ObjectStreamException {
if (this.intValue() == STDOUT.intValue())
return STDOUT;
if (this.intValue() == STDERR.intValue())
return STDERR;
throw new InvalidObjectException("Unknown instance :" + this);
}
}
class LoggingOutputStream extends ByteArrayOutputStream {
private String lineSeparator;
private Logger logger;
private Level level;
public LoggingOutputStream(Logger logger, Level level) {
super();
this.logger = logger;
this.level = level;
lineSeparator = System.getProperty("line.separator");
}
public void flush() throws IOException {
String record;
synchronized(this) {
super.flush();
record = this.toString();
super.reset();
if (record.length() == 0 || record.equals(lineSeparator)) {
// avoid empty records
return;
}
logger.logp(level, "", "", record);
}
}
} |
Partager