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'; | |
11 | 10 |
12 _deepCopy(src) { | 11 _deepCopy(src) { |
13 if (src is Map) { | 12 if (src is Map) { |
14 var dest = {}; | 13 var dest = {}; |
15 src.forEach((key, val) { | 14 src.forEach((key, val) { |
16 dest[key] = _deepCopy(val); | 15 dest[key] = _deepCopy(val); |
17 }); | 16 }); |
18 return dest; | 17 return dest; |
19 } else if (src is List) { | 18 } else if (src is List) { |
20 var dest = []; | 19 var dest = []; |
(...skipping 15 matching lines...) Expand all Loading... |
36 | 35 |
37 String toString() { | 36 String toString() { |
38 return "[${timeStamp}] ${message}"; | 37 return "[${timeStamp}] ${message}"; |
39 } | 38 } |
40 | 39 |
41 int _time; | 40 int _time; |
42 String message; | 41 String message; |
43 Map map; | 42 Map map; |
44 } | 43 } |
45 | 44 |
46 class Tracer extends Observable { | 45 class Tracer { |
47 // The current global tracer. | 46 // The current global tracer. |
48 static Tracer get current => _current; | 47 static Tracer get current => _current; |
49 | 48 |
50 static Tracer _current; | 49 static Tracer _current; |
51 | 50 |
52 static void start() { | 51 static void start() { |
53 if (_current == null) { | 52 if (_current == null) { |
54 _current = new Tracer(); | 53 _current = new Tracer(); |
55 } | 54 } |
56 } | 55 } |
57 | 56 |
58 static void stop() { | 57 static void stop() { |
59 if (_current != null) { | 58 if (_current != null) { |
60 _current.cancel(); | 59 _current.cancel(); |
61 _current = null; | 60 _current = null; |
62 } | 61 } |
63 } | 62 } |
64 | 63 |
65 // The tracer subscribes to all logging events. | 64 // The tracer subscribes to all logging events. |
66 StreamSubscription loggerSub = null; | 65 StreamSubscription loggerSub = null; |
67 | 66 |
68 // The start time for the current request. | 67 // The start time for the current request. |
69 Stopwatch _time; | 68 Stopwatch _time; |
70 | 69 |
71 // A list of all tracing events for thre current request. | 70 // A list of all tracing events for thre current request. |
72 ObservableList<TraceEvent> events = new ObservableList<TraceEvent>(); | 71 List<TraceEvent> events = <TraceEvent>[]; |
73 | 72 |
74 Tracer() { | 73 Tracer() { |
75 _time = new Stopwatch(); | 74 _time = new Stopwatch(); |
76 _time.start(); | 75 _time.start(); |
77 loggerSub = Logger.root.onRecord.listen((LogRecord rec) { | 76 loggerSub = Logger.root.onRecord.listen((LogRecord rec) { |
78 // Echo all log messages to the trace. | 77 // Echo all log messages to the trace. |
79 trace('${rec.level.name}: ${rec.message}'); | 78 trace('${rec.level.name}: ${rec.message}'); |
80 }); | 79 }); |
81 reset(); | 80 reset(); |
82 } | 81 } |
83 | 82 |
84 void cancel() { | 83 void cancel() { |
85 loggerSub.cancel(); | 84 loggerSub.cancel(); |
86 } | 85 } |
87 | 86 |
88 void reset() { | 87 void reset() { |
89 _time.reset(); | 88 _time.reset(); |
90 events.clear(); | 89 events.clear(); |
91 } | 90 } |
92 | 91 |
93 TraceEvent trace(String message, {Map map: null}) { | 92 TraceEvent trace(String message, {Map map: null}) { |
94 var event = new TraceEvent.msg(_time.elapsedMicroseconds, message, map); | 93 var event = new TraceEvent.msg(_time.elapsedMicroseconds, message, map); |
95 events.add(event); | 94 events.add(event); |
96 return event; | 95 return event; |
97 } | 96 } |
98 } | 97 } |
OLD | NEW |