Skip to content

Commit

Permalink
Convert annotation to string before printing. (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Dec 3, 2023
1 parent 558486a commit 68366a6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/console/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
message[:arguments] = arguments
end

if annotation = Fiber.current.annotation
message[:annotation] = annotation
end

if block_given?
if block.arity.zero?
message[:message] = yield
Expand Down
10 changes: 8 additions & 2 deletions lib/console/terminal/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,14 @@ def default_suffix(object = nil)
buffer = +""

if @verbose
if annotation = Fiber.current.annotation and annotation.size > 0
buffer << ": #{@terminal[:annotation]}#{annotation}#{@terminal.reset}"
if annotation = Fiber.current.annotation
# While typically annotations should be strings, that is not always the case.
annotation = annotation.to_s

# If the annotation is empty, we don't want to print it, as it will look like a formatting bug.
if annotation.size > 0
buffer << ": #{@terminal[:annotation]}#{annotation}#{@terminal.reset}"
end
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/console/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,19 @@ def after
expect(Console::Logger.default_log_level({'CONSOLE_LEVEL' => 'debug'})).to be == Console::Logger::DEBUG
end
end

with "Fiber annotation" do
it "logs fiber annotations" do
Fiber.new do
Fiber.annotate("Running in a fiber.")

logger.info(message)
end.resume

expect(output.last).to have_keys(
annotation: be == "Running in a fiber.",
subject: be == "Hello World",
)
end
end
end
30 changes: 30 additions & 0 deletions test/console/serialized/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,34 @@
expect(error_message).to have_keys(:kind, :message, :stack)
end
end

with "Fiber annotation" do
it "logs fiber annotations" do
Fiber.new do
Fiber.annotate("Running in a fiber.")

logger.call(message)
end.resume

expect(record).to have_keys(
annotation: be == "Running in a fiber.",
subject: be == "Hello World",
)
end

it "logs fiber annotations when it isn't a string" do
thing = ["Running in a fiber."]

Fiber.new do
Fiber.annotate(thing)

logger.call(message)
end.resume

expect(record).to have_keys(
annotation: be == thing,
subject: be == "Hello World",
)
end
end
end
24 changes: 24 additions & 0 deletions test/console/terminal/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,28 @@
expect(io.string).to be(:include?, message)
end
end

with "Fiber annotation" do
it "logs fiber annotations" do
Fiber.new do
Fiber.annotate("Running in a fiber.")

logger.call(message)
end.resume

expect(io.string).to be(:include?, "Running in a fiber.")
end

it "logs fiber annotations when it isn't a string" do
thing = ["Running in a fiber."]

Fiber.new do
Fiber.annotate(thing)

logger.call(message)
end.resume

expect(io.string).to be(:include?, thing.to_s)
end
end
end

0 comments on commit 68366a6

Please sign in to comment.