Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
- `Proxy#rotate` to an upstream proxy failed every HTTPS request with `ERR_TUNNEL_CONNECTION_FAILED`
- On JRuby `browser.pages`, `windows(:last)` and `workers` came back in random order, so the window a page had just
opened wasn't the last one. Targets are kept in the order they were created now
- On JRuby lines written to a `:logger` IO from several threads at once could get mixed up, they're written one by one now

### Removed
- `Browser::Process#default_user_agent`, `#browser_version`, `#protocol_version`, `#v8_version` and `#webkit_version`,
Expand Down
4 changes: 3 additions & 1 deletion lib/ferrum/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def initialize(logger, channels: CHANNELS, only: [], skip: [], format: :default,
end
@formatter = FORMATS.fetch(format).new(color: @color)
@commands = Concurrent::Map.new if format == :annotated || only.any? || skip.any?
@mutex = Mutex.new
end

# Whether the channel is written to the logger.
Expand Down Expand Up @@ -235,7 +236,8 @@ def write(channel, &block)
if logger.respond_to?(:debug)
logger.debug("ferrum:#{channel}", &block)
else
logger.puts(block.call)
line = block.call
@mutex.synchronize { logger.puts(line) }
end
rescue StandardError => e
warn "Ferrum: couldn't write to the logger, #{e.class}: #{e.message}" unless @write_failed
Expand Down
1 change: 1 addition & 0 deletions sig/ferrum/log.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Ferrum
@color: bool
@formatter: (Default | Json | Annotated)
@write_failed: bool?
@mutex: ::Thread::Mutex
@commands: ::Concurrent::Map[[untyped, ::Integer], [String, ::Float]]?

def self.parse: (String? value) -> { channels: Array[Symbol], only: Array[String], skip: Array[String] }?
Expand Down
16 changes: 16 additions & 0 deletions spec/unit/log_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

describe Ferrum::Log do
describe "#browser" do
it "keeps lines whole when written from several threads at once" do
io = StringIO.new
log = described_class.new(io)
lines = Array.new(8) { |i| i.to_s * 512 }

lines.map { |line| Thread.new { 200.times { log.browser(line) } } }.each(&:join)

expect(io.string.lines(chomp: true)).to all(satisfy { |l| lines.include?(l) })
expect(io.string.lines.size).to eq(8 * 200)
end
end
end
Loading