Class: ScribeIt

Inherits:
Object
  • Object
show all
Defined in:
lib/scribeit.rb,
lib/scribeit/log.rb,
lib/scribeit/source.rb

Defined Under Namespace

Classes: Log, Source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ScribeIt

Returns a new instance of ScribeIt.



12
13
14
15
16
17
# File 'lib/scribeit.rb', line 12

def initialize(config)
  @config = config
  @sources = []
  @scribe = Scribe.new
  @logger = ScribeIt::Log.new STDERR
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/scribeit.rb', line 9

def config
  @config
end

#sourcesObject (readonly)

Returns the value of attribute sources.



10
11
12
# File 'lib/scribeit.rb', line 10

def sources
  @sources
end

Instance Method Details

#registerObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/scribeit.rb', line 19

def register
  if !@config.has_key? "sources"
    raise "Config error - no sources defined"
  end

  sources = @config["sources"]
  sources.each do |source|
    puts source.inspect
    if source.is_a? Array or source.is_a? Hash
      cat, files = source
    else
      raise "Config error - no category for file: #{files.inspect}"
    end

    files = [files] if !files.is_a? Array

    files.each do |f|
      @logger.debug("Adding new source #{f} to category #{cat}")
      s = ScribeIt::Source.new(f, cat) { |event| fire(event) }
      s.register
      @sources << s
    end
  end

  register_signal_handler
end

#register_signal_handlerObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scribeit.rb', line 58

def register_signal_handler
  @signals = EventMachine::Channel.new

  Signal.trap("INT") do
    @signals.push(:INT)
  end

  Signal.trap("USR1") do
    @signals.push(:USR1)
  end

  @signals.subscribe do |msg|
    case msg
    when :USR1
      #eventually, we want to reload our config.
      # for now, we'll just show what we're looking at.
      puts @sources
    when :INT
      EventMachine.stop_event_loop
    end
  end
end

#run(&block) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/scribeit.rb', line 46

def run(&block)
  EventMachine.epoll
  EventMachine.run do
    self.register
    yield if block_given?
  end
end

#stopObject



54
55
56
# File 'lib/scribeit.rb', line 54

def stop
  EventMachine.stop_event_loop
end