Sha256: fdafbc402e5a115713a44e63ec6fbaaac21f1f18c06db0dea8ac427fc945b7b4

Contents?: true

Size: 1.3 KB

Versions: 25

Compression:

Stored size: 1.3 KB

Contents

module Vedeu

  module Logging

    # Ensures we can always write to the log file by creating a
    # lock-less log device.
    #
    class LocklessLogDevice < ::Logger::LogDevice

      # Returns a new instance of Vedeu::Logging::LocklessLogDevice.
      #
      # @param log [void]
      # @return [Vedeu::Logging::LocklessLogDevice]
      def initialize(log = nil)
        @dev      = nil
        @filename = nil

        if log.respond_to?(:write) && log.respond_to?(:close)
          @dev = log

        else
          @dev      = open_logfile(log)
          @dev.sync = true
          @filename = log

        end
      end

      # @param message [String]
      # @return [void]
      def write(message)
        @dev.write(message)

      rescue StandardError => exception
        warn("log writing failed. #{exception}".freeze)
      end

      # @return [void]
      def close
        @dev.close
      rescue
        nil
      end

      private

      # @param log [String]
      # @return [void]
      def open_logfile(log)
        if FileTest.exist?(log)
          open(log, (File::WRONLY | File::APPEND))

        else
          logdev = open(log, (File::WRONLY | File::APPEND | File::CREAT))
          logdev.sync = true
          logdev

        end
      end

    end # LocklessLogDevice

  end # Logging

end # Vedeu

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
vedeu-0.6.50 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.49 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.48 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.47 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.46 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.45 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.44 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.43 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.42 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.41 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.40 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.39 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.38 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.37 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.36 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.35 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.34 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.33 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.32 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.6.31 lib/vedeu/logging/lockless_log_device.rb