Class: RubywHelper

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

Constant Summary collapse

Version =
VERSION = '0.1.5'
Defaults =
{
  :out => File.join(Dir.pwd, 'logs', "#{app_name}.stdout.log"),
  :err => File.join(Dir.pwd, 'logs', "#{app_name}.stderr.log"),
  :in  => case RUBY_PLATFORM
          when /mingw|mswin/
            'NUL:'
          else
            '/dev/null'
          end
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out = nil, err = nil, inn = nil) ⇒ RubywHelper

out replaces $stdout, err replaces $stderr, inn replaces $stdin, simple. provide nils / false to use the Defaults



25
26
27
28
29
# File 'lib/rubyw_helper.rb', line 25

def initialize(out = nil, err = nil, inn = nil)
  @stdout, @stderr = out || Defaults[:out], err || Defaults[:err]
  @stdin = inn || Defaults[:in]
  @old_out, @old_err, @old_in = $stdout, $stderr, $stdin
end

Instance Attribute Details

#old_errObject (readonly)

Returns the value of attribute old_err.



21
22
23
# File 'lib/rubyw_helper.rb', line 21

def old_err
  @old_err
end

#old_inObject (readonly)

Returns the value of attribute old_in.



21
22
23
# File 'lib/rubyw_helper.rb', line 21

def old_in
  @old_in
end

#old_outObject (readonly)

Returns the value of attribute old_out.



21
22
23
# File 'lib/rubyw_helper.rb', line 21

def old_out
  @old_out
end

Class Method Details

.versionObject



7
# File 'lib/rubyw_helper.rb', line 7

def self.version; Version; end

Instance Method Details

#redirect_stdio!Object

Sets up the global IO objects to point to where we want.



66
67
68
69
70
71
72
73
# File 'lib/rubyw_helper.rb', line 66

def redirect_stdio!
  inn, out, err = open(@stdin), open(@stdout, 'a+'), open(@stderr, 'a+')
  no_warn do
    $stdin   = Object.const_set(:STDIN,  inn)
    $stdout  = Object.const_set(:STDOUT, out)
    $stderr  = Object.const_set(:STDERR, err)
  end
end

#restore_stdio!Object



75
76
77
78
79
80
81
# File 'lib/rubyw_helper.rb', line 75

def restore_stdio!
  no_warn do
    $stdin   = Object.const_set(:STDIN,  @old_in)
    $stdout  = Object.const_set(:STDOUT, @old_out)
    $stderr  = Object.const_set(:STDERR, @old_err)
  end
end

#stdio_danger?Boolean

Returns true for the two common cases when you would not receive data from stdout, stderr and stdin, because they’re nulled out in some way, closed, or in some other way unusable. This specific implementation provides checks for rubyw.exe behavior, where all IOs are closed, and Win32::Daemon behavior, where they’re all nulled. Sometimes may not be accurate, recommendation is to redirect by configuration, and use this as a guide only where appropriate.

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubyw_helper.rb', line 38

def stdio_danger?
  # rubyw.exe running as a user:
  $stdout.closed? && $stderr.closed? && $stdin.closed? ||
  # rubyw.exe + Win32::Daemon started:
  [$stdout, $stderr, $stdin].all? { |io| io.inspect =~ /NUL/ } ||
  # rubyw.exe running as SYSTEM, pre Win32::Daemon started:
  begin
    open("CONIN$") {}
    open("CONOUT$", "w") {}
    false
  rescue SystemCallError
    true
  end
end

#with_redirectionObject

Takes a block, because under these conditions, it really helps developers if best effort is made to try and log error conditions to the files before leaving the process.



56
57
58
59
60
61
62
63
# File 'lib/rubyw_helper.rb', line 56

def with_redirection
  ensure_files!
  redirect_stdio!
  yield
  restore_stdio!
rescue Exception => exception
  fatal! exception.to_s_mri
end