Sha256: 5b4af370b9945d148ea2b195a1b151886629f9a4065f1222cd9a0894620018b2

Contents?: true

Size: 1.59 KB

Versions: 22

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Logging

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

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

        if writable? && closeable?
          @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}")
      end

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

      protected

      # @!attribute [r] log
      # @return [String]
      attr_reader :log

      private

      # @return [Boolean]
      def closable?
        log.respond_to?(:close)
      end

      # @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

      # @return [Boolean]
      def writable?
        log.respond_to?(:write)
      end

    end # LocklessLogDevice

  end # Logging

end # Vedeu

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
vedeu-0.8.32 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.31 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.30 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.29 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.28 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.27 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.26 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.25 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.24 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.23 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.22 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.21 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.20 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.19 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.18 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.17 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.16 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.15 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.14 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.13 lib/vedeu/logging/lockless_log_device.rb