OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 instrumentation; | 5 library instrumentation; |
6 | 6 |
7 /** | 7 /** |
8 * The interface used by client code to communicate with an instrumentation | 8 * The interface used by client code to communicate with an instrumentation |
9 * server. | 9 * server. |
10 */ | 10 */ |
11 abstract class InstrumentationServer { | 11 abstract class InstrumentationServer { |
12 /** | 12 /** |
13 * Pass the given [message] to the instrumentation server so that it will be | 13 * Pass the given [message] to the instrumentation server so that it will be |
14 * logged with other messages. | 14 * logged with other messages. |
| 15 * |
| 16 * This method should be used for most logging. |
15 */ | 17 */ |
16 void log(String message); | 18 void log(String message); |
17 | 19 |
18 /** | 20 /** |
| 21 * Pass the given [message] to the instrumentation server so that it will be |
| 22 * logged with other messages. |
| 23 * |
| 24 * This method should only be used for logging high priority messages, such as |
| 25 * exceptions that cause the server to shutdown. |
| 26 */ |
| 27 void logWithPriority(String message); |
| 28 |
| 29 /** |
19 * Signal that the client is done communicating with the instrumentation | 30 * Signal that the client is done communicating with the instrumentation |
20 * server. This method should be invoked exactly one time and no other methods | 31 * server. This method should be invoked exactly one time and no other methods |
21 * should be invoked on this instance after this method has been invoked. | 32 * should be invoked on this instance after this method has been invoked. |
22 */ | 33 */ |
23 void shutdown(); | 34 void shutdown(); |
24 } | 35 } |
25 | 36 |
26 /** | 37 /** |
27 * An [InstrumentationServer] that ignores all instrumentation requests sent to | 38 * The interface used by client code to communicate with an instrumentation |
28 * it. It can be used when no instrumentation data is to be collected as a way | 39 * server by wrapping an [InstrumentationServer]. |
29 * to avoid needing to check for null values. | |
30 */ | 40 */ |
31 class NullInstrumentationServer implements InstrumentationServer { | 41 class InstrumentationService { |
32 /** | 42 /** |
33 * Initialize a newly created instance of this class. | 43 * An instrumentation service that will not log any instrumentation data. |
34 */ | 44 */ |
35 const NullInstrumentationServer(); | 45 static const InstrumentationService NULL_SERVICE = |
| 46 const InstrumentationService(null); |
36 | 47 |
37 @override | 48 static const String TAG_NOTIFICATION = 'Noti'; |
38 void log(String message) { | 49 static const String TAG_REQUEST = 'Req'; |
| 50 static const String TAG_RESPONSE = 'Res'; |
| 51 static const String TAG_VERSION = 'Ver'; |
| 52 /** |
| 53 * The instrumentation server used to communicate with the server, or `null` |
| 54 * if instrumentation data should not be logged. |
| 55 */ |
| 56 final InstrumentationServer instrumentationServer; |
| 57 |
| 58 /** |
| 59 * Initialize a newly created instrumentation service to comunicate with the |
| 60 * given instrumentation server. |
| 61 */ |
| 62 const InstrumentationService(this.instrumentationServer); |
| 63 |
| 64 /** |
| 65 * The current time, expressed as a decimal encoded number of milliseconds. |
| 66 */ |
| 67 String get _timestamp => new DateTime.now().millisecond.toString(); |
| 68 |
| 69 /** |
| 70 * Log that a notification has been sent to the client. |
| 71 */ |
| 72 void logNotification(String notification) { |
| 73 _log(TAG_NOTIFICATION, notification); |
39 } | 74 } |
40 | 75 |
41 @override | 76 /** |
| 77 * Log that a request has been sent to the client. |
| 78 */ |
| 79 void logRequest(String request) { |
| 80 _log(TAG_REQUEST, request); |
| 81 } |
| 82 |
| 83 /** |
| 84 * Log that a response has been sent to the client. |
| 85 */ |
| 86 void logResponse(String response) { |
| 87 _log(TAG_RESPONSE, response); |
| 88 } |
| 89 |
| 90 /** |
| 91 * Signal that the client is done communicating with the instrumentation |
| 92 * server. This method should be invoked exactly one time and no other methods |
| 93 * should be invoked on this instance after this method has been invoked. |
| 94 */ |
42 void shutdown() { | 95 void shutdown() { |
| 96 if (instrumentationServer != null) { |
| 97 instrumentationServer.shutdown(); |
| 98 } |
| 99 } |
| 100 |
| 101 /** |
| 102 * Log the given message with the given tag. |
| 103 */ |
| 104 void _log(String tag, String message) { |
| 105 if (instrumentationServer != null) { |
| 106 instrumentationServer.log('$_timestamp:$tag:$message'); |
| 107 } |
43 } | 108 } |
44 } | 109 } |
OLD | NEW |