Sha256: d8b9d03c818cfc3f7373a5b52ba728ad12298a89c22a553e267adbbe6b17f987

Contents?: true

Size: 1.54 KB

Versions: 120

Compression:

Stored size: 1.54 KB

Contents

require_relative '../../puppet/thread_local'

module Puppet
  module Pops
    # Utility class for keeping track of the "Puppet stack", ie the file
    # and line numbers of Puppet Code that created the current context.
    #
    # To use this make a call with:
    #
    # ```rb
    # Puppet::Pops::PuppetStack.stack(file, line, receiver, message, args)
    # ```
    #
    # To get the stack call:
    #
    # ```rb
    # Puppet::Pops::PuppetStack.stacktrace
    # ```
    #
    # or
    #
    # ```rb
    # Puppet::Pops::PuppetStack.top_of_stack
    # ```
    #
    # To support testing, a given file that is an empty string, or nil
    # as well as a nil line number are supported. Such stack frames
    # will be represented with the text `unknown` and `0ยด respectively.
    module PuppetStack
      @stack = Puppet::ThreadLocal.new { Array.new }

      def self.stack(file, line, obj, message, args, &block)
        file = 'unknown' if (file.nil? || file == '')
        line = 0 if line.nil?

        result = nil
        @stack.value.unshift([file, line])
        begin
          if block_given?
            result = obj.send(message, *args, &block)
          else
            result = obj.send(message, *args)
          end
        ensure
          @stack.value.shift()
        end
        result
      end

      def self.stacktrace
        @stack.value.dup
      end

      # Returns an Array with the top of the puppet stack, or an empty
      # Array if there was no such entry.
      def self.top_of_stack
        @stack.value.first || []
      end
    end
  end
end

Version data entries

120 entries across 120 versions & 1 rubygems

Version Path
puppet-7.34.0 lib/puppet/pops/puppet_stack.rb
puppet-7.34.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.34.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.34.0-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-7.33.0 lib/puppet/pops/puppet_stack.rb
puppet-7.33.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.33.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.33.0-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-7.32.1 lib/puppet/pops/puppet_stack.rb
puppet-7.32.1-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.32.1-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.32.1-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-7.31.0 lib/puppet/pops/puppet_stack.rb
puppet-7.31.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.31.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.31.0-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-7.30.0 lib/puppet/pops/puppet_stack.rb
puppet-7.30.0-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.30.0-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-7.30.0-universal-darwin lib/puppet/pops/puppet_stack.rb