Module: ClusterKit::Silence

Defined in:
lib/clusterkit/silence.rb

Overview

Module to suppress stdout output from the Rust library Following the pattern used by Rails/ActiveSupport and other popular gems

Class Method Summary collapse

Class Method Details

.maybe_silenceObject

Conditionally silence based on configuration



34
35
36
37
38
39
40
# File 'lib/clusterkit/silence.rb', line 34

def self.maybe_silence
  if ClusterKit.configuration.verbose
    yield
  else
    silence_output { yield }
  end
end

.silence_outputObject

Silence both stdout and stderr



25
26
27
28
29
30
31
# File 'lib/clusterkit/silence.rb', line 25

def self.silence_output
  silence_stream(STDOUT) do
    silence_stream(STDERR) do
      yield
    end
  end
end

.silence_stream(stream) ⇒ Object

Temporarily silence stdout and stderr This is the most idiomatic Ruby way to suppress output from C/Rust extensions

Examples:

ClusterKit::Silence.silence_stream(STDOUT) do
  # code that produces unwanted output
end


14
15
16
17
18
19
20
21
22
# File 'lib/clusterkit/silence.rb', line 14

def self.silence_stream(stream)
  old_stream = stream.dup
  stream.reopen(File::NULL)
  stream.sync = true
  yield
ensure
  stream.reopen(old_stream)
  old_stream.close
end