1
1
package processing .app ;
2
2
3
- import cc .arduino .files .DeleteFilesOnShutdown ;
4
- import org .apache .commons .compress .utils .IOUtils ;
5
-
6
- import static processing .app .I18n ._ ;
7
-
8
- import java .io .File ;
9
- import java .io .FileOutputStream ;
10
- import java .io .IOException ;
11
3
import java .io .OutputStream ;
12
4
import java .io .PrintStream ;
13
5
14
6
class EditorConsoleStream extends OutputStream {
15
- static File tempFolder ;
16
- static File outFile ;
17
- static File errFile ;
18
-
19
- static EditorConsole currentConsole ;
20
7
21
- static OutputStream stderrFile ;
22
- static OutputStream stdoutFile ;
23
- static PrintStream consoleErr ;
24
- static PrintStream consoleOut ;
25
- static public PrintStream systemErr ;
26
- static public PrintStream systemOut ;
8
+ private static EditorConsole currentConsole ;
9
+ private static PrintStream systemErr ;
10
+ private static PrintStream systemOut ;
27
11
28
12
public static void init () {
29
13
if (systemOut == null ) {
30
14
systemOut = System .out ;
31
15
systemErr = System .err ;
32
16
33
- // Create a temporary folder which will have a randomized name. Has to
34
- // be randomized otherwise another instance of Processing (or one of its
35
- // sister IDEs) might collide with the file causing permissions problems.
36
- // The files and folders are not deleted on exit because they may be
37
- // needed for debugging or bug reporting.
38
- tempFolder = BaseNoGui .createTempFolder ("console" );
39
- DeleteFilesOnShutdown .add (tempFolder );
40
- try {
41
- String outFileName = PreferencesData .get ("console.output.file" );
42
- if (outFileName != null ) {
43
- outFile = new File (tempFolder , outFileName );
44
- DeleteFilesOnShutdown .add (outFile );
45
- stdoutFile = new FileOutputStream (outFile );
46
- }
47
-
48
- String errFileName = PreferencesData .get ("console.error.file" );
49
- if (errFileName != null ) {
50
- errFile = new File (tempFolder , errFileName );
51
- DeleteFilesOnShutdown .add (errFile );
52
- stderrFile = new FileOutputStream (errFile );
53
- }
54
- } catch (IOException e ) {
55
- Base .showWarning (_ ("Console Error" ),
56
- _ ("A problem occurred while trying to open the\n files used to store the console output." ),
57
- e );
58
- }
59
- consoleOut = new PrintStream (new EditorConsoleStream (false ));
60
- consoleErr = new PrintStream (new EditorConsoleStream (true ));
61
-
62
17
if (PreferencesData .getBoolean ("console" )) {
63
- try {
64
- System .setOut (consoleOut );
65
- System .setErr (consoleErr );
66
- } catch (Exception e ) {
67
- e .printStackTrace (systemOut );
68
- }
18
+ PrintStream consoleOut = new PrintStream (new EditorConsoleStream (false ));
19
+ PrintStream consoleErr = new PrintStream (new EditorConsoleStream (true ));
20
+
21
+ System .setOut (consoleOut );
22
+ System .setErr (consoleErr );
69
23
}
70
24
}
71
25
}
72
26
73
- /**
74
- * Close the streams so that the temporary files can be deleted.
75
- * <p/>
76
- * File.deleteOnExit() cannot be used because the stdout and stderr files are
77
- * inside a folder, and have to be deleted before the folder itself is
78
- * deleted, which can't be guaranteed when using the deleteOnExit() method.
79
- */
80
- public static void quit () {
81
- // replace original streams to remove references to console's streams
82
- System .setOut (systemOut );
83
- System .setErr (systemErr );
84
-
85
- // close the PrintStream
86
- IOUtils .closeQuietly (consoleOut );
87
- IOUtils .closeQuietly (consoleErr );
88
-
89
- // also have to close the original FileOutputStream
90
- // otherwise it won't be shut down completely
91
- IOUtils .closeQuietly (stdoutFile );
92
- IOUtils .closeQuietly (stderrFile );
93
-
94
- outFile .delete ();
95
- errFile .delete ();
96
- tempFolder .delete ();
97
- }
98
-
99
- final boolean err ; // whether stderr or stdout
100
- PrintStream system ;
101
- OutputStream file ;
27
+ private final boolean isStdErr ; // whether stderr or stdout
28
+ private final PrintStream system ;
102
29
103
- public EditorConsoleStream (boolean _err ) {
104
- err = _err ;
105
- if (err ) {
30
+ private EditorConsoleStream (boolean isStdErr ) {
31
+ this . isStdErr = isStdErr ;
32
+ if (this . isStdErr ) {
106
33
system = systemErr ;
107
- file = stderrFile ;
108
34
} else {
109
35
system = systemOut ;
110
- file = stdoutFile ;
111
36
}
112
37
}
113
38
@@ -118,31 +43,22 @@ public void flush() {
118
43
}
119
44
120
45
public void write (int b ) {
121
- write (new byte [] { (byte ) b });
46
+ write (new byte []{ (byte ) b });
122
47
}
123
48
124
49
public void write (byte b []) { // appears never to be used
125
50
write (b , 0 , b .length );
126
51
}
127
52
128
53
public void write (byte b [], int offset , int length ) {
129
- if (currentConsole != null )
130
- currentConsole .appendText (new String (b , offset , length ), err );
54
+ if (currentConsole != null ) {
55
+ currentConsole .appendText (new String (b , offset , length ), isStdErr );
56
+ }
131
57
132
58
system .write (b , offset , length );
133
-
134
- if (file != null ) {
135
- try {
136
- file .write (b , offset , length );
137
- file .flush ();
138
- } catch (IOException e ) {
139
- e .printStackTrace ();
140
- file = null ;
141
- }
142
- }
143
59
}
144
60
145
- static public void setCurrent (EditorConsole console ) {
61
+ public static void setCurrent (EditorConsole console ) {
146
62
currentConsole = console ;
147
63
}
148
64
0 commit comments