OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Message logging. | |
6 library log; | |
7 | |
8 import 'dart:io'; | |
9 import 'dart:async'; | |
10 | |
11 import 'io.dart'; | |
12 import 'utils.dart'; | |
13 | |
14 typedef LogFn(Entry entry); | |
15 final Map<Level, LogFn> _loggers = new Map<Level, LogFn>(); | |
16 | |
17 /// The list of recorded log messages. Will only be recorded if | |
18 /// [recordTranscript()] is called. | |
19 List<Entry> _transcript; | |
20 | |
21 /// An enum type for defining the different logging levels. By default, [ERROR] | |
22 /// and [WARNING] messages are printed to sterr. [MESSAGE] messages are printed | |
23 /// to stdout, and others are ignored. | |
24 class Level { | |
25 /// An error occurred and an operation could not be completed. Usually shown | |
26 /// to the user on stderr. | |
27 static const ERROR = const Level._("ERR "); | |
28 | |
29 /// Something unexpected happened, but the program was able to continue, | |
30 /// though possibly in a degraded fashion. | |
31 static const WARNING = const Level._("WARN"); | |
32 | |
33 /// A message intended specifically to be shown to the user. | |
34 static const MESSAGE = const Level._("MSG "); | |
35 | |
36 /// Some interaction with the external world occurred, such as a network | |
37 /// operation, process spawning, or file IO. | |
38 static const IO = const Level._("IO "); | |
39 | |
40 /// Incremental output during pub's version constraint solver. | |
41 static const SOLVER = const Level._("SLVR"); | |
42 | |
43 /// Fine-grained and verbose additional information. Can be used to provide | |
44 /// program state context for other logs (such as what pub was doing when an | |
45 /// IO operation occurred) or just more detail for an operation. | |
46 static const FINE = const Level._("FINE"); | |
47 | |
48 const Level._(this.name); | |
49 final String name; | |
50 | |
51 String toString() => name; | |
52 int get hashCode => name.hashCode; | |
53 } | |
54 | |
55 /// A single log entry. | |
56 class Entry { | |
57 final Level level; | |
58 final List<String> lines; | |
59 | |
60 Entry(this.level, this.lines); | |
61 } | |
62 | |
63 /// Logs [message] at [Level.ERROR]. | |
64 void error(message, [error]) { | |
65 if (error != null) { | |
66 message = "$message: $error"; | |
67 var trace = getAttachedStackTrace(error); | |
68 if (trace != null) { | |
69 message = "$message\nStackTrace: $trace"; | |
70 } | |
71 } | |
72 write(Level.ERROR, message); | |
73 } | |
74 | |
75 /// Logs [message] at [Level.WARNING]. | |
76 void warning(message) => write(Level.WARNING, message); | |
77 | |
78 /// Logs [message] at [Level.MESSAGE]. | |
79 void message(message) => write(Level.MESSAGE, message); | |
80 | |
81 /// Logs [message] at [Level.IO]. | |
82 void io(message) => write(Level.IO, message); | |
83 | |
84 /// Logs [message] at [Level.SOLVER]. | |
85 void solver(message) => write(Level.SOLVER, message); | |
86 | |
87 /// Logs [message] at [Level.FINE]. | |
88 void fine(message) => write(Level.FINE, message); | |
89 | |
90 /// Logs [message] at [level]. | |
91 void write(Level level, message) { | |
92 if (_loggers.isEmpty) showNormal(); | |
93 | |
94 var lines = splitLines(message.toString()); | |
95 var entry = new Entry(level, lines); | |
96 | |
97 var logFn = _loggers[level]; | |
98 if (logFn != null) logFn(entry); | |
99 | |
100 if (_transcript != null) _transcript.add(entry); | |
101 } | |
102 | |
103 /// Logs an asynchronous IO operation. Logs [startMessage] before the operation | |
104 /// starts, then when [operation] completes, invokes [endMessage] with the | |
105 /// completion value and logs the result of that. Returns a future that | |
106 /// completes after the logging is done. | |
107 /// | |
108 /// If [endMessage] is omitted, then logs "Begin [startMessage]" before the | |
109 /// operation and "End [startMessage]" after it. | |
110 Future ioAsync(String startMessage, Future operation, | |
111 [String endMessage(value)]) { | |
112 if (endMessage == null) { | |
113 io("Begin $startMessage."); | |
114 } else { | |
115 io(startMessage); | |
116 } | |
117 | |
118 return operation.then((result) { | |
119 if (endMessage == null) { | |
120 io("End $startMessage."); | |
121 } else { | |
122 io(endMessage(result)); | |
123 } | |
124 return result; | |
125 }); | |
126 } | |
127 | |
128 /// Logs the spawning of an [executable] process with [arguments] at [IO] | |
129 /// level. | |
130 void process(String executable, List<String> arguments) { | |
131 io("Spawning $executable ${arguments.join(' ')}"); | |
132 } | |
133 | |
134 /// Logs the results of running [executable]. | |
135 void processResult(String executable, PubProcessResult result) { | |
136 // Log it all as one message so that it shows up as a single unit in the logs. | |
137 var buffer = new StringBuffer(); | |
138 buffer.write("Finished $executable. Exit code ${result.exitCode}."); | |
139 | |
140 dumpOutput(String name, List<String> output) { | |
141 if (output.length == 0) { | |
142 buffer.write("Nothing output on $name."); | |
143 } else { | |
144 buffer.write("$name:"); | |
145 var numLines = 0; | |
146 for (var line in output) { | |
147 if (++numLines > 1000) { | |
148 buffer.write('[${output.length - 1000}] more lines of output ' | |
149 'truncated...]'); | |
150 break; | |
151 } | |
152 | |
153 buffer.write(line); | |
154 } | |
155 } | |
156 } | |
157 | |
158 dumpOutput("stdout", result.stdout); | |
159 dumpOutput("stderr", result.stderr); | |
160 | |
161 io(buffer.toString()); | |
162 } | |
163 | |
164 /// Enables recording of log entries. | |
165 void recordTranscript() { | |
166 _transcript = <Entry>[]; | |
167 } | |
168 | |
169 /// If [recordTranscript()] was called, then prints the previously recorded log | |
170 /// transcript to stderr. | |
171 void dumpTranscript() { | |
172 if (_transcript == null) return; | |
173 | |
174 stderr.writeln('---- Log transcript ----'); | |
175 for (var entry in _transcript) { | |
176 _logToStderrWithLabel(entry); | |
177 } | |
178 stderr.writeln('---- End log transcript ----'); | |
179 } | |
180 | |
181 /// Sets the verbosity to "normal", which shows errors, warnings, and messages. | |
182 void showNormal() { | |
183 _loggers[Level.ERROR] = _logToStderr; | |
184 _loggers[Level.WARNING] = _logToStderr; | |
185 _loggers[Level.MESSAGE] = _logToStdout; | |
186 _loggers[Level.IO] = null; | |
187 _loggers[Level.SOLVER] = null; | |
188 _loggers[Level.FINE] = null; | |
189 } | |
190 | |
191 /// Sets the verbosity to "io", which shows errors, warnings, messages, and IO | |
192 /// event logs. | |
193 void showIO() { | |
194 _loggers[Level.ERROR] = _logToStderrWithLabel; | |
195 _loggers[Level.WARNING] = _logToStderrWithLabel; | |
196 _loggers[Level.MESSAGE] = _logToStdoutWithLabel; | |
197 _loggers[Level.IO] = _logToStderrWithLabel; | |
198 _loggers[Level.SOLVER] = null; | |
199 _loggers[Level.FINE] = null; | |
200 } | |
201 | |
202 /// Sets the verbosity to "solver", which shows errors, warnings, messages, and | |
203 /// solver logs. | |
204 void showSolver() { | |
205 _loggers[Level.ERROR] = _logToStderr; | |
206 _loggers[Level.WARNING] = _logToStderr; | |
207 _loggers[Level.MESSAGE] = _logToStdout; | |
208 _loggers[Level.IO] = null; | |
209 _loggers[Level.SOLVER] = _logToStdout; | |
210 _loggers[Level.FINE] = null; | |
211 } | |
212 | |
213 /// Sets the verbosity to "all", which logs ALL the things. | |
214 void showAll() { | |
215 _loggers[Level.ERROR] = _logToStderrWithLabel; | |
216 _loggers[Level.WARNING] = _logToStderrWithLabel; | |
217 _loggers[Level.MESSAGE] = _logToStdoutWithLabel; | |
218 _loggers[Level.IO] = _logToStderrWithLabel; | |
219 _loggers[Level.SOLVER] = _logToStderrWithLabel; | |
220 _loggers[Level.FINE] = _logToStderrWithLabel; | |
221 } | |
222 | |
223 /// Log function that prints the message to stdout. | |
224 void _logToStdout(Entry entry) { | |
225 _logToStream(stdout, entry, showLabel: false); | |
226 } | |
227 | |
228 /// Log function that prints the message to stdout with the level name. | |
229 void _logToStdoutWithLabel(Entry entry) { | |
230 _logToStream(stdout, entry, showLabel: true); | |
231 } | |
232 | |
233 /// Log function that prints the message to stderr. | |
234 void _logToStderr(Entry entry) { | |
235 _logToStream(stderr, entry, showLabel: false); | |
236 } | |
237 | |
238 /// Log function that prints the message to stderr with the level name. | |
239 void _logToStderrWithLabel(Entry entry) { | |
240 _logToStream(stderr, entry, showLabel: true); | |
241 } | |
242 | |
243 void _logToStream(IOSink sink, Entry entry, {bool showLabel}) { | |
244 bool firstLine = true; | |
245 for (var line in entry.lines) { | |
246 if (showLabel) { | |
247 if (firstLine) { | |
248 sink.write('${entry.level.name}: '); | |
249 } else { | |
250 sink.write(' | '); | |
251 } | |
252 } | |
253 | |
254 sink.writeln(line); | |
255 | |
256 firstLine = false; | |
257 } | |
258 } | |
OLD | NEW |