Sha256: 93bd9d4ec77f385df6b28d59b5185bb8989f5e1c1acc3b1fa470799b939c9cf5

Contents?: true

Size: 1.35 KB

Versions: 9

Compression:

Stored size: 1.35 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)
        @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}")
      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

9 entries across 9 versions & 1 rubygems

Version Path
vedeu-0.8.9 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.8 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.7 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.6 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.5 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.4 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.3 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.2 lib/vedeu/logging/lockless_log_device.rb
vedeu-0.8.1 lib/vedeu/logging/lockless_log_device.rb