class Fluent::Plugin::MultiOutput

def after_shutdown

def after_shutdown
  super
  call_lifecycle_method(:after_shutdown, :after_shutdown?)
end

def after_start

def after_start
  super
  call_lifecycle_method(:after_start, :after_started?)
end

def before_shutdown

def before_shutdown
  super
  call_lifecycle_method(:before_shutdown, :before_shutdown?)
end

def call_lifecycle_method(method_name, checker_name)

TODO: this hack will be removed at v2.
sub-plugins. So child plugins' lifecycles MUST be controlled by MultiOutput plugin itself.
But when MultiOutput plugins are created dynamically (by forest plugin or others), agent cannot find
def call_lifecycle_method(method_name, checker_name)
  return if @outputs_statically_created
  @outputs.each do |o|
    begin
      log.debug "calling #{method_name} on output plugin dynamically created", type: Fluent::Plugin.lookup_type_from_class(o.class), plugin_id: o.plugin_id
      o.send(method_name) unless o.send(checker_name)
    rescue Exception => e
      log.warn "unexpected error while calling #{method_name} on output plugin dynamically created", plugin: o.class, plugin_id: o.plugin_id, error: e
      log.warn_backtrace
    end
  end
end

def close

def close
  super
  call_lifecycle_method(:close, :closed?)
end

def configure(conf)

def configure(conf)
  super
  @stores.each do |store|
    store_conf = store.corresponding_config_element
    type = store_conf['@type']
    unless type
      raise Fluent::ConfigError, "Missing '@type' parameter in <store> section"
    end
    log.debug "adding store", type: type
    output = Fluent::Plugin.new_output(type)
    output.context_router = self.context_router
    output.configure(store_conf)
    @outputs << output
  end
end

def emit_sync(tag, es)

def emit_sync(tag, es)
  @counters_monitor.synchronize{ @emit_count += 1 }
  begin
    process(tag, es)
    @counters_monitor.synchronize{ @emit_records += es.size }
  rescue
    @counters_monitor.synchronize{ @num_errors += 1 }
    raise
  end
end

def initialize

def initialize
  super
  @outputs = []
  @outputs_statically_created = false
  @counters_monitor = Monitor.new
  # TODO: well organized counters
  @num_errors = 0
  @emit_count = 0
  @emit_records = 0
  # @write_count = 0
  # @rollback_count = 0
end

def multi_output?

def multi_output?
  true
end

def process(tag, es)

def process(tag, es)
  raise NotImplementedError, "BUG: output plugins MUST implement this method"
end

def shutdown

def shutdown
  super
  call_lifecycle_method(:shutdown, :shutdown?)
end

def start

def start
  super
  call_lifecycle_method(:start, :started?)
end

def static_outputs

def static_outputs
  @outputs_statically_created = true
  @outputs
end

def stop

def stop
  super
  call_lifecycle_method(:stop, :stopped?)
end

def terminate

def terminate
  super
  call_lifecycle_method(:terminate, :terminated?)
end