Skip to content

Commit

Permalink
🐛 Added missing debug_* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pboling committed Mar 1, 2024
1 parent 970ac23 commit ec96e31
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 21 deletions.
16 changes: 16 additions & 0 deletions lib/debug_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ def debug_colorized_chain_for_class=(colorized_chain_for_class)
@debug_logging_configuration.colorized_chain_for_class = colorized_chain_for_class
end

def debug_add_timestamp
@debug_logging_configuration.add_timestamp
end

def debug_add_timestamp=(add_timestamp)
@debug_logging_configuration.add_timestamp = add_timestamp
end

def debug_time_formatter_proc
@debug_logging_configuration.time_formatter_proc
end

def debug_time_formatter_proc=(time_formatter_proc)
@debug_logging_configuration.time_formatter_proc = time_formatter_proc
end

def debug_add_invocation_id
@debug_logging_configuration.add_invocation_id
end
Expand Down
8 changes: 4 additions & 4 deletions lib/debug_logging/argument_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def debug_invocation_id_to_s(args: nil, kwargs: nil, start_at: nil, config_proxy

# @return [String]
def debug_time_to_s(time_or_monotonic, config_proxy: nil)
return "" unless config_proxy&.add_timestamp
return config_proxy.time_formatter_proc.call(Time.now) unless time_or_monotonic
return "" unless config_proxy&.debug_add_timestamp
return config_proxy.debug_time_formatter_proc.call(Time.now) unless time_or_monotonic

time = Util.debug_time(time_or_monotonic)

config_proxy.time_formatter_proc.call(time)
config_proxy.debug_time_formatter_proc.call(time)
end

# A custom time format will never apply here, because ActiveSupport::Notifications have a required time format
Expand Down Expand Up @@ -194,7 +194,7 @@ def debug_payload_to_s(payload: nil, config_proxy: nil)
proc_name: "add_payload",
proc: config_proxy.debug_add_payload,
args: payload,
max_length: config_proxy.payload_max_length,
max_length: config_proxy.debug_payload_max_length,
)
printed_payload += printed
printed_payload += config_proxy.debug_ellipsis if add_payload_ellipsis
Expand Down
141 changes: 127 additions & 14 deletions spec/debug_logging/argument_printer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,23 @@
subject(:debug_time_to_s) { instance.debug_time_to_s(time_or_monotonic, config_proxy:) }

let(:config_proxy) {
double(
"config_proxy",
add_timestamp: true,
time_formatter_proc: DebugLogging::Constants::DEFAULT_TIME_FORMATTER
instance_double(
DebugLogging::Configuration,
debug_add_timestamp: true,
debug_time_formatter_proc: DebugLogging::Constants::DEFAULT_TIME_FORMATTER,
)
}

context "when no config_proxy" do
let(:config_proxy) { nil }

let(:time_or_monotonic) { 0.1 }

it "prints" do
expect(debug_time_to_s).to eq("")
end
end

context "when float" do
let(:time_or_monotonic) { 0.1 }

Expand Down Expand Up @@ -116,6 +126,14 @@
end
end

context "when empty" do
let(:time_or_monotonic) { [] }

it "prints" do
expect(debug_event_time_to_s).to match(event_time_format_regex)
end
end

context "when otherwise" do
let(:time_or_monotonic) { :time }

Expand All @@ -126,24 +144,119 @@
end

describe "#debug_invocation_to_s" do
subject(:debug_invocation_to_s) { instance.debug_invocation_to_s(klass: nil, separator: nil, method_to_log: nil, config_proxy:) }
subject(:debug_invocation_to_s) { instance.debug_invocation_to_s(klass:, separator:, method_to_log:, config_proxy:) }

let(:klass) { nil }
let(:separator) { nil }
let(:method_to_log) { nil }
let(:config_proxy) { false }

it "prints" do
expect(debug_invocation_to_s).to eq("")
end

context "when config_proxy" do
let(:config_proxy) {
instance_double(
DebugLogging::Configuration,
debug_colorized_chain_for_class: ->(str) { str.blue },
debug_colorized_chain_for_method: ->(str) { str.blue },
)
}
let(:klass) { ParentSingletonClass }
let(:separator) { "^.^" }
let(:method_to_log) { :shoe_fly }

it "prints" do
expect(debug_invocation_to_s).to eq("\e[0;34;49mParentSingletonClass\e[0m^.^\e[0;34;49mshoe_fly\e[0m")
end
end
end

describe "#debug_signature_to_s" do
subject(:debug_signature_to_s) { instance.debug_signature_to_s(args: nil, config_proxy: nil) }
subject(:debug_signature_to_s) { instance.debug_signature_to_s(args:, config_proxy:) }

let(:args) { false }
let(:config_proxy) { false }
let(:args) { nil }
let(:config_proxy) { nil }

it "prints" do
expect(debug_signature_to_s).to eq("")
end

context "when config_proxy" do
let(:config_proxy) {
instance_double(
DebugLogging::Configuration,
debug_last_hash_to_s_proc: ->(hash) { hash.keys.to_s },
debug_args_to_s_proc: ->(args) { args.to_s[0..9] },
debug_args_max_length: 10,
debug_multiple_last_hashes: false,
debug_last_hash_max_length: 15,
)
}
let(:args) { [1, 2, 3, {zz: :top}] }

it "prints" do
expect(debug_signature_to_s).to eq("([1, 2, 3], [:zz])")
end
end

context "when ellipsis" do
let(:config_proxy) {
instance_double(
DebugLogging::Configuration,
debug_last_hash_to_s_proc: ->(hash) { hash.keys.to_s },
debug_args_to_s_proc: ->(args) { args.to_s[0..9] },
debug_args_max_length: 3,
debug_multiple_last_hashes: false,
debug_last_hash_max_length: 5,
debug_ellipsis: ".•.",
)
}
let(:args) { [1, 2, 3, {zz_yy_xx: :top}] }

it "prints" do
expect(debug_signature_to_s).to eq("([1, .•., [:zz_y.•.)")
end
end

context "when multiple last hashes" do
let(:config_proxy) {
instance_double(
DebugLogging::Configuration,
debug_last_hash_to_s_proc: ->(hash) { hash.keys.to_s },
debug_args_to_s_proc: ->(args) { args.to_s[0..9] },
debug_args_max_length: 10,
debug_multiple_last_hashes: true,
debug_last_hash_max_length: 15,
debug_ellipsis: "..."
)
}
let(:args) { [1, 2, 3, {zz: :top}, {def: :leopard}] }

it "prints" do
expect(debug_signature_to_s).to eq("([1, 2, 3], [:zz], [:def])")
end
end

context "when multiple last hashes and ellipsis" do
let(:config_proxy) {
instance_double(
DebugLogging::Configuration,
debug_last_hash_to_s_proc: ->(hash) { hash.keys.to_s },
debug_args_to_s_proc: ->(args) { args.to_s[0..9] },
debug_args_max_length: 5,
debug_multiple_last_hashes: true,
debug_last_hash_max_length: 7,
debug_ellipsis: ".~."
)
}
let(:args) { [1, 2, 3, {zz: :top}, {def_leopard_pours_sugar: :on_me}] }

it "prints" do
expect(debug_signature_to_s).to eq("([1, 2,.~., [:zz], [:def_le.~.)")
end
end
end

describe "#debug_payload_to_s" do
Expand All @@ -162,10 +275,10 @@
let(:payload) { [] }
let(:payload_max_length) { 15 }
let(:config_proxy) {
double(
"config_proxy",
instance_double(
DebugLogging::Configuration,
debug_add_payload: ->(args) { "pppaaayyylllo #{args}" },
payload_max_length: payload_max_length,
debug_payload_max_length: payload_max_length,
debug_ellipsis: "^^^",
)
}
Expand All @@ -179,10 +292,10 @@
let(:payload) { [] }
let(:payload_max_length) { 15 }
let(:config_proxy) {
double(
"config_proxy",
instance_double(
DebugLogging::Configuration,
debug_add_payload: ->(args) { "pppaaayyy #{args}" },
payload_max_length: payload_max_length,
debug_payload_max_length: payload_max_length,
debug_ellipsis: "^^^",
)
}
Expand Down
6 changes: 3 additions & 3 deletions spec/debug_logging/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
end

context "when time" do
let(:time_or_monotonic) { Time.new(2023, 10, 31, 3, 5, 23) }
let(:time_or_monotonic) { Time.new(2017, 10, 31, 3, 5, 23) }

it "returns a time" do
expect(debug_time.to_i).to eq(1698696323)
expect(debug_time.year).to eq(2017)
end
end

Expand All @@ -55,7 +55,7 @@
let(:time_or_monotonic) { Time.new(2023, 10, 31, 3, 5, 23).to_s }

it "returns a time" do
expect(debug_time.to_i).to eq(1698696323)
expect(debug_time.year).to eq(2023)
end
end

Expand Down

0 comments on commit ec96e31

Please sign in to comment.