Class: RuntimeInspection::ThreadRedirect

Inherits:
Object
  • Object
show all
Defined in:
lib/rti/redirect.rb

Overview

Used when we want to redirect stdout or stderr for a specific thread. This is intelligent enough so that it reuses one if it is already in place from another thread.

Constant Summary collapse

MUTEX =

Synchronization for ensuring we are either using the same redirection manager or creating only one new one.

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_io) ⇒ ThreadRedirect

Create a new redirection manager for the IO stream provided.



67
68
69
70
# File 'lib/rti/redirect.rb', line 67

def initialize( default_io )
    @default_io = default_io
    @usage = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/rti/redirect.rb', line 92

def method_missing( sym, *args, &block )
    if rio = Thread.current[:rti_redirect_io]
        rio.send( sym, *args, &block )
    else
        @default_io.send( sym, *args, &block )
    end
end

Instance Attribute Details

#default_ioObject (readonly)

The IO to use for non-redirected output.



79
80
81
# File 'lib/rti/redirect.rb', line 79

def default_io
  @default_io
end

#usageObject

A reference counter if other threads are also using this redirection manager.



75
76
77
# File 'lib/rti/redirect.rb', line 75

def usage
  @usage
end

Class Method Details

.startObject

Replace the global stdout and stderr with a redirection object. If one is already there, then use it.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rti/redirect.rb', line 26

def self.start
    MUTEX.synchronize do
        if $stdout.kind_of? self
            $stdout.usage += 1
        else
            $stdout = new( $stdout )
        end
        if $stderr.kind_of? self
            $stderr.usage += 1
        else
            $stderr = new( $stderr )
        end
    end
    return true
end

.stopObject

Stop any redirection and put the original IO back in its place unless there are still other threads using redirection.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rti/redirect.rb', line 46

def self.stop
    MUTEX.synchronize do
        unless $stdout.kind_of? self
            if $stdout.usage > 0
                $stdout.usage -= 1
            else
                $stdout = $stdout.default_io
            end
        end
        unless $stderr.kind_of? self
            if $stderr.usage > 0
                $stderr.usage -= 1
            else
                $stderr = $stderr.default_io
            end
        end
    end
end

Instance Method Details

#write(*args) ⇒ Object

Provide the output method so this object can be used as either stdout or stderr.



84
85
86
87
88
89
90
# File 'lib/rti/redirect.rb', line 84

def write( *args )
    if rio = ::Thread.current[:rti_redirect_io]
        rio.write( *args )
    else
        @default_io.write( *args )
    end
end