-
Notifications
You must be signed in to change notification settings - Fork 1
/
logs.rb
86 lines (67 loc) · 1.81 KB
/
logs.rb
1
2
3
4
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
require 'rubygems'
require 'fileutils'
require 'yaml'
class LogsError < StandardError; end
class Logs
attr_reader :path, :pidfile, :start_stop
def initialize(options={})
raise ArgumentError, "must supply a path (:path => '/path/to/logs')" unless options.has_key?(:path)
@path = options[:path] || nil
@name = options[:name] || nil
@pidfile = options[:pidfile] || nil
@start_stop = options[:start_stop] || nil
end
def name
if @name
@name
else
File.basename( @path )
end
end
def path_update
file_update( @path )
end
def pidfile_update
file_update( @pidfile )
end
def pidfile_exists?
@pidfile ? File.exists?( @pidfile ) : false
end
def get_some_log(lines=25)
result = `tail -n #{lines} #{@path}`
result.split(/\n/)
end
# add_logs support file globs
def self.add_logs(options={})
logs = []
files = Dir.glob( options[:path] )
if files.nil? || files.empty?
raise LogsError.new("the path '#{options[:path]}' resulted in no files being found")
else
if files.length == 1
logs << Logs.new( {:path => files.first}.merge(options) )
else
files.each{ |file| logs += Logs.add_logs( :path => file ) }
end
end
logs
end
def self.load_config_file
host_file = File.join(File.dirname(__FILE__), '..', 'config', 'logs.yml')
if File.exists?( host_file )
File.open host_file do |yf|
YAML.each_document( yf ) do |ydoc|
return ydoc.map{ |rec| rec[1] }
end
end
else
raise LogsError.new("the file 'config/logs.yml' does not exist, please copy logs.example.yml and edit")
end
end
private
def file_update(path)
if File.exists?(path )
return File.mtime( path )
end
end
end