OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library appengine.logging_impl; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:fixnum/fixnum.dart'; |
| 10 import 'package:appengine/api/logging.dart'; |
| 11 |
| 12 import '../protobuf_api/rpc/rpc_service.dart'; |
| 13 import '../protobuf_api/logging_service.dart'; |
| 14 import '../protobuf_api/internal/log_service.pb.dart' as pb; |
| 15 |
| 16 class LoggingRpcImpl extends Logging { |
| 17 final LoggingServiceClientRPCStub _clientRPCStub; |
| 18 final List<pb.UserAppLogLine> _logLines = <pb.UserAppLogLine>[]; |
| 19 |
| 20 LoggingRpcImpl(RPCService rpcService, String ticket) |
| 21 : _clientRPCStub = new LoggingServiceClientRPCStub(rpcService, ticket); |
| 22 |
| 23 void log(LogLevel level, String message, {DateTime timestamp}) { |
| 24 if (timestamp == null) { |
| 25 timestamp = new DateTime.now(); |
| 26 } |
| 27 |
| 28 // Issue 15747158. |
| 29 print('$timestamp: ApplicationLog | $level: ${message.trim()}'); |
| 30 |
| 31 _logLines.add(_createLogLine(level, message, timestamp)); |
| 32 } |
| 33 |
| 34 pb.UserAppLogLine _createLogLine( |
| 35 LogLevel level, String message, DateTime timestamp) { |
| 36 var timestampUsec = timestamp.toUtc().millisecondsSinceEpoch * 1000; |
| 37 return new pb.UserAppLogLine() |
| 38 ..timestampUsec = new Int64(timestampUsec) |
| 39 ..level = new Int64(level.level) |
| 40 ..message = message; |
| 41 } |
| 42 |
| 43 Future flush() { |
| 44 if (_logLines.isEmpty) { |
| 45 return new Future.value(); |
| 46 } |
| 47 |
| 48 var group = new pb.UserAppLogGroup(); |
| 49 group.logLine.addAll(_logLines); |
| 50 _logLines.clear(); |
| 51 |
| 52 var request = new pb.FlushRequest() |
| 53 ..logs = group.writeToBuffer(); |
| 54 |
| 55 // NOTE: We swallow errors here to prevent clients sending error messages |
| 56 // using the logging service -- which would create an infinite error loop. |
| 57 // TODO: We could introduce a local logging mechanism in the future which |
| 58 // we could use to report such errors. |
| 59 return _clientRPCStub.Flush(request).catchError((_) {}).then((_) => null); |
| 60 } |
| 61 } |
OLD | NEW |