Skip to content

Commit

Permalink
Merge "Prettify uses longest source name to size column"
Browse files Browse the repository at this point in the history
  • Loading branch information
David Sabeti authored and Gerrit Code Review committed Oct 3, 2012
2 parents 54b6221 + 882c0cd commit 98c3196
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/steno/json_prettifier.rb
Expand Up @@ -9,13 +9,15 @@ 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

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)
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions spec/unit/json_prettifier_spec.rb
Expand Up @@ -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]")
Expand Down

0 comments on commit 98c3196

Please sign in to comment.