| 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:io'; |
| 8 import 'dart:utf' as utf; | 8 import 'dart:utf' as utf; |
| 9 | 9 |
| 10 class DebugLogger { | 10 class DebugLogger { |
| 11 static IOSink _sink; | 11 static OutputStream _stream; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * If [path] was null, the DebugLogger will write messages to stdout. | 14 * If [path] was null, the DebugLogger will write messages to stdout. |
| 15 */ | 15 */ |
| 16 static init(Path path, {append: false}) { | 16 static init(Path path, {append: false}) { |
| 17 if (path != null) { | 17 if (path != null) { |
| 18 var mode = append ? FileMode.APPEND : FileMode.WRITE; | 18 var mode = append ? FileMode.APPEND : FileMode.WRITE; |
| 19 _sink = new File.fromPath(path).openWrite(mode: mode); | 19 _stream = new File.fromPath(path).openOutputStream(mode); |
| 20 } | 20 } |
| 21 } | 21 } |
| 22 | 22 |
| 23 static void close() { | 23 static void close() { |
| 24 if (_sink != null) { | 24 if (_stream != null) { |
| 25 _sink.close(); | 25 _stream.close(); |
| 26 _sink = null; | 26 _stream = null; |
| 27 } | 27 } |
| 28 } | 28 } |
| 29 | 29 |
| 30 static void info(String msg) { | 30 static void info(String msg) { |
| 31 _print("Info: $msg"); | 31 _print("Info: $msg"); |
| 32 } | 32 } |
| 33 | 33 |
| 34 static void warning(String msg) { | 34 static void warning(String msg) { |
| 35 _print("Warning: $msg"); | 35 _print("Warning: $msg"); |
| 36 } | 36 } |
| 37 | 37 |
| 38 static void error(String msg) { | 38 static void error(String msg) { |
| 39 _print("Error: $msg"); | 39 _print("Error: $msg"); |
| 40 } | 40 } |
| 41 | 41 |
| 42 static void _print(String msg) { | 42 static void _print(String msg) { |
| 43 if (_sink != null) { | 43 if (_stream != null) { |
| 44 _sink.writeln(msg); | 44 _stream.write(encodeUtf8(msg)); |
| 45 _stream.write([0x0a]); |
| 45 } else { | 46 } else { |
| 46 print(msg); | 47 print(msg); |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 } | 50 } |
| 50 | 51 |
| 51 List<int> encodeUtf8(String string) { | 52 List<int> encodeUtf8(String string) { |
| 52 return utf.encodeUtf8(string); | 53 return utf.encodeUtf8(string); |
| 53 } | 54 } |
| 54 | 55 |
| 55 // 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 |
| 56 // invalid utf8-encoded input to the log. | 57 // invalid utf8-encoded input to the log. |
| 57 // Currently invalid bytes will be replaced by a replacement character. | 58 // Currently invalid bytes will be replaced by a replacement character. |
| 58 String decodeUtf8(List<int> bytes) { | 59 String decodeUtf8(List<int> bytes) { |
| 59 return utf.decodeUtf8(bytes); | 60 return utf.decodeUtf8(bytes); |
| 60 } | 61 } |
| 61 | 62 |
| OLD | NEW |