aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ruby/pb/test/server.rb
diff options
context:
space:
mode:
authorGravatar Tim Emiola <temiola@google.com>2015-11-10 14:59:15 -0800
committerGravatar Tim Emiola <temiola@google.com>2015-11-11 13:47:37 -0800
commit69a672e6c2cdbcf253ed2bc7a9cda13bb5db2ceb (patch)
tree0442c6d6cbe6315f8670772aa6178be3b4441746 /src/ruby/pb/test/server.rb
parent7d21c04b2c9d9405bbd85c5a7ba0ece0cbae8b7a (diff)
Adds standard logging to the client/server
- truncates the server debug log entries to make that readable
Diffstat (limited to 'src/ruby/pb/test/server.rb')
-rwxr-xr-xsrc/ruby/pb/test/server.rb56
1 files changed, 55 insertions, 1 deletions
diff --git a/src/ruby/pb/test/server.rb b/src/ruby/pb/test/server.rb
index 25c1b1e9e6..0d5670f5a1 100755
--- a/src/ruby/pb/test/server.rb
+++ b/src/ruby/pb/test/server.rb
@@ -45,6 +45,7 @@ $LOAD_PATH.unshift(pb_dir) unless $LOAD_PATH.include?(pb_dir)
$LOAD_PATH.unshift(this_dir) unless $LOAD_PATH.include?(this_dir)
require 'forwardable'
+require 'logger'
require 'optparse'
require 'grpc'
@@ -53,6 +54,59 @@ require 'test/proto/empty'
require 'test/proto/messages'
require 'test/proto/test_services'
+# DebugIsTruncated extends the default Logger to truncate debug messages
+class DebugIsTruncated < Logger
+ def debug(s)
+ super(truncate(s, 1024))
+ end
+
+ # Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>:
+ #
+ # 'Once upon a time in a world far far away'.truncate(27)
+ # # => "Once upon a time in a wo..."
+ #
+ # Pass a string or regexp <tt>:separator</tt> to truncate +text+ at a natural break:
+ #
+ # 'Once upon a time in a world far far away'.truncate(27, separator: ' ')
+ # # => "Once upon a time in a..."
+ #
+ # 'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
+ # # => "Once upon a time in a..."
+ #
+ # The last characters will be replaced with the <tt>:omission</tt> string (defaults to "...")
+ # for a total length not exceeding <tt>length</tt>:
+ #
+ # 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
+ # # => "And they f... (continued)"
+ def truncate(s, truncate_at, options = {})
+ return s unless s.length > truncate_at
+ omission = options[:omission] || '...'
+ with_extra_room = truncate_at - omission.length
+ stop = \
+ if options[:separator]
+ rindex(options[:separator], with_extra_room) || with_extra_room
+ else
+ with_extra_room
+ end
+ "#{s[0, stop]}#{omission}"
+ end
+end
+
+# RubyLogger defines a logger for gRPC based on the standard ruby logger.
+module RubyLogger
+ def logger
+ LOGGER
+ end
+
+ LOGGER = DebugIsTruncated.new(STDOUT)
+end
+
+# GRPC is the general RPC module
+module GRPC
+ # Inject the noop #logger if no module-level logger method has been injected.
+ extend RubyLogger
+end
+
# loads the certificates by the test server.
def load_test_certs
this_dir = File.expand_path(File.dirname(__FILE__))
@@ -113,7 +167,7 @@ class TestTarget < Grpc::Testing::TestService::Service
def streaming_input_call(call)
sizes = call.each_remote_read.map { |x| x.payload.body.length }
- sum = sizes.inject { |s, x| s + x }
+ sum = sizes.inject(0) { |s, x| s + x }
StreamingInputCallResponse.new(aggregated_payload_size: sum)
end