Class: ColorTail::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/colortail/application.rb

Class Method Summary collapse

Class Method Details

.cleanup(threads) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/colortail/application.rb', line 77

def cleanup(threads)
    threads.each do |thread|
        thread.kill
    end
    $stderr.puts "Terminating..."
    exit
end

.run!(*arguments) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/colortail/application.rb', line 5

def run!(*arguments)
   opt = ColorTail::Options.new(arguments) 
   files = opt[:files]
   options = opt[:options]
    
   # Deal with any/all options issues
   if options[:invalid_argument]
       $stderr.puts options[:invalid_argument]
       options[:help] = true
   end
    
   if options[:help]
       $stderr.puts opt.opts
       return 1
   end
    
   begin
       # Read the config file if it exists
       if File.exists?(options[:conf])
           config = ColorTail::Configuration.new(options[:conf])
           @match_group = config.load_opts(options[:group])
       else
           # Create this to ensure we always have a value for this array
           @match_group = Array.push( 'default' => [] )
       end

       # Display the list of available groups and exit
       if options[:list]
           config.display_match_groups()
           return 1
       end
    
       logger = ColorTail::Colorize.new()

       # Add the color match array if we aren't using the default
       if @match_group.class == Array
           @match_group.each do |matcher|
               logger.add_color_matcher( matcher )
           end
       else
           logger.add_color_matcher( @match_group )
       end
    
       # Create a thread for each file
       threads = []
       files.each do |file|
           threads[files.index(file)] = Thread.new {
               tailer = ColorTail::TailFile.new( file )
    
               # First display the last 10 lines of each file in ARGV
               tailer.interval = 10
               tailer.backward( 10 )
    
               # Tail the file and show new lines
               tailer.tail { |line|  logger.log( file, line ) }
           }
           threads[files.index(file)].run
       end

       # Let the threads do their real work
       threads.each do |thread|
           thread.join
       end

   # If we get a CTRL-C, catch it (rescue) and send it for cleanup
   rescue Interrupt
       cleanup(threads)
   end

   return 0
end