Sha256: c44b69822d63750564b0e059892859ba7ea3b6481889d65be85dc0a901f68c62

Contents?: true

Size: 1.83 KB

Versions: 42

Compression:

Stored size: 1.83 KB

Contents

require 'thread'

module Vagrant
  module Util
    # Represents a logger for a specific resource within Vagrant. Each
    # logger should be initialized and set to represent a single
    # resource. Each logged message will then appear in the following
    # format:
    #
    #     [resource] message
    #
    # This class is thread safe. The backing class which actually does
    # all the logging IO is protected.
    class ResourceLogger
      @@singleton_logger = nil
      @@writer_lock = Mutex.new

      # The resource which this logger represents.
      attr_reader :resource

      # The environment that this logger is part of
      attr_reader :env

      # The backing logger which actually handles the IO. This logger
      # should be a subclass of the standard library Logger, in general.
      # IMPORTANT: This logger must be thread-safe.
      attr_reader :logger

      class << self
        # Returns a singleton logger. If one has not yet be
        # instantiated, then the given environment will be used to
        # create a new logger.
        def singleton_logger(env=nil)
          return PlainLogger.new(nil) if !env.loaded?

          @@singleton_logger ||= begin
            file = env.log_path.join("#{Time.now.to_i}.log")
            PlainLogger.new(file)
          end
        end

        # Resets the singleton logger (only used for testing).
        def reset_singleton_logger!
          @@singleton_logger = nil
        end
      end

      def initialize(resource, env)
        @resource = resource
        @env = env
        @logger = self.class.singleton_logger(env)
      end

      [:debug, :info, :error, :fatal].each do |method|
        define_method(method) do |message|
          @@writer_lock.synchronize do
            logger.send(method, "[#{resource}] #{message}")
          end
        end
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 4 rubygems

Version Path
vagrantup-0.7.8 lib/vagrant/util/resource_logger.rb
vagrantup-0.7.7 lib/vagrant/util/resource_logger.rb
vagrantup-0.7.6 lib/vagrant/util/resource_logger.rb
vagrantup-0.7.5 lib/vagrant/util/resource_logger.rb
vagrantup-0.7.4 lib/vagrant/util/resource_logger.rb
vagrantup-0.7.3 lib/vagrant/util/resource_logger.rb
vagrantup-0.7.2 lib/vagrant/util/resource_logger.rb
vagrantup-0.7.1 lib/vagrant/util/resource_logger.rb
vagrantup-0.7.0 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.9 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.8 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.7 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.6 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.5 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.4 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.3 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.2 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.1 lib/vagrant/util/resource_logger.rb
vagrantup-0.6.0 lib/vagrant/util/resource_logger.rb
vagrant-0.7.8 lib/vagrant/util/resource_logger.rb