| 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(Map<String, String> srcPathMap) : super(srcPathMap); |
| 27 |
| 28 @override |
| 29 Operation convert(String line) { |
| 30 try { |
| 31 String timeStampString = _parseTimeStamp(line); |
| 32 String data = line.substring(timeStampString.length); |
| 33 if (data.startsWith(RECEIVED_FRAGMENT)) { |
| 34 Map<String, dynamic> json = JSON.decode(data.substring(4)); |
| 35 if (json.containsKey('event')) { |
| 36 return convertNotification(json); |
| 37 } |
| 38 return null; |
| 39 } else if (data.startsWith(SENT_FRAGMENT)) { |
| 40 Map<String, dynamic> json = JSON.decode(data.substring(4)); |
| 41 if (json.containsKey('method')) { |
| 42 return convertRequest(json); |
| 43 } |
| 44 return null; |
| 45 } |
| 46 logger.log(Level.INFO, 'unknown input line: $line'); |
| 47 return null; |
| 48 } catch (e, s) { |
| 49 throw new AnalysisException( |
| 50 'Failed to parse line\n $line', new CaughtException(e, s)); |
| 51 } |
| 52 } |
| 53 |
| 54 /** |
| 55 * Determine if the given line is from an instrumentation file. |
| 56 * For example: |
| 57 * `1428347977499 <= {"event":"server.connected","params":{"version":"1.6.0"}}
` |
| 58 */ |
| 59 static bool isFormat(String line) { |
| 60 String timeStampString = _parseTimeStamp(line); |
| 61 int start = timeStampString.length; |
| 62 int end = start + CONNECTED_MSG_FRAGMENT.length; |
| 63 return (10 < start && end < line.length) && |
| 64 line.substring(start, end) == CONNECTED_MSG_FRAGMENT; |
| 65 } |
| 66 |
| 67 /** |
| 68 * Parse the given line and return the millisecond timestamp or `null` |
| 69 * if it cannot be determined. |
| 70 */ |
| 71 static String _parseTimeStamp(String line) { |
| 72 int index = 0; |
| 73 while (index < line.length) { |
| 74 int code = line.codeUnitAt(index); |
| 75 if (code < ZERO || NINE < code) { |
| 76 return line.substring(0, index); |
| 77 } |
| 78 ++index; |
| 79 } |
| 80 return line; |
| 81 } |
| 82 } |
| OLD | NEW |