Skip to content
Browse files

Merge "Prettify uses longest source name to size column"

  • Loading branch information...
2 parents 54b6221 + 882c0cd commit 98c3196a974c4b713a96aff5fe71d01a94648001 @dsabeti dsabeti committed with Gerrit Code Review
Showing with 40 additions and 1 deletion.
  1. +4 −1 lib/steno/json_prettifier.rb
  2. +36 −0 spec/unit/json_prettifier_spec.rb
View
5 lib/steno/json_prettifier.rb
@@ -9,6 +9,7 @@ module Steno
class Steno::JsonPrettifier
FIELD_ORDER = %w[timestamp source process_id thread_id fiber_id location data
log_level message]
+ MIN_COL_WIDTH = 14
class ParseError < StandardError
end
@@ -16,6 +17,7 @@ class ParseError < StandardError
def initialize(excluded_fields = [])
@time_format = "%Y-%m-%d %H:%M:%S.%6N"
@excluded_fields = Set.new(excluded_fields)
+ @max_src_len = MIN_COL_WIDTH
end
def prettify_line(line)
@@ -63,7 +65,8 @@ def format_timestamp(record)
end
def format_source(record)
- "%14s" % record["source"]
+ @max_src_len = [@max_src_len, record["source"].length].max
+ record["source"].ljust(@max_src_len)
end
def format_process_id(record)
View
36 spec/unit/json_prettifier_spec.rb
@@ -28,6 +28,42 @@
prettified.should match(exp_regex)
end
+ it "should always use the largest src len to determine src column width" do
+ test_srcs = [
+ 'a' * (Steno::JsonPrettifier::MIN_COL_WIDTH - 3),
+ 'a' * (Steno::JsonPrettifier::MIN_COL_WIDTH - 1),
+ 'a' * (Steno::JsonPrettifier::MIN_COL_WIDTH),
+ 'a' * (Steno::JsonPrettifier::MIN_COL_WIDTH + 1),
+ 'a' * (Steno::JsonPrettifier::MIN_COL_WIDTH - 3),
+ 'a' * (Steno::JsonPrettifier::MIN_COL_WIDTH + 3),
+ 'a' * (Steno::JsonPrettifier::MIN_COL_WIDTH - 2),
+ 'a' * (Steno::JsonPrettifier::MIN_COL_WIDTH + 2)
+ ]
+
+ regex = ['\d{4}-\d{2}-\d{2}', # YYYY-MM-DD
+ '\d{2}:\d{2}:\d{2}\.\d{6}', # HH:MM:SS.uS
+ '([a-zA-Z0-9\ ]+)', # Source (to be captured)
+ 'pid=\d+', # Process id
+ '.+' # Everything else
+ ].join("\s") + "\n"
+
+ max_src_len = Steno::JsonPrettifier::MIN_COL_WIDTH
+ test_srcs.each do |src|
+ record = Steno::Record.new(src,
+ :info,
+ "message",
+ ["filename", "line", "method"],
+ "test" => "data")
+
+ encoded = codec.encode_record(record)
+ prettified = prettifier.prettify_line(encoded)
+ src_col = prettified.match(regex)[1]
+
+ max_src_len = [max_src_len, src.length].max
+ src_col.length.should == max_src_len
+ end
+ end
+
it "should raise a parse error when the json-encoded string is not a hash" do
expect {
prettifier.prettify_line("[1,2,3]")

0 comments on commit 98c3196

Please sign in to comment.
Something went wrong with that request. Please try again.