Skip to content

Commit dbd1144

Browse files
committed
Add configurable logging
1 parent ae4331a commit dbd1144

File tree

7 files changed

+42
-16
lines changed

7 files changed

+42
-16
lines changed

lib/concurrent/actress.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'concurrent/configuration'
44
require 'concurrent/executor/one_by_one'
55
require 'concurrent/ivar'
6+
require 'concurrent/logging'
67

78
module Concurrent
89

lib/concurrent/actress/core.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Actress
77
# @api private
88
class Core
99
include TypeCheck
10+
include Concurrent::Logging
1011

1112
attr_reader :reference, :name, :path, :executor
1213

@@ -15,6 +16,8 @@ class Core
1516
# @option opts [Context] actress_class a class to be instantiated defining Actor's behaviour
1617
# @option opts [Array<Object>] args arguments for actress_class instantiation
1718
# @option opts [Executor] executor, default is `Concurrent.configuration.global_task_pool`
19+
# @option opts [Proc, nil] logger a proc accepting (level, progname, message = nil, &block) params,
20+
# can be used to hook actor instance to any logging system
1821
# @param [Proc] block for class instantiation
1922
def initialize(opts = {}, &block)
2023
@mailbox = Array.new
@@ -32,11 +35,8 @@ def initialize(opts = {}, &block)
3235
raise 'only root has no parent'
3336
end
3437

35-
@path = @parent_core ? File.join(@parent_core.path, @name) : @name
36-
# TODO add proper logging
37-
@logger = Logger.new($stderr)
38-
@logger.level = opts.fetch(:logger_level, 1)
39-
@logger.progname = @path
38+
@path = @parent_core ? File.join(@parent_core.path, @name) : @name
39+
@logger = opts[:logger]
4040

4141
@parent_core.add_child reference if @parent_core
4242

@@ -48,7 +48,7 @@ def initialize(opts = {}, &block)
4848
@actress = actress_class.new *args, &block
4949
@actress.send :initialize_core, self
5050
rescue => ex
51-
@logger.error ex
51+
log ERROR, ex
5252
terminate!
5353
end
5454
end
@@ -111,7 +111,7 @@ def terminate!
111111
@parent_core.remove_child reference if @parent_core
112112
@mailbox.each do |envelope|
113113
reject_envelope envelope
114-
@logger.debug "rejected #{envelope.message} from #{envelope.sender_path}"
114+
log DEBUG, "rejected #{envelope.message} from #{envelope.sender_path}"
115115
end
116116
@mailbox.clear
117117
# TODO terminate all children
@@ -146,15 +146,15 @@ def receive_envelope
146146

147147
if terminated?
148148
reject_envelope envelope
149-
@logger.fatal "this should not be happening #{caller[0]}"
149+
log FATAL, "this should not be happening #{caller[0]}"
150150
end
151151

152-
@logger.debug "received #{envelope.message} from #{envelope.sender_path}"
152+
log DEBUG, "received #{envelope.message} from #{envelope.sender_path}"
153153

154154
result = @actress.on_envelope envelope
155155
envelope.ivar.set result unless envelope.ivar.nil?
156156
rescue => error
157-
@logger.error error
157+
log ERROR, error
158158
envelope.ivar.fail error unless envelope.ivar.nil?
159159
terminate!
160160
ensure
@@ -170,7 +170,7 @@ def schedule_execution
170170
Thread.current[:__current_actress__] = reference
171171
yield
172172
rescue => e
173-
@logger.fatal e
173+
log FATAL, e
174174
ensure
175175
Thread.current[:__current_actress__] = nil
176176
end
@@ -181,6 +181,10 @@ def schedule_execution
181181
def reject_envelope(envelope)
182182
envelope.reject! ActressTerminated.new(reference)
183183
end
184+
185+
def log(level, message = nil, &block)
186+
super level, @path, message, &block
187+
end
184188
end
185189
end
186190
end

lib/concurrent/agent.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ def work(&handler) # :nodoc:
200200
validator, value = mutex.synchronize { [@validator, @value] }
201201

202202
begin
203-
# FIXME creates second thread
204203
result, valid = Concurrent::timeout(@timeout) do
205204
result = handler.call(value)
206205
[result, validator.call(result)]

lib/concurrent/configuration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ module Concurrent
1010
# A gem-level configuration object.
1111
class Configuration
1212

13+
attr_accessor :logger
14+
1315
# Create a new configuration object.
1416
def initialize
1517
@global_task_pool = Delay.new { new_task_pool }
1618
@global_operation_pool = Delay.new { new_operation_pool }
1719
@global_timer_set = Delay.new { Concurrent::TimerSet.new }
20+
@logger = no_logger
21+
end
22+
23+
def no_logger
24+
-> (level, progname, message = nil, &block) {}
1825
end
1926

2027
# Global thread pool optimized for short *tasks*.

lib/concurrent/logging.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'logger'
2+
3+
module Concurrent
4+
module Logging
5+
include Logger::Severity
6+
7+
def log(level, progname, message = nil, &block)
8+
(@logger || Concurrent.configuration.logger).call level, progname, message, &block
9+
end
10+
end
11+
end

spec/concurrent/actress_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ def assert condition
104104
end
105105

106106
it 'terminates on failed initialization' do
107-
a = AdHoc.spawn(name: :fail, logger_level: 4) { raise }
107+
a = AdHoc.spawn(name: :fail, logger: Concurrent.configuration.no_logger) { raise }
108108
a.ask(nil).wait.rejected?.should be_true
109109
a.terminated?.should be_true
110110
end
111111

112112
it 'terminates on failed message processing' do
113-
a = AdHoc.spawn(name: :fail, logger_level: 4) { -> _ { raise } }
113+
a = AdHoc.spawn(name: :fail, logger: Concurrent.configuration.no_logger) { -> _ { raise } }
114114
a.ask(nil).wait.rejected?.should be_true
115115
a.terminated?.should be_true
116116
end

spec/spec_helper.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
require 'coveralls'
33

44
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5-
SimpleCov::Formatter::HTMLFormatter,
6-
Coveralls::SimpleCov::Formatter
5+
SimpleCov::Formatter::HTMLFormatter,
6+
Coveralls::SimpleCov::Formatter
77
]
88

99
SimpleCov.start do
@@ -17,6 +17,10 @@
1717

1818
require 'concurrent'
1919

20+
logger = Logger.new($stderr)
21+
logger.level = Logger::INFO
22+
Concurrent.configuration.logger = -> (level, progname, message = nil, &block) { logger.add level, message, progname, &block }
23+
2024
# import all the support files
2125
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require File.expand_path(f) }
2226

0 commit comments

Comments
 (0)