reox 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/reox.rb +38 -43
- data/lib/reox_file.rb +2 -2
- data/lib/reox_io.rb +38 -20
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9ce8289e033cd4a41d292664abaa3da11cc557f410329a7827bd9d209e7b6aa1
|
|
4
|
+
data.tar.gz: 842630f74649820d5ac955641af2e1329d121f54da788f267235f0a2d2676490
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d9dac730088b5e7d2d6e77e28228dd2357a3fc25061a9c6c0b26b306d6a922e2bf659e8a4205ade4905f243949bb5b3ad487beb296ad6e4d9e3e5eef291616c2
|
|
7
|
+
data.tar.gz: 833a76a755fe0a2daafce2b951a84e2f90c021452271a1457b31d8af90cfc47e773dc43316a3933603c174271a2ebd5783dbb042c61bd91e0b85d7aaa4a5783e
|
data/lib/reox.rb
CHANGED
|
@@ -1,48 +1,43 @@
|
|
|
1
1
|
require_relative 'reox_io'
|
|
2
2
|
require_relative 'reox_file'
|
|
3
3
|
|
|
4
|
+
# Reox is the driver class, it hijacks and restore streams
|
|
4
5
|
class Reox
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
$stderr = STDOUT if $stderr.is_a? ReoxIO
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def self.setup_files
|
|
38
|
-
FILE_NAMES.map do |file_name|
|
|
39
|
-
setup_file(file_name)
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def self.setup_file(file_name)
|
|
44
|
-
file = ReoxFile.open(File.join(Dir.pwd,file_name),mode: 'a')
|
|
45
|
-
file.sync = true
|
|
46
|
-
file
|
|
6
|
+
FILE_NAMES = %w[output.log error.log].freeze
|
|
7
|
+
def self.record(opts = {})
|
|
8
|
+
hijack_streams(opts)
|
|
9
|
+
yield
|
|
10
|
+
restore_streams
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.record!(opts = {})
|
|
14
|
+
hijack_streams(opts)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.stop!
|
|
18
|
+
restore_streams
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private_class_method def self.hijack_streams(opts)
|
|
22
|
+
output_log, error_log = setup_files
|
|
23
|
+
$stdout = ReoxIO.new([STDOUT, output_log], opts.merge!(type: ReoxIO::OUT)) unless $stdout.is_a? ReoxIO
|
|
24
|
+
$stderr = ReoxIO.new([STDERR, error_log], opts.merge!(type: ReoxIO::ERR)) unless $stderr.is_a? ReoxIO
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private_class_method def self.restore_streams
|
|
28
|
+
$stdout = STDOUT if $stdout.is_a? ReoxIO
|
|
29
|
+
$stderr = STDERR if $stderr.is_a? ReoxIO
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private_class_method def self.setup_files
|
|
33
|
+
FILE_NAMES.map do |file_name|
|
|
34
|
+
setup_file(file_name)
|
|
47
35
|
end
|
|
48
|
-
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private_class_method def self.setup_file(file_name)
|
|
39
|
+
file = ReoxFile.open(File.join(Dir.pwd, file_name), mode: 'a')
|
|
40
|
+
file.sync = true
|
|
41
|
+
file
|
|
42
|
+
end
|
|
43
|
+
end
|
data/lib/reox_file.rb
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
class ReoxFile < File
|
|
2
|
-
end
|
|
1
|
+
class ReoxFile < File
|
|
2
|
+
end
|
data/lib/reox_io.rb
CHANGED
|
@@ -1,28 +1,46 @@
|
|
|
1
1
|
# Insipired from https://stackoverflow.com/a/6407200
|
|
2
|
+
require 'logger'
|
|
2
3
|
require_relative 'reox_file'
|
|
4
|
+
|
|
5
|
+
# ReoxIO mimics the IO class for STDOUT and STDERR,
|
|
6
|
+
# As well as injects the ReoxFile with logs
|
|
3
7
|
class ReoxIO
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
OUT = :out
|
|
9
|
+
ERR = :err
|
|
10
|
+
|
|
11
|
+
def initialize(streams, opts = {})
|
|
12
|
+
@streams = streams
|
|
13
|
+
@verbose = opts[:verbose] || false
|
|
14
|
+
@type = opts[:type] || OUT
|
|
15
|
+
@formatter = Logger::Formatter.new if @verbose
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def write(*content)
|
|
19
|
+
@streams.each do |stream|
|
|
20
|
+
if @verbose && stream.is_a?(ReoxFile) && !content.join.strip.empty?
|
|
21
|
+
message = get_log_message(severity_level, caller(3..3).first, content.join)
|
|
22
|
+
stream.write(message)
|
|
14
23
|
else
|
|
15
24
|
stream.write(*content)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
19
28
|
|
|
20
|
-
|
|
29
|
+
def flush
|
|
21
30
|
@streams.each(&:flush)
|
|
22
|
-
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def close
|
|
34
|
+
@streams.each(&:close)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def severity_level
|
|
40
|
+
@type == OUT ? :INFO : :ERROR
|
|
41
|
+
end
|
|
23
42
|
|
|
24
|
-
|
|
25
|
-
@
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
end
|
|
43
|
+
def get_log_message(severity, progname, msg)
|
|
44
|
+
@formatter.call(severity, Time.now, progname, msg)
|
|
45
|
+
end
|
|
46
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: reox
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jatin Dhankhar
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2019-04-
|
|
11
|
+
date: 2019-04-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A simple stream logger library
|
|
14
14
|
email: jatin@jatindhankhar.in
|