Sha256: 51657474a3a67ebea04e24ba24712a013cb04fb98e031509a8d5071c1840d108

Contents?: true

Size: 1.94 KB

Versions: 7

Compression:

Stored size: 1.94 KB

Contents

require_relative 'breadcrumb'

module Raygun
  module Breadcrumbs
    class Store
      def self.initialize
        Thread.current[:breadcrumbs] ||= []
      end

      def self.clear
        Thread.current[:breadcrumbs] = nil
      end

      def self.stored
        Thread.current[:breadcrumbs]
      end

      def self.record(
        message: nil,
        category: '',
        level: :info,
        timestamp: Time.now.utc.to_i,
        metadata: {},
        class_name: nil,
        method_name: nil,
        line_number: nil
      )
        raise ArgumentError.new('missing keyword: message') if message == nil
        crumb = Breadcrumb.new

        crumb.message = message
        crumb.category = category
        crumb.level = level
        crumb.metadata = metadata
        crumb.timestamp = timestamp
        crumb.type = 'manual'

        caller = caller_locations[1]
        crumb.class_name = class_name
        crumb.method_name = method_name || caller.label
        crumb.line_number = line_number || caller.lineno

        Thread.current[:breadcrumbs] << crumb if should_record?(crumb)
      end

      def self.any?
        stored != nil && stored.length > 0
      end

      private

      def self.should_record?(crumb)
        if stored.nil?
          if Raygun.configuration.debug
            Raygun.log('[Raygun.breadcrumbs] store is uninitialized while breadcrumb is being recorded, discarding breadcrumb')
          end

          return false
        end

        levels = Raygun::Breadcrumbs::BREADCRUMB_LEVELS

        active_level = levels.index(Raygun.configuration.breadcrumb_level)
        crumb_level = levels.index(crumb.level) || -1

        discard = crumb_level < active_level

        if discard && Raygun.configuration.debug
          Raygun.log("[Raygun.breadcrumbs] discarding breadcrumb because #{crumb.level} is below active breadcrumb level (#{Raygun.configuration.breadcrumb_level})")
        end

        !discard
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
raygun4ruby-3.1.1 lib/raygun/breadcrumbs/store.rb
raygun4ruby-3.1.0 lib/raygun/breadcrumbs/store.rb
raygun4ruby-3.0.0 lib/raygun/breadcrumbs/store.rb
raygun4ruby-2.7.1 lib/raygun/breadcrumbs/store.rb
raygun4ruby-2.7.0 lib/raygun/breadcrumbs/store.rb
raygun4ruby-2.6.0 lib/raygun/breadcrumbs/store.rb
raygun4ruby-2.5.0 lib/raygun/breadcrumbs/store.rb