Sha256: 84e89bb29c9268424265f126b0a57841aadc0627e9c67b26aa48cba11f28ad6c

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

#--
# Copyright (c) 2023 Jeremy Hinegardner
# All rights reserved.  See LICENSE and/or COPYING for details.
#++

require 'libsql/profile_tap'
require 'stringio'

module ::Libsql
  module Taps
    #
    # An IOTap is an easy way to send all top information to any IO based
    # object.  Both profile and trace tap information can be captured
    # This means you can send the events to STDOUT with:
    #
    #   db.profile_tap = db.trace_tap  = ::Libsql::Taps::Stdout.new
    #
    #
    class IO

      attr_reader :profile_tap
      attr_reader :io
      attr_reader :trace_count

      def initialize( io )
        @io = io
        @profile_tap = ProfileTap.new( self, 'output_profile_event' )
        @trace_count = 0
      end

      def trace( msg )
        @trace_count += 1
        io.puts msg
      end

      # need a profile method, it routes through the profile tap which calls back
      # to output_profile_event
      def profile( msg, time )
        @profile_tap.profile(msg, time)
      end

      def output_profile_event( msg, time )
        io.puts "#{time} : #{msg}"
      end

      def dump_profile
        samplers.each_pair do |k,v|
          io.puts v.to_s
        end
      end

      def samplers
        profile_tap.samplers
      end
    end

    #
    # This class provides an IO tap that writes to a StringIO.  The result is
    # available via .to_s or .string.
    #
    class StringIO < ::Libsql::Taps::IO
      def initialize
        @stringio = ::StringIO.new
        super( @stringio )
      end

      def to_s
        @stringio.string
      end
      alias :string :to_s
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
libsql-0.1.0-x64-mingw-ucrt lib/libsql/taps/io.rb
libsql-0.1.0-x64-mingw32 lib/libsql/taps/io.rb
libsql-0.1.0-x86-mingw32 lib/libsql/taps/io.rb
libsql-0.1.0 lib/libsql/taps/io.rb