source: java/installer2/src/net/oni2/aeinstaller/backend/LogPrintStream.java@ 1043

Last change on this file since 1043 was 1032, checked in by alloc, 9 years ago

v.25: Added some debug information to mod installation, auto flush log, print detected platform

File size: 1.5 KB
Line 
1package net.oni2.aeinstaller.backend;
2
3import java.io.ByteArrayOutputStream;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.io.OutputStream;
8import java.io.PrintStream;
9
10/**
11 * @author Christian Illy
12 */
13public class LogPrintStream extends OutputStream {
14 private static LogPrintStream instance = new LogPrintStream();
15
16 private ByteArrayOutputStream baos = new ByteArrayOutputStream();
17 private FileWriter fw = null;
18 private PrintStream stdOut = null;
19
20 private boolean logStdOut = true;
21
22 private LogPrintStream() {
23 stdOut = System.out;
24
25 PrintStream ps = new PrintStream(this, true);
26 System.setOut(ps);
27 System.setErr(ps);
28 }
29
30 /**
31 * @return Logger instance
32 */
33 public static LogPrintStream getInstance() {
34 return instance;
35 }
36
37 /**
38 * Set the file to log to
39 *
40 * @param logFile
41 * Log file
42 * @throws IOException
43 * Should not happen?!
44 */
45 public void setFile(File logFile) throws IOException {
46 if (fw != null)
47 fw.close();
48 fw = new FileWriter(logFile);
49 }
50
51 @Override
52 public void write(int b) throws IOException {
53 baos.write(b);
54 if (logStdOut)
55 stdOut.write(b);
56 if (fw != null)
57 fw.write(b);
58 }
59
60 /**
61 * @return Get the text that has been written to log up to now
62 */
63 public String getLog() {
64 return baos.toString();
65 }
66
67 @Override
68 public void flush() throws IOException {
69 stdOut.flush();
70 baos.flush();
71 if (fw != null)
72 fw.flush();
73 }
74
75 @Override
76 public void close() throws IOException {
77 if (fw != null)
78 fw.close();
79 super.close();
80 }
81}
Note: See TracBrowser for help on using the repository browser.