OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library input.transformer.log_file; | |
6 | |
7 import 'dart:convert'; | |
8 | |
9 import 'package:analyzer/src/generated/java_engine.dart'; | |
10 import 'package:logging/logging.dart'; | |
11 | |
12 import 'input_converter.dart'; | |
13 import 'operation.dart'; | |
14 | |
15 const CONNECTED_MSG_FRAGMENT = ' <= {"event":"server.connected"'; | |
16 final int NINE = '9'.codeUnitAt(0); | |
17 const RECEIVED_FRAGMENT = ' <= {'; | |
18 const SENT_FRAGMENT = ' => {'; | |
19 final int ZERO = '0'.codeUnitAt(0); | |
20 | |
21 /** | |
22 * [LogFileInputConverter] converts a log file stream | |
23 * into a series of operations to be sent to the analysis server. | |
24 */ | |
25 class LogFileInputConverter extends CommonInputConverter { | |
26 LogFileInputConverter(String tmpSrcDirPath, Map<String, String> srcPathMap, | |
27 {int diagnosticPort}) | |
28 : super(tmpSrcDirPath, srcPathMap, diagnosticPort: diagnosticPort); | |
29 | |
30 @override | |
31 Operation convert(String line) { | |
32 try { | |
33 String timeStampString = _parseTimeStamp(line); | |
34 String data = line.substring(timeStampString.length); | |
35 if (data.startsWith(RECEIVED_FRAGMENT)) { | |
36 Map<String, dynamic> json = JSON.decode(data.substring(4)); | |
37 if (json.containsKey('event')) { | |
38 return convertNotification(json); | |
39 } else { | |
40 return convertResponse(json); | |
41 } | |
42 } else if (data.startsWith(SENT_FRAGMENT)) { | |
43 Map<String, dynamic> json = JSON.decode(data.substring(4)); | |
44 if (json.containsKey('method')) { | |
45 return convertRequest(json); | |
46 } | |
47 return null; | |
48 } | |
49 logger.log(Level.INFO, 'unknown input line: $line'); | |
50 return null; | |
51 } catch (e, s) { | |
52 throw new AnalysisException( | |
53 'Failed to parse line\n $line', new CaughtException(e, s)); | |
54 } | |
55 } | |
56 | |
57 /** | |
58 * Determine if the given line is from an instrumentation file. | |
59 * For example: | |
60 * `1428347977499 <= {"event":"server.connected","params":{"version":"1.6.0"}}
` | |
61 */ | |
62 static bool isFormat(String line) { | |
63 String timeStampString = _parseTimeStamp(line); | |
64 int start = timeStampString.length; | |
65 int end = start + CONNECTED_MSG_FRAGMENT.length; | |
66 return (10 < start && end < line.length) && | |
67 line.substring(start, end) == CONNECTED_MSG_FRAGMENT; | |
68 } | |
69 | |
70 /** | |
71 * Parse the given line and return the millisecond timestamp or `null` | |
72 * if it cannot be determined. | |
73 */ | |
74 static String _parseTimeStamp(String line) { | |
75 int index = 0; | |
76 while (index < line.length) { | |
77 int code = line.codeUnitAt(index); | |
78 if (code < ZERO || NINE < code) { | |
79 return line.substring(0, index); | |
80 } | |
81 ++index; | |
82 } | |
83 return line; | |
84 } | |
85 } | |
OLD | NEW |