syslogify 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5a06fe92a907c52f29708c3ade97894d62ae2ae
4
- data.tar.gz: d419b1ece05a7146b93a12d745e952506ebbe904
3
+ metadata.gz: 7ff824b514a7b5f41ea8d6cae842dbb316d47d0a
4
+ data.tar.gz: fe71fcb5d549727cd8671d117612fe00469d13a6
5
5
  SHA512:
6
- metadata.gz: c1cb0108438fae9ce6fcd5e4b712b27f415607def67070ca013bc523b84af5cf9744b85870965e92d86cc893c075a78bdf6d155fda0eb520be3ff6d7cffa913d
7
- data.tar.gz: 262bf1ea50fd9a44c4ab825b4bad2bb34461d90c3f3f3f2bfd72b0f3c784f2480d3fd4d47db34710c6e476b93a8b480426674340c4568c43f539b0219b2ea5cd
6
+ metadata.gz: 761199098e7713764ec677c0e83f304c5b7a5a0d05eca888822c54f30459c0c8db962040cbb53b2dea72a294907da72693c52bdb4222de3d62e6759dc1e7a6fd
7
+ data.tar.gz: 18b68acc82d09618ea770c653c3510fe8dad89a78ba9c25a555643c4d38333eb0c57ce330ebd4ba8eb0be8666200e2590171e0f4daca6ab00c7fe3372f1864aa
@@ -16,19 +16,25 @@ module Syslogify
16
16
  @pid = nil
17
17
  end
18
18
 
19
+ # causes standard output and error to be redirected to syslog
20
+ # (through a subprocess)
19
21
  def start
20
- return if @pid
22
+ return if @sink
21
23
 
22
- rd, wr = IO.pipe
24
+ drain, @sink = IO.pipe
23
25
  @old_stdout = STDOUT.dup
24
26
  @old_stderr = STDERR.dup
25
27
 
28
+ # Test connection to syslog in the parent process
26
29
  Syslog.open(_identity, Syslog::LOG_CONS | Syslog::LOG_NDELAY)
27
30
  Syslog.log(Syslog::LOG_NOTICE, 'Diverting logs to syslog')
28
31
 
29
- @pid = fork do
30
- STDIN.reopen(rd)
31
- wr.close
32
+ # spawn a subprocess which pipes from its standard input to syslog
33
+ pid = fork do
34
+ Process.daemon # otherwise we'll intercept signals
35
+ $PROGRAM_NAME = "#{Syslog.ident}.syslogify"
36
+ STDIN.reopen(drain)
37
+ @sink.close
32
38
 
33
39
  while line = STDIN.gets
34
40
  Syslog.log(Syslog::LOG_NOTICE, line.gsub('%', '%%'))
@@ -36,33 +42,47 @@ module Syslogify
36
42
  Syslog.log(Syslog::LOG_NOTICE, 'Shutting down')
37
43
  end
38
44
 
39
- Syslog.close
45
+ Process.detach(pid) # the subprocess will shut down on its own once starved of input
46
+ Syslog.close # the parent does not need syslog access
40
47
 
41
- rd.close
42
- STDOUT.reopen(wr)
43
- STDERR.reopen(wr)
48
+ # redirect stdout/err to the subprocess
49
+ drain.close
50
+ STDOUT.reopen(@sink)
51
+ STDERR.reopen(@sink)
44
52
  STDOUT.sync = STDERR.sync = true
45
-
46
- @old_stderr.puts('Check syslog for further messages')
53
+
54
+ begin
55
+ @old_stderr.puts('Check syslog for further messages')
56
+ rescue Errno::EPIPE
57
+ # this can happen if something else tampered with our @old streams,
58
+ # e.g. the daemons library
59
+ end
47
60
  self
48
61
  end
49
62
 
63
+ # cancels the outout redirection
50
64
  def stop
51
- return unless @pid
52
- STDOUT.reopen(@old_stdout)
53
- STDERR.reopen(@old_stderr)
65
+ return unless @sink
66
+ # NOTE: we shouldn't kill the subprocess (which can be shared igqf the parent
67
+ # forked after #start), and it'll shut down on its own anyways.
68
+ STDOUT.reopen(@old_stdout) unless @old_stdout.closed?
69
+ STDERR.reopen(@old_stderr) unless @old_stderr.closed?
70
+ @sink.close unless @sink.closed?
54
71
  @old_stdout = @old_stderr = nil
55
-
56
- Process.kill('TERM', @pid)
57
- Process.wait(@pid)
58
- @pid = nil
72
+ @sink = nil
59
73
  self
60
74
  end
61
75
 
76
+ # useful when e.g. the process name changes, and/or after forking
77
+ def restart
78
+ stop
79
+ start
80
+ end
81
+
62
82
  private
63
83
 
64
84
  def _identity
65
- @identity ||= ENV.fetch('SYSLOG_IDENTITY', File.basename($PROGRAM_NAME))
85
+ ENV.fetch('SYSLOG_IDENTITY', File.basename($PROGRAM_NAME))
66
86
  end
67
87
  end
68
88
  end
@@ -1,3 +1,3 @@
1
1
  module Syslogify
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syslogify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Letessier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-06 00:00:00.000000000 Z
11
+ date: 2015-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler