132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2007 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Oct 2007: Initial release
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 Synchronized, formatted console output. This can be used in lieu
|
|
12 of true logging where appropriate.
|
|
13
|
|
14 Trace exposes this style of usage:
|
|
15 ---
|
|
16 Trace.format ("abc {}", 1); => abc 1
|
|
17 Trace.format ("abc {}:{}", 1, 2); => abc 1:2
|
|
18 Trace.format ("abc {1}:{0}", 1, 2); => abc 2:1
|
|
19 ---
|
|
20
|
|
21 Special character sequences, such as "\n", are written directly to
|
|
22 the output without any translation (though an output-filter could
|
|
23 be inserted to perform translation as required). Platform-specific
|
|
24 newlines are generated instead via the formatln() method, which also
|
|
25 flushes the output when configured to do so:
|
|
26 ---
|
|
27 Trace.formatln ("hello {}", "world");
|
|
28 ---
|
|
29
|
|
30 Explicitly flushing the output is achieved via a flush() method
|
|
31 ---
|
|
32 Trace.format ("hello {}", "world").flush;
|
|
33 ---
|
|
34
|
|
35 *******************************************************************************/
|
|
36
|
|
37 module tango.util.log.Trace;
|
|
38
|
|
39 private import tango.io.Console;
|
|
40
|
|
41 private import tango.io.model.IConduit;
|
|
42
|
|
43 private import tango.text.convert.Layout;
|
|
44
|
|
45 /*******************************************************************************
|
|
46
|
|
47 Construct Trace when this module is loaded
|
|
48
|
|
49 *******************************************************************************/
|
|
50
|
|
51 /// global trace instance
|
|
52 public static SyncPrint Trace;
|
|
53
|
|
54 static this()
|
|
55 {
|
|
56 Trace = new SyncPrint (Cerr.stream, Cerr, !Cerr.redirected);
|
|
57 }
|
|
58
|
|
59 /*******************************************************************************
|
|
60
|
|
61 Intended for internal use only
|
|
62
|
|
63 *******************************************************************************/
|
|
64
|
|
65 private class SyncPrint
|
|
66 {
|
|
67 private Object mutex;
|
|
68 private OutputStream output;
|
|
69 private Layout!(char) convert;
|
|
70 private bool flushLines;
|
|
71
|
|
72 version (Win32)
|
|
73 private const char[] Eol = "\r\n";
|
|
74 else
|
|
75 private const char[] Eol = "\n";
|
|
76
|
|
77 /**********************************************************************
|
|
78
|
|
79 Construct a Print instance, tying the provided stream
|
|
80 to a layout formatter
|
|
81
|
|
82 **********************************************************************/
|
|
83
|
|
84 this (OutputStream output, Object mutex, bool flush=false)
|
|
85 {
|
|
86 this.mutex = mutex;
|
|
87 this.output = output;
|
|
88 this.flushLines = flush;
|
|
89 this.convert = new Layout!(char);
|
|
90 }
|
|
91
|
|
92 /**********************************************************************
|
|
93
|
|
94 Layout using the provided formatting specification
|
|
95
|
|
96 **********************************************************************/
|
|
97
|
|
98 final SyncPrint format (char[] fmt, ...)
|
|
99 {
|
|
100 synchronized (mutex)
|
|
101 convert (&sink, _arguments, _argptr, fmt);
|
|
102 return this;
|
|
103 }
|
|
104
|
|
105 /**********************************************************************
|
|
106
|
|
107 Layout using the provided formatting specification
|
|
108
|
|
109 **********************************************************************/
|
|
110
|
|
111 final SyncPrint formatln (char[] fmt, ...)
|
|
112 {
|
|
113 synchronized (mutex)
|
|
114 {
|
|
115 convert (&sink, _arguments, _argptr, fmt);
|
|
116 output.write (Eol);
|
|
117 if (flushLines)
|
|
118 output.flush;
|
|
119 }
|
|
120 return this;
|
|
121 }
|
|
122
|
|
123 /**********************************************************************
|
|
124
|
|
125 Flush the output stream
|
|
126
|
|
127 **********************************************************************/
|
|
128
|
|
129 final void flush ()
|
|
130 {
|
|
131 synchronized (mutex)
|
|
132 output.flush;
|
|
133 }
|
|
134
|
|
135 /**********************************************************************
|
|
136
|
|
137 Sink for passing to the formatter
|
|
138
|
|
139 **********************************************************************/
|
|
140
|
|
141 private final uint sink (char[] s)
|
|
142 {
|
|
143 return output.write (s);
|
|
144 }
|
|
145 }
|