teerb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c00aeb61492c9a0ef82b25656834bf4abb4e9e9f
4
+ data.tar.gz: f1d316467d6399e2eaeecd6cbaeca9660df2b09b
5
+ SHA512:
6
+ metadata.gz: 295305ea5ee12424173ef0efe80bbb404cc02ea7530d8644595c6d0e6f01eba2040ffa7a02d0ed9b225814b7d66644409eb5eb07feaa586daa623083e8996b30
7
+ data.tar.gz: caa4648666413197cf0a570cedaf625d258b2948022718d8b44e0a60174ffc9668628e9521d19c2d0fb2d25d62241a4f4eb07b2dd05af1ce9cad5580fc8dfe8d
@@ -0,0 +1,3 @@
1
+ .bundle
2
+ Gemfile.lock
3
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in teerb.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) [year] [fullname]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # TeeRb - Several ways to capture and tee Ruby's standard input and output
2
+
3
+ * Pipe calls to $stdout and $stderr to a file
4
+ * Pipe calls to $stdout and $stderr to an instance of Ruby's logger class
5
+ * Pipe calls to an instance of Ruby's logger class to $stdout
6
+
7
+ ## Usage
8
+
9
+
10
+ # Tee $stdout and $stderr to several files
11
+ log_file = File.open("debug.log", "a")
12
+ tee = TeeRb::Tee.new(log_file)
13
+ tee.enable
14
+
15
+ puts 'puts hello'
16
+ $stdout.puts 'stdout hello'
17
+ $stderr.puts 'stderr hello'
18
+
19
+ tee.disable
20
+
21
+ puts 'puts hello'
22
+ $stdout.puts 'stdout hello'
23
+ $stderr.puts 'stderr hello'
24
+
25
+ # same as above but with block syntax
26
+ log_file = File.open("debug.log", "a")
27
+ TeeRb::Tee.new(log_file) do
28
+ puts 'puts hello'
29
+ $stdout.puts 'stdout hello'
30
+ $stderr.puts 'stderr hello'
31
+ end
32
+
33
+
34
+ # Capture $stdout and $stderr and send it through `logger`
35
+ require 'logger'
36
+ log_file = File.open("debug.log", "a")
37
+ logger = Logger.new(log_file)
38
+ tee = TeeRb::LoggerTee.new(logger)
39
+ tee.enable
40
+
41
+ logger.warn "hello"
42
+ $stderr.puts "stderr hello"
43
+ puts "stdout hello"
44
+
45
+
46
+ # Pipe calls to an instance of Ruby's logger class to $stdout
47
+ require 'logger'
48
+ log_file = File.open("debug.log", "a")
49
+ logger = Logger.new(TeeRb::IODelegate.new(log_file, STDOUT))
50
+ logger.warn "warn"
51
+ $stderr.puts "stderr hello"
52
+ puts "stdout hello"
53
+
54
+ ## Problems
55
+
56
+ This only works for `$stdout` and `$stderr` and not for `STDOUT` and `STDERR`, since they constants
57
+ that should not be reassigned.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,105 @@
1
+ require "teerb/version"
2
+ require 'logger'
3
+
4
+ module TeeRb
5
+ class IODelegate
6
+ def initialize(*targets)
7
+ @targets = targets
8
+ end
9
+
10
+ def write(*args)
11
+ @targets.each {|t| t.write(*args)}
12
+ end
13
+
14
+ def puts(*args)
15
+ write(*args)
16
+ write("\n")
17
+ end
18
+
19
+ def put(*args)
20
+ write(*args)
21
+ end
22
+
23
+ def close
24
+ @targets.each(&:close)
25
+ end
26
+ end
27
+
28
+ class LoggerIODelegate
29
+ def initialize(logger, log_level)
30
+ @logger = logger
31
+ @log_level = log_level
32
+ end
33
+
34
+ def write(*args)
35
+ @logger.send(@log_level, *args) unless args == ["\n"]
36
+ end
37
+
38
+ def puts(*args)
39
+ write(*args)
40
+ end
41
+
42
+ def put(*args)
43
+ write(*args)
44
+ end
45
+
46
+ def close
47
+ @logger.close
48
+ end
49
+ end
50
+
51
+
52
+ class Tee
53
+ def initialize(*logfiles, &block)
54
+ @stdout_tee = IODelegate.new(*([STDOUT] + logfiles))
55
+ @stderr_tee = IODelegate.new(*([STDERR] + logfiles))
56
+ @original_stdout = $stdout
57
+ @original_stderr = $stderr
58
+
59
+ if block_given?
60
+ enable
61
+ yield
62
+ disable
63
+ end
64
+ end
65
+
66
+ def enable
67
+ $stdout = @stdout_tee
68
+ $stderr = @stderr_tee
69
+ end
70
+
71
+ def disable
72
+ $stdout = @original_stdout
73
+ $stderr = @original_stderr
74
+ end
75
+ end
76
+
77
+ #sends stdout to logger.info
78
+ #sends stderror to logger.error
79
+ #sends everything to STDOUT
80
+ class LoggerTee
81
+ def initialize(logger)
82
+ @stdout_tee = IODelegate.new(LoggerIODelegate.new(logger, :info) , STDOUT)
83
+ @stderr_tee = IODelegate.new(LoggerIODelegate.new(logger, :error), STDERR)
84
+
85
+ @original_stdout = $stdout
86
+ @original_stderr = $stderr
87
+
88
+ if block_given?
89
+ enable
90
+ yield
91
+ disable
92
+ end
93
+ end
94
+
95
+ def enable
96
+ $stdout = @stdout_tee
97
+ $stderr = @stderr_tee
98
+ end
99
+
100
+ def disable
101
+ $stdout = @original_stdout
102
+ $stderr = @original_stderr
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module TeeRb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'teerb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "teerb"
8
+ spec.version = TeeRb::VERSION
9
+ spec.authors = ["Patrick Huesler"]
10
+ spec.email = ["patrick.huesler@gmail.com"]
11
+ spec.summary = "Ruby Tee Utils"
12
+ spec.description = "Several ways to capture and tee Ruby's standard input and output"
13
+ spec.homepage = "https://github.com/phuesler/teerb"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake", "~> 0"
23
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: teerb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Huesler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Several ways to capture and tee Ruby's standard input and output
42
+ email:
43
+ - patrick.huesler@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/teerb.rb
54
+ - lib/teerb/version.rb
55
+ - teerb.gemspec
56
+ homepage: https://github.com/phuesler/teerb
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Ruby Tee Utils
80
+ test_files: []