132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2005 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Nov 2005: Initial release
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 Standard, global formatters for console output. If you don't need
|
|
12 formatted output or unicode translation, consider using the module
|
|
13 tango.io.Console directly
|
|
14
|
|
15 Stdout & Stderr expose this style of usage:
|
|
16 ---
|
|
17 Stdout ("hello"); => hello
|
|
18 Stdout (1); => 1
|
|
19 Stdout (3.14); => 3.14
|
|
20 Stdout ('b'); => b
|
|
21 Stdout (1, 2, 3); => 1, 2, 3
|
|
22 Stdout ("abc", 1, 2, 3); => abc, 1, 2, 3
|
|
23 Stdout ("abc", 1, 2) ("foo"); => abc, 1, 2foo
|
|
24 Stdout ("abc") ("def") (3.14); => abcdef3.14
|
|
25
|
|
26 Stdout.format ("abc {}", 1); => abc 1
|
|
27 Stdout.format ("abc {}:{}", 1, 2); => abc 1:2
|
|
28 Stdout.format ("abc {1}:{0}", 1, 2); => abc 2:1
|
|
29 Stdout.format ("abc ", 1); => abc
|
|
30 ---
|
|
31
|
|
32 Note that the last example does not throw an exception. There
|
|
33 are several use-cases where dropping an argument is legitimate,
|
|
34 so we're currently not enforcing any particular trap mechanism.
|
|
35
|
|
36 Flushing the output is achieved through the flush() method, or
|
|
37 via an empty pair of parens:
|
|
38 ---
|
|
39 Stdout ("hello world") ();
|
|
40 Stdout ("hello world").flush;
|
|
41
|
|
42 Stdout.format ("hello {}", "world") ();
|
|
43 Stdout.format ("hello {}", "world").flush;
|
|
44 ---
|
|
45
|
|
46 Special character sequences, such as "\n", are written directly to
|
|
47 the output without any translation (though an output-filter could
|
|
48 be inserted to perform translation as required). Platform-specific
|
|
49 newlines are generated instead via the newline() method, which also
|
|
50 flushes the output when configured to do so:
|
|
51 ---
|
|
52 Stdout ("hello ") ("world").newline;
|
|
53 Stdout.format ("hello {}", "world").newline;
|
|
54 Stdout.formatln ("hello {}", "world");
|
|
55 ---
|
|
56
|
|
57 The format() method of both Stderr and Stdout support the range
|
|
58 of formatting options provided by tango.text.convert.Layout and
|
|
59 extensions thereof; including the full I18N extensions where it
|
|
60 has been configured in that manner. To enable a French Stdout,
|
|
61 do the following:
|
|
62 ---
|
|
63 import tango.text.locale.Locale;
|
|
64
|
|
65 Stdout.layout = new Locale (Culture.getCulture ("fr-FR"));
|
|
66 ---
|
|
67
|
|
68 Note that Stdout is a shared entity, so every usage of it will
|
|
69 be affected by the above example. For applications supporting
|
|
70 multiple regions, create multiple Locale instances instead and
|
|
71 cache them in an appropriate manner
|
|
72
|
|
73 Note also that the output-stream in use is exposed by these
|
|
74 global instances ~ this can be leveraged, for instance, to copy a
|
|
75 file to the standard output:
|
|
76 ---
|
|
77 Stdout.copy (new FileConduit ("myfile"));
|
|
78 ---
|
|
79
|
|
80 Note that Stdout is *not* intended to be thread-safe. Use either
|
|
81 tango.util.log.Trace or the standard logging facilities in order
|
|
82 to enable atomic console I/O
|
|
83
|
|
84 *******************************************************************************/
|
|
85
|
|
86 module tango.io.Stdout;
|
|
87
|
|
88 private import tango.io.Print,
|
|
89 tango.io.Console;
|
|
90
|
|
91 private import tango.text.convert.Layout;
|
|
92
|
|
93 /*******************************************************************************
|
|
94
|
|
95 Construct Stdout & Stderr when this module is loaded
|
|
96
|
|
97 *******************************************************************************/
|
|
98
|
|
99 static this()
|
|
100 {
|
|
101 auto layout = new Layout!(char);
|
|
102
|
|
103 Stdout = new Print!(char) (layout, Cout.stream);
|
|
104 Stderr = new Print!(char) (layout, Cerr.stream);
|
|
105
|
|
106 Stdout.flush = !Cout.redirected;
|
|
107 Stderr.flush = !Cerr.redirected;
|
|
108 }
|
|
109
|
|
110 public static Print!(char) Stdout, /// global standard output
|
|
111 Stderr; /// global error output
|
|
112
|
|
113
|
|
114 /******************************************************************************
|
|
115
|
|
116 ******************************************************************************/
|
|
117
|
|
118 debug (Stdout)
|
|
119 {
|
|
120 void main()
|
|
121 {
|
|
122 Stdout ("hello").newline;
|
|
123 Stdout (1).newline;
|
|
124 Stdout (3.14).newline;
|
|
125 Stdout ('b').newline;
|
|
126 Stdout ("abc") ("def") (3.14).newline;
|
|
127 Stdout ("abc", 1, 2, 3).newline;
|
|
128 Stdout (1, 2, 3).newline;
|
|
129 Stdout (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1).newline;
|
|
130
|
|
131 Stdout ("abc {}{}{}", 1, 2, 3).newline;
|
|
132 Stdout.format ("abc {}{}{}", 1, 2, 3).newline;
|
|
133 }
|
|
134 }
|