forked from facchinm/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorConsoleStream.java
More file actions
150 lines (126 loc) · 4.04 KB
/
Copy pathEditorConsoleStream.java
File metadata and controls
150 lines (126 loc) · 4.04 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package processing.app;
import static processing.app.I18n._;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
class EditorConsoleStream extends OutputStream {
static File tempFolder;
static File outFile;
static File errFile;
static EditorConsole currentConsole;
static OutputStream stderrFile;
static OutputStream stdoutFile;
static PrintStream consoleErr;
static PrintStream consoleOut;
static public PrintStream systemErr;
static public PrintStream systemOut;
public static void init() {
if (systemOut == null) {
systemOut = System.out;
systemErr = System.err;
// Create a temporary folder which will have a randomized name. Has to
// be randomized otherwise another instance of Processing (or one of its
// sister IDEs) might collide with the file causing permissions problems.
// The files and folders are not deleted on exit because they may be
// needed for debugging or bug reporting.
tempFolder = Base.createTempFolder("console");
tempFolder.deleteOnExit();
try {
String outFileName = Preferences.get("console.output.file");
if (outFileName != null) {
outFile = new File(tempFolder, outFileName);
outFile.deleteOnExit();
stdoutFile = new FileOutputStream(outFile);
}
String errFileName = Preferences.get("console.error.file");
if (errFileName != null) {
errFile = new File(tempFolder, errFileName);
errFile.deleteOnExit();
stderrFile = new FileOutputStream(errFile);
}
} catch (IOException e) {
Base.showWarning(_("Console Error"),
_("A problem occurred while trying to open the\nfiles used to store the console output."),
e);
}
consoleOut = new PrintStream(new EditorConsoleStream(false));
consoleErr = new PrintStream(new EditorConsoleStream(true));
if (Preferences.getBoolean("console")) {
try {
System.setOut(consoleOut);
System.setErr(consoleErr);
} catch (Exception e) {
e.printStackTrace(systemOut);
}
}
}
}
/**
* Close the streams so that the temporary files can be deleted.
* <p/>
* File.deleteOnExit() cannot be used because the stdout and stderr files are
* inside a folder, and have to be deleted before the folder itself is
* deleted, which can't be guaranteed when using the deleteOnExit() method.
*/
public static void quit() {
// replace original streams to remove references to console's streams
System.setOut(systemOut);
System.setErr(systemErr);
// close the PrintStream
consoleOut.close();
consoleErr.close();
// also have to close the original FileOutputStream
// otherwise it won't be shut down completely
try {
stdoutFile.close();
stderrFile.close();
} catch (IOException e) {
e.printStackTrace();
}
outFile.delete();
errFile.delete();
tempFolder.delete();
}
final boolean err; // whether stderr or stdout
PrintStream system;
OutputStream file;
public EditorConsoleStream(boolean _err) {
err = _err;
if (err) {
system = systemErr;
file = stderrFile;
} else {
system = systemOut;
file = stdoutFile;
}
}
public void close() {
}
public void flush() {
}
public void write(int b) {
write(new byte[] { (byte) b });
}
public void write(byte b[]) { // appears never to be used
write(b, 0, b.length);
}
public void write(byte b[], int offset, int length) {
if (currentConsole != null)
currentConsole.appendText(new String(b, offset, length), err);
system.write(b, offset, length);
if (file != null) {
try {
file.write(b, offset, length);
file.flush();
} catch (IOException e) {
e.printStackTrace();
file = null;
}
}
}
static public void setCurrent(EditorConsole console) {
currentConsole = console;
}
}