OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library utils; | 5 library utils; |
6 | 6 |
| 7 import 'dart:io'; |
7 import 'dart:utf' as utf; | 8 import 'dart:utf' as utf; |
8 | 9 |
| 10 class DebugLogger { |
| 11 static OutputStream _stream; |
| 12 |
| 13 /** |
| 14 * If [path] was null, the DebugLogger will write messages to stdout. |
| 15 */ |
| 16 static init(Path path, {append: false}) { |
| 17 if (path != null) { |
| 18 var mode = append ? FileMode.APPEND : FileMode.WRITE; |
| 19 _stream = new File.fromPath(path).openOutputStream(mode); |
| 20 } |
| 21 } |
| 22 |
| 23 static void close() { |
| 24 if (_stream != null) { |
| 25 _stream.close(); |
| 26 _stream = null; |
| 27 } |
| 28 } |
| 29 |
| 30 static void info(String msg) { |
| 31 _print("Info: $msg"); |
| 32 } |
| 33 |
| 34 static void warning(String msg) { |
| 35 _print("Warning: $msg"); |
| 36 } |
| 37 |
| 38 static void error(String msg) { |
| 39 _print("Error: $msg"); |
| 40 } |
| 41 |
| 42 static void _print(String msg) { |
| 43 if (_stream != null) { |
| 44 _stream.write(encodeUtf8(msg)); |
| 45 _stream.write([0x0a]); |
| 46 } else { |
| 47 print(msg); |
| 48 } |
| 49 } |
| 50 } |
9 | 51 |
10 List<int> encodeUtf8(String string) { | 52 List<int> encodeUtf8(String string) { |
11 return utf.encodeUtf8(string); | 53 return utf.encodeUtf8(string); |
12 } | 54 } |
13 | 55 |
14 // TODO(kustermann,ricow): As soon we have a debug log we should log | 56 // TODO(kustermann,ricow): As soon we have a debug log we should log |
15 // invalid utf8-encoded input to the log. | 57 // invalid utf8-encoded input to the log. |
16 // Currently invalid bytes will be replaced by a replacement character. | 58 // Currently invalid bytes will be replaced by a replacement character. |
17 String decodeUtf8(List<int> bytes) { | 59 String decodeUtf8(List<int> bytes) { |
18 return utf.decodeUtf8(bytes); | 60 return utf.decodeUtf8(bytes); |
19 } | 61 } |
20 | 62 |
OLD | NEW |