Class: ColorTail::Application

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

Class Method Summary collapse

Class Method Details

.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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 opt[:invalid_argument]
       $stderr.puts opt[:invalid_argument]
       options[:help] = true
   end
    
   # Show the help menu if desired
   if options[:help]
       $stderr.puts opt.opts
       return 1
   end

   
   # The meat of the program
   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.new
           @match_group.push( 'default' => [] )
       end

       # Display the list of available groups and exit
       if options[:list]
           puts "The following match groups are available through your config files:"
           if config
               config.display_match_groups()
           else
               puts "  * default"
           end
           return 1
       end
    
       # Before we go any further, check for existance of files
       @files_exist = false
       files.each do |file|
           if File.exists?(file)
               @files_exist = true
               break
           end
       end
    
       # If we have no files, tell them and show the help
       unless @files_exist
           $stderr.puts "Please check to make sure the files exist ..."
           $stderr.puts opt.opts
           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
       thread_cleanup(threads)
   end

   return 0
end

.thread_cleanup(threads) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/colortail/application.rb', line 102

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