Sha256: 15caec408a92468a09806a8a0ce869d4ae1f3a172c48e76184756d7a72127fbc

Contents?: true

Size: 1.96 KB

Versions: 25

Compression:

Stored size: 1.96 KB

Contents

#
# Copyright 2011 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by loglicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module Backstage
  class Log
    include Resource

    attr_reader :file_path

    LOG_GLOB = "*log"
    
    class << self
      def to_hash_attributes
        super + [:name, :file_path, :size]
      end

      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

      alias_method :find, :new
    end

    def initialize(file_path)
      @file_path = file_path
    end

    alias_method :full_name, :file_path

    def name
      File.basename( file_path )
    end

    def size
      File.size( file_path )
    end
    
    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

  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
torquebox-backstage-1.1.2 lib/logs/models/log.rb
torquebox-backstage-1.1.1 lib/logs/models/log.rb
torquebox-backstage-1.1.0 lib/logs/models/log.rb
torquebox-backstage-1.0.7 lib/logs/models/log.rb
torquebox-backstage-1.0.6 lib/logs/models/log.rb
torquebox-backstage-1.0.5 lib/logs/models/log.rb
torquebox-backstage-1.0.4 lib/logs/models/log.rb
torquebox-backstage-1.0.3 lib/logs/models/log.rb
torquebox-backstage-1.0.2 lib/logs/models/log.rb
torquebox-backstage-1.0.1 lib/logs/models/log.rb
torquebox-backstage-1.0.0 lib/logs/models/log.rb
torquebox-backstage-1.0.0.beta2 lib/logs/models/log.rb
torquebox-backstage-1.0.0.beta1 lib/logs/models/log.rb
torquebox-backstage-0.5.4 lib/logs/models/log.rb
torquebox-backstage-0.5.3 lib/logs/models/log.rb
torquebox-backstage-0.5.2 lib/logs/models/log.rb
torquebox-backstage-0.5.1 lib/logs/models/log.rb
torquebox-backstage-0.5 lib/logs/models/log.rb
torquebox-backstage-0.4.3 lib/logs/models/log.rb
torquebox-backstage-0.4.2 lib/logs/models/log.rb