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 tracer; | 5 library tracer; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:logging/logging.dart'; | 9 import 'package:logging/logging.dart'; |
10 import 'package:observe/observe.dart'; | 10 import 'package:observe/observe.dart'; |
11 | 11 |
12 _deepCopy(src) { | 12 _deepCopy(src) { |
13 if (src is Map) { | 13 if (src is Map) { |
14 var dest = {}; | 14 var dest = {}; |
15 src.forEach((key, val) { | 15 src.forEach((key, val) { |
16 dest[key] = _deepCopy(val); | 16 dest[key] = _deepCopy(val); |
17 }); | 17 }); |
18 return dest; | 18 return dest; |
19 } else if (src is List) { | 19 } else if (src is List) { |
20 var dest = []; | 20 var dest = []; |
21 src.forEach((val) { | 21 src.forEach((val) { |
22 dest.add(_deepCopy(val)); | 22 dest.add(_deepCopy(val)); |
23 }); | 23 }); |
24 return dest; | 24 return dest; |
25 } else { | 25 } else { |
26 return src; | 26 return src; |
27 } | 27 } |
28 } | 28 } |
29 | 29 |
30 class TraceEvent { | 30 class TraceEvent { |
31 TraceEvent.msg(this._time, this.message, Map originalMap) { | 31 TraceEvent.msg(this._time, this.message, Map originalMap) { |
32 map = _deepCopy(originalMap); | 32 map = _deepCopy(originalMap); |
(...skipping 14 matching lines...) Expand all Loading... |
47 // The current global tracer. | 47 // The current global tracer. |
48 static Tracer get current => _current; | 48 static Tracer get current => _current; |
49 | 49 |
50 static Tracer _current; | 50 static Tracer _current; |
51 | 51 |
52 static void start() { | 52 static void start() { |
53 if (_current == null) { | 53 if (_current == null) { |
54 _current = new Tracer(); | 54 _current = new Tracer(); |
55 } | 55 } |
56 } | 56 } |
| 57 |
57 static void stop() { | 58 static void stop() { |
58 if (_current != null) { | 59 if (_current != null) { |
59 _current.cancel(); | 60 _current.cancel(); |
60 _current = null; | 61 _current = null; |
61 } | 62 } |
62 } | 63 } |
63 | 64 |
64 // The tracer subscribes to all logging events. | 65 // The tracer subscribes to all logging events. |
65 StreamSubscription loggerSub = null; | 66 StreamSubscription loggerSub = null; |
66 | 67 |
67 // The start time for the current request. | 68 // The start time for the current request. |
68 Stopwatch _time; | 69 Stopwatch _time; |
69 | 70 |
70 // A list of all tracing events for thre current request. | 71 // A list of all tracing events for thre current request. |
71 ObservableList<TraceEvent> events = new ObservableList<TraceEvent>(); | 72 ObservableList<TraceEvent> events = new ObservableList<TraceEvent>(); |
72 | 73 |
73 Tracer() { | 74 Tracer() { |
74 _time = new Stopwatch(); | 75 _time = new Stopwatch(); |
75 _time.start(); | 76 _time.start(); |
76 loggerSub = Logger.root.onRecord.listen((LogRecord rec) { | 77 loggerSub = Logger.root.onRecord.listen((LogRecord rec) { |
77 // Echo all log messages to the trace. | 78 // Echo all log messages to the trace. |
78 trace('${rec.level.name}: ${rec.message}'); | 79 trace('${rec.level.name}: ${rec.message}'); |
79 }); | 80 }); |
80 reset(); | 81 reset(); |
81 } | 82 } |
82 | 83 |
83 void cancel() { | 84 void cancel() { |
84 loggerSub.cancel(); | 85 loggerSub.cancel(); |
85 } | 86 } |
86 | 87 |
87 void reset() { | 88 void reset() { |
88 _time.reset(); | 89 _time.reset(); |
89 events.clear(); | 90 events.clear(); |
90 } | 91 } |
91 | 92 |
92 TraceEvent trace(String message, {Map map: null}) { | 93 TraceEvent trace(String message, {Map map: null}) { |
93 var event = new TraceEvent.msg(_time.elapsedMicroseconds, message, map); | 94 var event = new TraceEvent.msg(_time.elapsedMicroseconds, message, map); |
94 events.add(event); | 95 events.add(event); |
95 return event; | 96 return event; |
96 } | 97 } |
97 } | 98 } |
OLD | NEW |