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