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