timestamped_out 0.1.0 → 0.2.0
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/README.md +19 -4
- data/lib/timestamped_out/version.rb +1 -1
- data/lib/timestamped_out.rb +36 -32
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 140f805dbd7c2fcbd5f47704d7f1e72bb5bc7246
|
4
|
+
data.tar.gz: 6ec21b1653a47f803baaafe77630f41fc81163e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba223cbd2f97776a5052160fd0f9368867043c72505b7740fd31ebff447be8c74649f90be294ca346b497468668e4d3679b1ffa3afc1ecb36447e64484ba4982
|
7
|
+
data.tar.gz: 7e9fb68aaecb243fb6e47fb21edf95118e384ef12285270957c6cbd58d13667b0e490abb4f956ffdc2b899814792b800a8d3d59a0b8501e75ad661e52566c493
|
data/README.md
CHANGED
@@ -20,18 +20,35 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
Prints STDOUT as INFO:
|
24
|
+
|
23
25
|
```ruby
|
24
26
|
TimestampedOut.new.call do
|
25
27
|
print "\nTest"
|
26
28
|
end
|
27
|
-
2016-
|
29
|
+
2016-06-23 17:08:10 +0200 95434 TID-out0sge0o INFO: Test
|
28
30
|
```
|
29
31
|
|
32
|
+
Prints STDERR (including backtrace) as ERROR:
|
33
|
+
|
30
34
|
```ruby
|
31
35
|
TimestampedOut.new.call do
|
32
36
|
1 / 0
|
33
37
|
end
|
34
|
-
2016-
|
38
|
+
2016-06-23 17:20:42 +0200 95986 TID-ow6a19fy4 ERROR: divided by 0
|
39
|
+
...backtrace...
|
40
|
+
```
|
41
|
+
|
42
|
+
Works also with embedded shell commands:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
TimestampedOut.new.call do
|
46
|
+
system 'echo "abc"'
|
47
|
+
result = `echo "2"`
|
48
|
+
p result.to_i * 3
|
49
|
+
end
|
50
|
+
2016-06-23 17:23:07 +0200 95986 TID-ow6a2el10 INFO: abc
|
51
|
+
2016-06-23 17:23:07 +0200 95986 TID-ow6a2el10 INFO: 6
|
35
52
|
```
|
36
53
|
|
37
54
|
## Development
|
@@ -44,8 +61,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
44
61
|
|
45
62
|
Bug reports and pull requests are welcome on GitHub at https://github.com/maicher/timestamped_out. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
46
63
|
|
47
|
-
|
48
64
|
## License
|
49
65
|
|
50
66
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
51
|
-
|
data/lib/timestamped_out.rb
CHANGED
@@ -1,45 +1,49 @@
|
|
1
1
|
require 'timestamped_out/version'
|
2
2
|
|
3
3
|
class TimestampedOut
|
4
|
-
|
5
|
-
def initialize(stdout, level)
|
6
|
-
@stdout = stdout
|
7
|
-
@level = level
|
8
|
-
end
|
9
|
-
|
10
|
-
def write(str)
|
11
|
-
timestamped_str =
|
12
|
-
if str.length == 1 && str == "\n"
|
13
|
-
''
|
14
|
-
elsif str[0] == "\n"
|
15
|
-
prepend_timestamp(str[1..-1])
|
16
|
-
elsif str[-1] == "\n"
|
17
|
-
prepend_timestamp(str[0..-2])
|
18
|
-
else
|
19
|
-
prepend_timestamp(str)
|
20
|
-
end
|
4
|
+
DEFAULT_FORMATTER = lambda { |severity, datetime, progname, message| "#{datetime} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{progname} #{severity}: #{message}\n" }
|
21
5
|
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
private
|
6
|
+
attr_accessor :formatter
|
26
7
|
|
27
|
-
|
28
|
-
|
29
|
-
end
|
8
|
+
def initialize(formatter = DEFAULT_FORMATTER)
|
9
|
+
self.formatter = formatter
|
30
10
|
end
|
31
11
|
|
32
12
|
def call
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
13
|
+
old_stdout = STDOUT.clone
|
14
|
+
pipe_r, pipe_w = IO.pipe
|
15
|
+
pipe_r.sync = true
|
16
|
+
output = ''
|
17
|
+
reader = Thread.new do
|
18
|
+
begin
|
19
|
+
loop do
|
20
|
+
output << formatter.call('INFO', Time.now, '', pipe_r.readpartial(1024))
|
21
|
+
end
|
22
|
+
rescue EOFError
|
23
|
+
end
|
24
|
+
end
|
25
|
+
STDOUT.reopen(pipe_w)
|
37
26
|
yield
|
38
27
|
self
|
39
|
-
rescue
|
40
|
-
|
28
|
+
rescue StandardError => e
|
29
|
+
STDERR.write formatter.call('ERROR', Time.now, '', "#{e}\n#{e.backtrace.join("\n")}")
|
41
30
|
ensure
|
42
|
-
|
43
|
-
|
31
|
+
STDOUT.reopen(old_stdout)
|
32
|
+
pipe_w.close
|
33
|
+
reader.join
|
34
|
+
STDOUT.write(output)
|
35
|
+
return self
|
36
|
+
end
|
37
|
+
|
38
|
+
def with_timestamp(str, level)
|
39
|
+
if str.length == 1 && str == "\n"
|
40
|
+
''
|
41
|
+
elsif str[0] == "\n"
|
42
|
+
prepend_timestamp(str[1..-1], level)
|
43
|
+
elsif str[-1] == "\n"
|
44
|
+
prepend_timestamp(str[0..-2], level)
|
45
|
+
else
|
46
|
+
prepend_timestamp(str, level)
|
47
|
+
end
|
44
48
|
end
|
45
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timestamped_out
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kjm
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|