-
Notifications
You must be signed in to change notification settings - Fork 37
/
io.d
263 lines (222 loc) · 5.79 KB
/
io.d
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* IO related functions
*/
module unit_threaded.io;
import std.concurrency;
import std.stdio;
import std.conv;
/**
* Write if debug output was enabled. Not thread-safe in the sense that it
* will get printed out immediately and may overlap with other output.
* This is why the test runner forces single-threaded mode when debug mode
* is selected.
*/
void writelnUt(T...)(T args) {
import std.stdio;
if (_debugOutput)
writeln(" ", args);
}
private shared(bool) _debugOutput = false; ///print debug msgs?
private shared(bool) _forceEscCodes = false; ///use ANSI escape codes anyway?
package void enableDebugOutput() nothrow {
synchronized {
_debugOutput = true;
}
}
package bool isDebugOutputEnabled() nothrow {
synchronized {
return _debugOutput;
}
}
package void forceEscCodes() nothrow {
synchronized {
_forceEscCodes = true;
}
}
package void utWrite(T...)(T args) {
WriterThread.get().write(args);
}
package void utWriteln(T...)(T args) {
WriterThread.get().writeln(args);
}
package void utWritelnGreen(T...)(T args) {
WriterThread.get().writelnGreen(args);
}
package void utWritelnRed(T...)(T args) {
WriterThread.get().writelnRed(args);
}
package void utWriteRed(T...)(T args) {
WriterThread.get().writeRed(args);
}
package void utWriteYellow(T...)(T args) {
WriterThread.get().writeYellow(args);
}
/**
* Thread to output to stdout
*/
class WriterThread {
/**
* Returns a reference to the only instance of this class.
*/
static WriterThread get() {
if (!_instantiated) {
synchronized {
if (_instance is null) {
_instance = new WriterThread;
}
_instantiated = true;
}
}
return _instance;
}
/**
* Writes the args in a thread-safe manner.
*/
void write(T...)(T args) {
_tid.send(text(args));
}
/**
* Writes the args in a thread-safe manner and appends a newline.
*/
void writeln(T...)(T args) {
write(args, "\n");
}
/**
* Writes the args in a thread-safe manner in green (POSIX only).
* and appends a newline.
*/
void writelnGreen(T...)(T args) {
_tid.send(green(text(args) ~ "\n"));
}
/**
* Writes the args in a thread-safe manner in red (POSIX only)
* and appends a newline.
*/
void writelnRed(T...)(T args) {
_tid.send(red(text(args) ~ "\n"));
}
/**
* Writes the args in a thread-safe manner in red (POSIX only).
* and appends a newline.
*/
void writeRed(T...)(T args) {
_tid.send(red(text(args)));
}
/**
* Writes the args in a thread-safe manner in yellow (POSIX only).
* and appends a newline.
*/
void writeYellow(T...)(T args) {
_tid.send(yellow(text(args)));
}
/**
* Creates the singleton instance and waits until it's ready.
*/
static void start() {
WriterThread.get._tid.send(true, thisTid);
receiveOnly!bool; //wait for it to start
}
/**
* Waits for the writer thread to terminate.
*/
void join() {
_tid.send(thisTid); //tell it to join
receiveOnly!Tid(); //wait for it to join
_instance = null;
_instantiated = false;
}
private:
enum Color {
red,
green,
yellow,
cancel,
}
this() {
_tid = spawn(&threadWriter);
version (Posix) {
import core.sys.posix.unistd;
_useEscCodes = _forceEscCodes || isatty(stdout.fileno()) != 0;
}
}
/**
* Generate green coloured output on POSIX systems
*/
string green(in string msg) @safe pure const {
return escCode(Color.green) ~ msg ~ escCode(Color.cancel);
}
/**
* Generate red coloured output on POSIX systems
*/
string red(in string msg) @safe pure const {
return escCode(Color.red) ~ msg ~ escCode(Color.cancel);
}
/**
* Generate yellow coloured output on POSIX systems
*/
string yellow(in string msg) @safe pure const {
return escCode(Color.yellow) ~ msg ~ escCode(Color.cancel);
}
/**
* Send escape code to the console
*/
string escCode(in Color code) @safe pure const {
return _useEscCodes ? _escCodes[code] : "";
}
Tid _tid;
static immutable string[] _escCodes = ["\033[31;1m", "\033[32;1m", "\033[33;1m",
"\033[0;;m"];
bool _useEscCodes;
static bool _instantiated; /// Thread local
__gshared WriterThread _instance;
}
private void threadWriter()
{
auto done = false;
Tid _tid;
auto saveStdout = stdout;
auto saveStderr = stderr;
scope (exit) {
saveStdout.flush();
stdout = saveStdout;
stderr = saveStderr;
}
if (!isDebugOutputEnabled()) {
version (Posix) {
enum nullFileName = "/dev/null";
} else {
enum nullFileName = "NUL";
}
stdout = File(nullFileName, "w");
stderr = File(nullFileName, "w");
}
while (!done) {
string output;
receive(
(string msg) {
output ~= msg;
},
(bool, Tid tid) {
//another thread is waiting for confirmation
//that we started, let them know it's ok to proceed
tid.send(true);
},
(Tid tid) {
done = true;
_tid = tid;
},
(OwnerTerminated trm) {
done = true;
}
);
saveStdout.write(output);
}
if (_tid != Tid.init)
_tid.send(thisTid);
}
unittest
{
//make sure this can be brought up and down again
WriterThread.get.join;
WriterThread.get.join;
}