Class: Backstage::Log

Inherits:
Object show all
Includes:
Resource
Defined in:
lib/logs/models/log.rb

Constant Summary collapse

LOG_GLOB =
"*log"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

#association_chain, #available_actions, included, #resource, #to_hash

Constructor Details

#initialize(file_path) ⇒ Log

Returns a new instance of Log.



40
41
42
# File 'lib/logs/models/log.rb', line 40

def initialize(file_path)
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject (readonly) Also known as: full_name

Returns the value of attribute file_path.



21
22
23
# File 'lib/logs/models/log.rb', line 21

def file_path
  @file_path
end

Class Method Details

.all(log_dir = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/logs/models/log.rb', line 30

def all(log_dir = nil)
  log_dir ||= Backstage.jboss_log_dir
  Dir.glob("#{log_dir}/#{LOG_GLOB}").collect do |path|
    Log.new( File.expand_path( path ) )
  end.sort_by(&:name)
end

.to_hash_attributesObject



26
27
28
# File 'lib/logs/models/log.rb', line 26

def to_hash_attributes
  super + [:name, :file_path, :size]
end

Instance Method Details

#nameObject



46
47
48
# File 'lib/logs/models/log.rb', line 46

def name
  File.basename( file_path )
end

#sizeObject



50
51
52
# File 'lib/logs/models/log.rb', line 50

def size
  File.size( file_path )
end

#tail(options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/logs/models/log.rb', line 54

def tail(options)
  offset = (options['offset'] || -1000).to_i
  File.open(@file_path, 'r') do |file|
    # Set offset to the beginning or end of the file if
    # the value passed in is less than or greather than
    # the min/max allowed
    if offset.abs > file.stat.size
      offset = offset < 0 ? 0 : file.stat.size
    end

    if offset < 0
      file.seek(offset, IO::SEEK_END)
      file.readline # Advance to the next entire line
    else
      file.seek(offset)
    end
    lines = []
    until file.eof? do
      lines << file.readline
    end
    offset = file.pos
    { :offset => offset, :lines => lines.compact }
  end
end