Sha256: 6a114adefb57a34f5e77df8f87ff5a383e0402d93cbc0387fda3aa9b388e1844

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'owskilog/version'
require 'owskilog/watcher'
require 'owskilog/event'

module OwskiLog
  class Holder
    attr_accessor :events, :watchers

    def initialize
      @events = Hash.new
      @watchers = Array.new
    end

    def register_event(key, description, parent_key: nil)
      ev = Event.new(key, description)
      EventManager::register_event(@events, ev, parent_key: parent_key)
      WatcherManager::trigger_watchers(@watchers, triggers: [:after_register, :all])
      ev
    end

    def finish_event(key, status, cause: nil)
      EventManager::finish_event(@events, key, status, cause: cause)
      WatcherManager::trigger_watchers(@watchers, triggers: [:after_finish, :all])
    end

    def register_watcher(watcher)
      WatcherManager::register_watcher(@watchers, watcher)
    end

    def wrap(key, description, parent_key: nil, &block)
      ev = register_event(key, description, parent_key: parent_key)
      block.call(key)
      unless ev.is_finished?
        finish_event(key, OwskiLog::Status::OK)
      end
    rescue Exception => e
      finish_event(key, OwskiLog::Status::ERROR, cause: e.inspect)
    end

    def get_log_structure
      @result = []

      @events.each do |_, value|
        if value.parent != nil
          value.parent.sub_events << value
        else
          @result << value
        end
      end

      @result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
owskilog-0.2.2 lib/owskilog.rb