|
|
@ -1,14 +1,14 @@ |
|
|
|
module Semantic |
|
|
|
# use the Zeitwerk autoloader to reattach_appender for development autoreloading feature |
|
|
|
class DevLoader |
|
|
|
def initialize |
|
|
|
@subscribers = [] |
|
|
|
def initialize(session_key) |
|
|
|
@session_key = session_key |
|
|
|
|
|
|
|
force_preload_module |
|
|
|
Rails.autoloaders.main.on_load('ApplicationController') do |
|
|
|
once_and_reload do |
|
|
|
append_ansi_formatter |
|
|
|
register_log_subscriber |
|
|
|
end |
|
|
|
append_ansi_formatter |
|
|
|
|
|
|
|
# FIXME: when final |
|
|
|
# Rails.logger.level = :error |
|
|
@ -16,10 +16,13 @@ module Semantic |
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
def once_and_reload(&) |
|
|
|
yield |
|
|
|
Rails.autoloaders.main.on_load('ApplicationController', &) |
|
|
|
end |
|
|
|
|
|
|
|
def force_preload_module |
|
|
|
self.class.module_parent.constants.each do |const| |
|
|
|
self.class.module_parent.const_get(const) |
|
|
|
end |
|
|
|
self.class.module_parent.constants.each { |const| self.class.module_parent.const_get(const) } |
|
|
|
end |
|
|
|
|
|
|
|
def append_ansi_formatter |
|
|
@ -31,16 +34,30 @@ module Semantic |
|
|
|
end |
|
|
|
|
|
|
|
def register_log_subscriber |
|
|
|
@subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) } |
|
|
|
@subscribers.clear |
|
|
|
reset_subscribers |
|
|
|
register_to_action_controller(:start_processing) |
|
|
|
register_to_action_controller(:process_action, :finish_processing) |
|
|
|
register_to_action_controller(:redirect_to) |
|
|
|
%i[send_file send_data halted_callback unpermitted_parameters send_stream write_fragment |
|
|
|
read_fragment expire_fragment exist_fragment?].each do |hook| |
|
|
|
register_to_action_controller(hook, :any_hook) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
log_subscriber = Semantic::LogSubscriber.new('toto1') |
|
|
|
@subscribers << ActiveSupport::Notifications.subscribe('start_processing.action_controller') do |event| |
|
|
|
log_subscriber.start_processing(event) |
|
|
|
def register_to_action_controller(hook, method = hook) |
|
|
|
@subscribers << ActiveSupport::Notifications.subscribe("#{hook}.action_controller") do |event| |
|
|
|
@log_subscriber.send(method, event) |
|
|
|
end |
|
|
|
@subscribers << ActiveSupport::Notifications.subscribe('process_action.action_controller') do |event| |
|
|
|
log_subscriber.finish_processing(event) |
|
|
|
end |
|
|
|
|
|
|
|
def reset_subscribers |
|
|
|
if defined?(@subscribers) |
|
|
|
@subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) } |
|
|
|
@subscribers.clear |
|
|
|
else |
|
|
|
@subscribers = [] |
|
|
|
end |
|
|
|
@log_subscriber = Semantic::LogSubscriber.new(@session_key) |
|
|
|
end |
|
|
|
end |
|
|
|
end |