Skip to content

Commit

Permalink
Remove dependency on logger
Browse files Browse the repository at this point in the history
* To fix the Ruby 3.3.5 warnings:
  #1061
* concurrent-ruby only uses 7 constants from Logger, so just copy those over.
  • Loading branch information
eregon committed Oct 7, 2024
1 parent 044020f commit d7ce956
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ To use the tools in the Edge gem it must be required separately:
require 'concurrent-edge'
```

If the library does not behave as expected, `Concurrent.use_stdlib_logger(Logger::DEBUG)` could
If the library does not behave as expected, `Concurrent.use_simple_logger(:DEBUG)` could
help to reveal the problem.

## Installation
Expand Down
6 changes: 1 addition & 5 deletions docs-source/actor/celluloid_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
# require 'stackprof'
# require 'profiler'

logger = Logger.new($stderr)
logger.level = Logger::INFO
Concurrent.configuration.logger = lambda do |level, progname, message = nil, &block|
logger.add level, message, progname, &block
end
Concurrent.use_simple_logger(:INFO)

scale = 1
ADD_TO = (100 * scale).to_i
Expand Down
3 changes: 1 addition & 2 deletions docs-source/actor/io.in.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'concurrent'

# logger = Logger.new(STDOUT)
# Concurrent.configuration.logger = logger.method(:add)
# Concurrent.use_simple_logger(:WARN, STDOUT)

# First option is to use operation pool

Expand Down
3 changes: 1 addition & 2 deletions docs-source/actor/io.out.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'concurrent' # => false

# logger = Logger.new(STDOUT)
# Concurrent.configuration.logger = logger.method(:add)
# Concurrent.use_simple_logger(:WARN, STDOUT)

# First option is to use operation pool

Expand Down
8 changes: 4 additions & 4 deletions docs-source/actor/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ Spawned actor cannot be garbage-collected until it's terminated. There is a refe

Actors are running on shared thread poll which allows user to create many actors cheaply.
Downside is that these actors cannot be directly used to do IO or other blocking operations.
Blocking operations could starve the `default_task_pool`. However there are two options:
Blocking operations could starve the `global_fast_executor`. However there are two options:

- Create an regular actor which will schedule blocking operations in `global_operation_pool`
- Create an regular actor which will schedule blocking operations in `global_io_executor`
(which is intended for blocking operations) sending results back to self in messages.
- Create an actor using `global_operation_pool` instead of `global_task_pool`, e.g.
`AnIOActor.spawn name: :blocking, executor: Concurrent.configuration.global_operation_pool`.
- Create an actor using `global_io_executor` instead of `global_fast_executor`, e.g.
`AnIOActor.spawn name: :blocking, executor: Concurrent.global_io_executor`.

### Example

Expand Down
2 changes: 1 addition & 1 deletion examples/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ def do_stuff(*args)
:stuff
end

Concurrent.use_simple_logger Logger::DEBUG
Concurrent.use_simple_logger :DEBUG
4 changes: 2 additions & 2 deletions lib/concurrent-ruby-edge/concurrent/actor/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def default_reference_class
Reference
end

# override to se different default executor, e.g. to change it to global_operation_pool
# override to se different default executor, e.g. to change it to global_fast_executor
# @return [Executor]
def default_executor
Concurrent.global_io_executor
Expand All @@ -109,7 +109,7 @@ def ask(message)
# @example by option hash
# inc2 = AdHoc.spawn(name: 'increment by 2',
# args: [2],
# executor: Concurrent.configuration.global_task_pool) do |increment_by|
# executor: Concurrent.global_fast_executor) do |increment_by|
# lambda { |number| number + increment_by }
# end
# inc2.ask!(2) # => 4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'logger'
require 'concurrent/concern/logging'
require 'concurrent/actor/public_delegations'

module Concurrent
module Actor
module InternalDelegations
include PublicDelegations
include Logger::Severity
include Concurrent::Concern::Logging

# @see Core#children
def children
Expand Down
18 changes: 9 additions & 9 deletions lib/concurrent-ruby-edge/concurrent/edge/erlang_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def initialize(mailbox, environment, name, executor)
end

def tell_op(message)
log Logger::DEBUG, @Pid, told: message
log DEBUG, @Pid, told: message
if (mailbox = @Mailbox)
mailbox.push_op(message).then { @Pid }
else
Expand All @@ -683,7 +683,7 @@ def tell_op(message)
end

def tell(message, timeout = nil)
log Logger::DEBUG, @Pid, told: message
log DEBUG, @Pid, told: message
if (mailbox = @Mailbox)
timed_out = mailbox.push message, timeout
timeout ? timed_out : @Pid
Expand All @@ -693,7 +693,7 @@ def tell(message, timeout = nil)
end

def ask(message, timeout, timeout_value)
log Logger::DEBUG, @Pid, asked: message
log DEBUG, @Pid, asked: message
if @Terminated.resolved?
raise NoActor.new(@Pid)
else
Expand Down Expand Up @@ -724,7 +724,7 @@ def ask(message, timeout, timeout_value)
end

def ask_op(message, probe)
log Logger::DEBUG, @Pid, asked: message
log DEBUG, @Pid, asked: message
if @Terminated.resolved?
probe.reject NoActor.new(@Pid), false
else
Expand Down Expand Up @@ -1029,7 +1029,7 @@ def terminate_self(reason, value)
end

def after_termination(final_reason)
log Logger::DEBUG, @Pid, terminated: final_reason
log DEBUG, @Pid, terminated: final_reason
clean_reply NoActor.new(@Pid)
while true
message = @Mailbox.try_pop NOTHING
Expand Down Expand Up @@ -1071,7 +1071,7 @@ def run(*args, &body)
inner_run(*args, &body).
run(Run::TEST).
then(&method(:after_termination)).
rescue { |e| log Logger::ERROR, e }
rescue { |e| log ERROR, e }
end

def receive(*rules, timeout: nil, timeout_value: nil, keep: false, &given_block)
Expand Down Expand Up @@ -1163,7 +1163,7 @@ def internal_receive
end

message_future.then(start, self) do |message, s, _actor|
log Logger::DEBUG, pid, got: message
log DEBUG, pid, got: message
catch(JUMP) do
if (message = consume_signal(message)) == NOTHING
@timeout = [@timeout + s - Concurrent.monotonic_time, 0].max if s
Expand Down Expand Up @@ -1230,7 +1230,7 @@ def receive(*rules, timeout: nil, timeout_value: nil, &given_block)
matcher = -> m { m.is_a?(Ask) ? rules_matcher === m.message : rules_matcher === m }
while true
message = @Mailbox.pop_matching(matcher, timeout, TIMEOUT)
log Logger::DEBUG, pid, got: message
log DEBUG, pid, got: message
unless (message = consume_signal(message)) == NOTHING
rules.each do |rule, job|
return eval_task(message, job) if rule === message
Expand Down Expand Up @@ -1535,7 +1535,7 @@ class NoReply < Error
def self.create(type, channel, environment, name, executor)
actor = KLASS_MAP.fetch(type).new(channel, environment, name, executor)
ensure
log Logger::DEBUG, actor.pid, created: caller[1] if actor
log Concern::Logging::DEBUG, actor.pid, created: caller[1] if actor
end

KLASS_MAP = {
Expand Down
29 changes: 17 additions & 12 deletions lib/concurrent-ruby/concurrent/concern/logging.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'logger'
require 'concurrent/atomic/atomic_reference'

module Concurrent
Expand All @@ -8,10 +7,12 @@ module Concern
#
# @!visibility private
module Logging
include Logger::Severity
# The same as Logger::Severity but we copy it here to avoid a dependency on the logger gem just for these 7 constants
DEBUG, INFO, WARN, ERROR, FATAL, UNKNOWN = 0, 1, 2, 3, 4, 5
SEV_LABEL = %w[DEBUG INFO WARN ERROR FATAL ANY].freeze

# Logs through {Concurrent.global_logger}, it can be overridden by setting @logger
# @param [Integer] level one of Logger::Severity constants
# @param [Integer] level one of Concurrent::Concern::Logging constants
# @param [String] progname e.g. a path of an Actor
# @param [String, nil] message when nil block is used to generate the message
# @yieldreturn [String] a message
Expand All @@ -23,7 +24,7 @@ def log(level, progname, message = nil, &block)
end
logger.call level, progname, message, &block
rescue => error
$stderr.puts "`Concurrent.configuration.logger` failed to log #{[level, progname, message, block]}\n" +
$stderr.puts "`Concurrent.global_logger` failed to log #{[level, progname, message, block]}\n" +
"#{error.message} (#{error.class})\n#{error.backtrace.join "\n"}"
end
end
Expand All @@ -33,8 +34,10 @@ def log(level, progname, message = nil, &block)
module Concurrent
extend Concern::Logging

# @return [Logger] Logger with provided level and output.
def self.create_simple_logger(level = Logger::FATAL, output = $stderr)
# Create a simple logger with provided level and output.
def self.create_simple_logger(level = :FATAL, output = $stderr)
level = Concern::Logging.const_get(level) unless level.is_a?(Integer)

# TODO (pitr-ch 24-Dec-2016): figure out why it had to be replaced, stdlogger was deadlocking
lambda do |severity, progname, message = nil, &block|
return false if severity < level
Expand All @@ -52,21 +55,23 @@ def self.create_simple_logger(level = Logger::FATAL, output = $stderr)

output.print format "[%s] %5s -- %s: %s\n",
Time.now.strftime('%Y-%m-%d %H:%M:%S.%L'),
Logger::SEV_LABEL[severity],
Concern::Logging::SEV_LABEL[severity],
progname,
formatted_message
true
end
end

# Use logger created by #create_simple_logger to log concurrent-ruby messages.
def self.use_simple_logger(level = Logger::FATAL, output = $stderr)
def self.use_simple_logger(level = :FATAL, output = $stderr)
Concurrent.global_logger = create_simple_logger level, output
end

# @return [Logger] Logger with provided level and output.
# Create a stdlib logger with provided level and output.
# If you use this deprecated method you might need to add logger to your Gemfile to avoid warnings from Ruby 3.3.5+.
# @deprecated
def self.create_stdlib_logger(level = Logger::FATAL, output = $stderr)
def self.create_stdlib_logger(level = :FATAL, output = $stderr)
require 'logger'
logger = Logger.new(output)
logger.level = level
logger.formatter = lambda do |severity, datetime, progname, msg|
Expand All @@ -93,7 +98,7 @@ def self.create_stdlib_logger(level = Logger::FATAL, output = $stderr)

# Use logger created by #create_stdlib_logger to log concurrent-ruby messages.
# @deprecated
def self.use_stdlib_logger(level = Logger::FATAL, output = $stderr)
def self.use_stdlib_logger(level = :FATAL, output = $stderr)
Concurrent.global_logger = create_stdlib_logger level, output
end

Expand All @@ -103,7 +108,7 @@ def self.use_stdlib_logger(level = Logger::FATAL, output = $stderr)
NULL_LOGGER = lambda { |level, progname, message = nil, &block| }

# @!visibility private
GLOBAL_LOGGER = AtomicReference.new(create_simple_logger(Logger::WARN))
GLOBAL_LOGGER = AtomicReference.new(create_simple_logger(:WARN))
private_constant :GLOBAL_LOGGER

def self.global_logger
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def requires=(paths)
config.before :all do
# Only configure logging if it has been required, to make sure the necessary require's are in place
if Concurrent.respond_to? :use_simple_logger
Concurrent.use_simple_logger Logger::FATAL
Concurrent.use_simple_logger :FATAL
end
end

Expand Down

0 comments on commit d7ce956

Please sign in to comment.