Skip to content

Commit

Permalink
Merge 4594153 into f5af4f5
Browse files Browse the repository at this point in the history
  • Loading branch information
emesepadanyi committed Jun 19, 2020
2 parents f5af4f5 + 4594153 commit 9320a0a
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 53 deletions.
1 change: 1 addition & 0 deletions REFACTOR.md
@@ -0,0 +1 @@
20200613113000: Remove $CELLULOID_DEBUG global variable. Fallback to [Logger](https://ruby-doc.org/stdlib-2.2.0/libdoc/logger/rdoc/Logger.html) mechanisms. [Issue #742](https://github.com/celluloid/celluloid/issues/742)
Binary file modified documentation/ClassDiagram-class_diagram.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion documentation/ClassDiagram-class_diagram.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion documentation/ClassDiagram.drawio

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions lib/celluloid.rb
Expand Up @@ -7,7 +7,6 @@

# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
$CELLULOID_DEBUG = false
$CELLULOID_MONITORING = false
# rubocop:enable Style/GlobalVars

Expand Down Expand Up @@ -464,10 +463,7 @@ def future(meth = nil, *args, &block)
require "celluloid/exceptions"

Celluloid.logger = Logger.new(STDERR).tap do |logger|
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
logger.level = Logger::INFO unless $CELLULOID_DEBUG
# rubocop:enable Style/GlobalVars
logger.level = Logger::INFO
end

Celluloid.shutdown_timeout = 10
Expand Down
7 changes: 3 additions & 4 deletions lib/celluloid/actor.rb
Expand Up @@ -286,10 +286,9 @@ def sleep(interval)

# Handle standard low-priority messages
def handle_message(message)
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Metrics/LineLength, Style/GlobalVars
Internals::Logger.debug "Discarded message (unhandled): #{message}" if !@handlers.handle_message(message) && !@receivers.handle_message(message) && $CELLULOID_DEBUG
# rubocop:enable Metrics/LineLength, Style/GlobalVars
if !@handlers.handle_message(message) && !@receivers.handle_message(message)
Internals::Logger.debug "Discarded message (unhandled): #{message}"
end

message
end
Expand Down
4 changes: 0 additions & 4 deletions lib/celluloid/debug.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/celluloid/group.rb
Expand Up @@ -16,7 +16,7 @@ def assert_active
def assert_inactive
return unless active?
if RUBY_PLATFORM == "java"
Celluloid.logger.warn "Group is still active"
Internals::Logger.warn "Group is still active"
else
raise Celluloid::StillActive
end
Expand Down
21 changes: 7 additions & 14 deletions lib/celluloid/internals/logger.rb
Expand Up @@ -7,10 +7,7 @@ def initialize(backtrace)
end

def debug(string)
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
Celluloid.logger.debug(decorate(string)) if $CELLULOID_DEBUG
# rubocop:enable Style/GlobalVars
Celluloid.logger.debug(decorate(string))
end

def info(string)
Expand Down Expand Up @@ -40,10 +37,7 @@ def with_backtrace(backtrace)

# Send a debug message
def debug(string)
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
Celluloid.logger.debug(string) if Celluloid.logger && $CELLULOID_DEBUG
# rubocop:enable Style/GlobalVars
Celluloid.logger.debug(string) if Celluloid.logger
end

# Send a info message
Expand Down Expand Up @@ -77,12 +71,6 @@ def crash(string, exception)
end
end

# Note a deprecation
def deprecate(message)
trace = caller.join("\n\t")
warn "DEPRECATION WARNING: #{message}\n\t#{trace}"
end

# Define an exception handler
# NOTE: These should be defined at application start time
def exception_handler(&block)
Expand All @@ -99,6 +87,11 @@ def format_exception(exception)
"EMPTY BACKTRACE\n\t"
end
end

# return level of logging
def level
return Celluloid.logger.level if Celluloid.logger
end
end
end
end
5 changes: 1 addition & 4 deletions lib/celluloid/mailbox.rb
Expand Up @@ -157,10 +157,7 @@ def next_message
end

def dead_letter(message)
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
Internals::Logger.debug "Discarded message (mailbox is dead): #{message}" if $CELLULOID_DEBUG
# rubocop:enable Style/GlobalVars
Internals::Logger.debug "Discarded message (mailbox is dead): #{message}"
end

def mailbox_full
Expand Down
2 changes: 0 additions & 2 deletions lib/celluloid/rspec.rb
Expand Up @@ -39,8 +39,6 @@ module Specs

# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
$CELLULOID_DEBUG = true

# Require but disable, so it has to be explicitly enabled in tests
require "celluloid/probe"
$CELLULOID_MONITORING = false
Expand Down
5 changes: 1 addition & 4 deletions lib/celluloid/system_events.rb
Expand Up @@ -5,10 +5,7 @@ def handle_system_event(event)
if handler = SystemEvent.handle(event.class)
send(handler, event)
else
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
Internals::Logger.debug "Discarded message (unhandled): #{message}" if $CELLULOID_DEBUG
# rubocop:enable Style/GlobalVars
Internals::Logger.debug "Discarded message (unhandled): #{message}"
end
end
end
Expand Down
17 changes: 4 additions & 13 deletions lib/celluloid/task.rb
Expand Up @@ -63,14 +63,11 @@ def suspend(status)

@status = status

# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
if $CELLULOID_DEBUG && @dangerous_suspend
if Internals::Logger.level == Logger::DEBUG && @dangerous_suspend
Internals::Logger.with_backtrace(caller[2...8]) do |logger|
logger.warn "Dangerously suspending task: type=#{@type.inspect}, meta=#{@meta.inspect}, status=#{@status.inspect}"
end
end
# rubocop:enable Style/GlobalVars

value = signal

Expand Down Expand Up @@ -111,15 +108,12 @@ def terminate
raise "Cannot terminate an exclusive task" if exclusive?

if running?
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
if $CELLULOID_DEBUG
if Internals::Logger.level == Logger::DEBUG
Internals::Logger.with_backtrace(backtrace) do |logger|
type = @dangerous_suspend ? :warn : :debug
logger.send(type, "Terminating task: type=#{@type.inspect}, meta=#{@meta.inspect}, status=#{@status.inspect}")
end
end
# rubocop:enable Style/GlobalVars

exception = TaskTerminated.new("task was terminated")
exception.set_backtrace(caller)
Expand Down Expand Up @@ -147,14 +141,11 @@ def inspect
end

def guard(message)
# !!! DO NOT INTRODUCE ADDITIONAL GLOBAL VARIABLES !!!
# rubocop:disable Style/GlobalVars
if @guard_warnings
Internals::Logger.warn message if $CELLULOID_DEBUG
Internals::Logger.warn message if Internals::Logger.level == Logger::DEBUG
else
raise message if $CELLULOID_DEBUG
raise message if Internals::Logger.level == Logger::DEBUG
end
# rubocop:enable Style/GlobalVars
end

private
Expand Down
90 changes: 90 additions & 0 deletions spec/celluloid/internals/logger_spec.rb
@@ -0,0 +1,90 @@
RSpec.describe Celluloid::Internals::Logger do
before do
Celluloid.logger = Logger.new(STDERR)
end

describe "#debug" do
subject { -> { described_class.debug("debug message") }}
context "when logger is nil" do
before do
Celluloid.logger = nil
end
it { is_expected.not_to raise_error }
end

context "when logger is a Logger object" do
it do
expect(Celluloid.logger).to receive(:debug).once
subject.call
end

context "logging level is DEBUG" do
before do
Celluloid.logger.level = Logger::DEBUG
end
it { is_expected.to output.to_stderr_from_any_process }
end

context "logging level is INFO" do
before do
Celluloid.logger.level = Logger::INFO
end
it { is_expected.not_to output.to_stderr_from_any_process }
end
end
end

describe "#info" do
subject { described_class.info("info message") }
context "when logger is nil" do
before do
Celluloid.logger = nil
expect { subject }.not_to raise_error
end
it { subject }
end

context "when logger is a Logger object" do
before do
expect(Celluloid.logger).to receive(:info).once
end
it { subject }
end
end

describe "#warn" do
subject { described_class.warn("warn message") }
context "when logger is nil" do
before do
Celluloid.logger = nil
expect { subject }.not_to raise_error
end
it { subject }
end

context "when logger is a Logger object" do
before do
expect(Celluloid.logger).to receive(:warn).once
end
it { subject }
end
end

describe "#error" do
subject { described_class.error("error message") }
context "when logger is nil" do
before do
Celluloid.logger = nil
expect { subject }.not_to raise_error
end
it { subject }
end

context "when logger is a Logger object" do
before do
expect(Celluloid.logger).to receive(:error).once
end
it { subject }
end
end
end
3 changes: 3 additions & 0 deletions spec/shared/actor_examples.rb
Expand Up @@ -207,6 +207,7 @@ def initialize
end

it "warns about suspending the initialize" do
allow(logger).to receive(:level).and_return(Logger::DEBUG)
# rubocop:disable Metrics/LineLength
expect(logger).to receive(:warn).with(/Dangerously suspending task: type=:call, meta={:dangerous_suspend=>true, :method_name=>:initialize}, status=:sleeping/)
# rubocop:enable Metrics/LineLength
Expand Down Expand Up @@ -241,6 +242,7 @@ def cleanup
allow(logger).to receive(:warn)
allow(logger).to receive(:crash).with(/finalizer crashed!/, Celluloid::TaskTerminated)
# rubocop:disable Metrics/LineLength
allow(logger).to receive(:level).and_return(Logger::DEBUG)
expect(logger).to receive(:warn).with(/Dangerously suspending task: type=:finalizer, meta={:dangerous_suspend=>true, :method_name=>:cleanup}, status=:sleeping/)
# rubocop:enable Metrics/LineLength
actor.terminate
Expand Down Expand Up @@ -559,6 +561,7 @@ def name_inside_task

context "when terminated" do
it "logs a debug" do
allow(logger).to receive(:level).and_return(Logger::DEBUG)
# rubocop:disable Metrics/LineLength
expect(logger).to receive(:debug).with(/^Terminating task: type=:call, meta={:dangerous_suspend=>false, :method_name=>:sleepy}, status=:sleeping/)
# rubocop:enable Metrics/LineLength
Expand Down
10 changes: 10 additions & 0 deletions spec/support/crash_checking.rb
Expand Up @@ -33,6 +33,16 @@ def warn(*args)
@real_logger.warn(*args)
end

def error(*args)
check
@real_logger.error(*args)
end

def level
check
@real_logger.level
end

def with_backtrace(_backtrace)
check
yield self
Expand Down

0 comments on commit 9320a0a

Please sign in to comment.