ztk 1.14.1 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWQ3ZjgzODdmMWM2ZWNmMTY2NDk4ZjBjNjhmYjY2ZmVmYzgxZDM3Ng==
4
+ ODJhNjRkODMyODczZDA0ZTNhN2RjZjI1Y2Q2OTY3MWMzNzlhYzUzYg==
5
5
  data.tar.gz: !binary |-
6
- ODBkZmE5ZjFjOTRhZTcyODY1ZTdhYWQ1ZGJkNDM2ODVhMzg2NWFmMQ==
6
+ YjMzYjJlOGY0ZTkxOTg4NzhmZDMyMzE0NDNiNjE2YTdiZDBjMzZjZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ODE1YTk3ZmUyODk5MDNhNjIwNmU2NWIyZjBlYzdjNmVmMzYzZThjNDc1NzEx
10
- YTc5OTI1MGJkZjczM2M1NWM0MTFmM2E4ZmMxMGUwNmRmZjZkNTcwZjJkN2U4
11
- ZjgxZWEyNWVkMDFiNzNhMjM1MmIxZjdmMDU5N2M1N2NlYWI0NWI=
9
+ OGYyMTI5ODlhNzNmYzAxNDc1N2E2MjZmYzRkNGRmYzBkODc4YjBhNTZjYzgw
10
+ NTU4NTczMzJkYWY0N2EzNzA5ZmViODM4YWJmY2MxYjE5OGExZWU4OTIxNTcx
11
+ N2JjNDY4MTI5NjBjYjFkNzBmMzEwMzFlMGIyMzRiNmZmMDhiOGQ=
12
12
  data.tar.gz: !binary |-
13
- NGE0ZjhiZjhlMWUyMmUzNjAzYjZiMWJjZTRkNzZlODQ0YTYyZWJjYjU5ZjAz
14
- MzYwYjA0NDk3YjdmNTcyYWE5MmRmYzg5OGIxMjI3NzI1Y2Y1ZWZkZTAwNDRj
15
- NzBmMDQwOWU2Y2UwZWRhZmIzNGQ2OGQ2MWQzZTVhYTRhMWU1ZDc=
13
+ NjM4MDk0NDRkOGYxNDk3NmQxODQ1YTNmOTY0ZmJiYmI4MTE1NTljYmZlN2M5
14
+ OTNjZjE4ZTkzZjc4M2RiZTcxMzkxNDQwY2RiMTBjMDY5NDJhMzM5NGJjZDEz
15
+ ZmY5MGZiYjg0ZDQ4YWFiMjFjYzA3NGE3MWNmM2YxZWUzMjY0YmY=
data/lib/ztk/base.rb CHANGED
@@ -128,13 +128,7 @@ module ZTK
128
128
  if !block_given?
129
129
  log_and_raise(BaseError, "You must supply a block to the log method!")
130
130
  elsif (@config.ui.logger.level <= ZTK::Logger.const_get(log_level.to_s.upcase))
131
- if @config.ui.logger.respond_to?(:logdev)
132
- @config.ui.logger.logdev.write(ZTK::ANSI.uncolor(yield))
133
- @config.ui.logger.logdev.respond_to?(:flush) and @config.ui.logger.logdev.flush
134
- else
135
- @config.ui.logger.instance_variable_get(:@logdev).instance_variable_get(:@dev).write(ZTK::ANSI.uncolor(yield))
136
- @config.ui.logger.instance_variable_get(:@logdev).instance_variable_get(:@dev).respond_to?(:flush) and @config.ui.logger.instance_variable_get(:@logdev).instance_variable_get(:@dev).flush
137
- end
131
+ @config.ui.logger << ZTK::ANSI.uncolor(yield)
138
132
  end
139
133
  end
140
134
 
data/lib/ztk/logger.rb CHANGED
@@ -4,9 +4,14 @@ module ZTK
4
4
 
5
5
  # Standard Logging Class
6
6
  #
7
- # Supplies loggers the same as the base ruby logger class, except adds some
8
- # extra spice to your log messages. This includes uSec timestamping, PIDs and
9
- # caller tree details.
7
+ # Allows chaining standard Ruby loggers as well as adding extra spice to your
8
+ # log messages. This includes uSec timestamping, PIDs and caller tree
9
+ # details.
10
+ #
11
+ # This class accepts the same initialize arguments as the Ruby logger class.
12
+ # You can chain multiple loggers together, for example to get an effect of
13
+ # logging to STDOUT and a file simultaneously without having to modify your
14
+ # existing logging statements.
10
15
  #
11
16
  # One can override the logging level on the command line with programs that
12
17
  # use this library like so:
@@ -22,21 +27,70 @@ module ZTK
22
27
  # $logger.error { "This is a error message!" }
23
28
  # $logger.fatal { "This is a fatal message!" }
24
29
  #
30
+ # = Simple logger chain:
31
+ #
32
+ # logger = ZTK::Logger.new
33
+ # logger.loggers << ::Logger.new(STDOUT)
34
+ # logger.loggers << ::Logger.new('test.log')
35
+ #
36
+ # logger.debug { "This will be written to STDOUT as well as test.log!" }
37
+ #
38
+ # = Alternate logger chaining:
39
+ #
40
+ # logger = ZTK::Logger.new(STDOUT)
41
+ # logger.loggers << ::Logger.new('test.log')
42
+ #
43
+ # logger.debug { "This will be written to STDOUT as well as test.log!" }
44
+ #
25
45
  # @author Zachary Patten <zachary AT jovelabs DOT com>
26
46
  class Logger < ::Logger
27
- attr_accessor :stdout_echo
28
47
 
29
48
  # Log Levels
30
49
  SEVERITIES = Severity.constants.inject([]) {|arr,c| arr[Severity.const_get(c)] = c; arr}
31
50
 
51
+ class LogDevice
52
+ attr_reader :dev
53
+ attr_reader :filename
54
+
55
+ def initialize(chain)
56
+ @chain = chain
57
+ @dev = nil
58
+ @filename = nil
59
+ end
60
+
61
+ def write(message)
62
+ @chain.loggers.each do |logger|
63
+ logger << message
64
+ end
65
+ end
66
+
67
+ def close
68
+ @chain.loggers.each do |logger|
69
+ logger.instance_variable_get(:@logdev).close
70
+ end
71
+ end
72
+ end
73
+
74
+
75
+ attr_accessor :loggers
76
+
32
77
  def initialize(*args)
33
- super(*args)
78
+ super(StringIO.new)
79
+
80
+ @loggers = Array.new
81
+ if args.count > 0
82
+ @loggers << ::Logger.new(*args)
83
+ end
84
+
85
+ @logdev = LogDevice.new(self)
86
+
34
87
  set_log_level
35
88
  end
36
89
 
37
- # Provides access to the raw log device.
38
- def logdev
39
- @logdev.instance_variable_get(:@dev)
90
+ # Generates a human-readable string about the logger.
91
+ def inspect
92
+ loggers_inspect = @loggers.collect{|logger| logger.instance_variable_get(:@logdev).instance_variable_get(:@dev).inspect }.join(', ')
93
+ "#<#{self.class} loggers=[#{loggers_inspect}]>"
40
94
  end
41
95
 
42
96
  # Specialized logging. Logs messages in the same format, except has the
@@ -55,9 +109,12 @@ module ZTK
55
109
  add(severity, nil, nil, shift, &block)
56
110
  end
57
111
 
58
- # Generates a human-readable string about the logger.
59
- def inspect
60
- "#<#{self.class} filename=#{@logdev.filename.inspect}>"
112
+ def level=(value)
113
+ @level = value
114
+
115
+ @loggers.each { |logger| logger.level = @level }
116
+
117
+ value
61
118
  end
62
119
 
63
120
 
@@ -96,9 +153,7 @@ module ZTK
96
153
  message = [message, progname].flatten.compact.join(": ")
97
154
  message = "%19s.%06d|%05d|%5s|%s%s\n" % [Time.now.utc.strftime("%Y-%m-%d|%H:%M:%S"), Time.now.utc.usec, Process.pid, SEVERITIES[severity], called_by, message]
98
155
 
99
- statement = ZTK::ANSI.uncolor(message)
100
- (stdout_echo == true) and $stdout.write(statement)
101
- @logdev.write(statement)
156
+ @logdev.write(ZTK::ANSI.uncolor(message))
102
157
  @logdev.respond_to?(:flush) and @logdev.flush
103
158
 
104
159
  true
@@ -41,45 +41,56 @@ module ZTK
41
41
  options.ui.logger.info { "download(#{remote.inspect}, #{local.inspect})" }
42
42
 
43
43
  ZTK::RescueRetry.try(:ui => config.ui, :tries => ZTK::SSH::RESCUE_RETRY_ATTEMPTS, :on_retry => method(:on_retry)) do
44
- if (options.use_scp == false)
45
- sftp.download!(remote.to_s, local.to_s, options.send(:table)) do |event, downloader, *args|
46
- case event
47
- when :open
48
- options.ui.logger.debug { "download(#{args[0].remote} -> #{args[0].local})" }
49
- options.on_progress.nil? or options.on_progress.call(:open, args)
50
- when :close
51
- options.ui.logger.debug { "close(#{args[0].local})" }
52
- options.on_progress.nil? or options.on_progress.call(:close, args)
53
- when :mkdir
54
- options.ui.logger.debug { "mkdir(#{args[0]})" }
55
- options.on_progress.nil? or options.on_progress.call(:mkdir, args)
56
- when :get
57
- options.ui.logger.debug { "get(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
58
- options.on_progress.nil? or options.on_progress.call(:get, args)
59
- when :finish
60
- options.ui.logger.debug { "finish" }
61
- options.on_progress.nil? or options.on_progress.call(:finish, args)
62
- end
63
- end
44
+ if (options.use_scp == true)
45
+ scp_download(remote, local, options)
64
46
  else
65
- opened = false
66
- args = []
47
+ sftp_download(remote, local, options)
48
+ end
49
+ end
67
50
 
68
- scp.download!(remote.to_s, local.to_s, options.send(:table)) do |ch, name, sent, total|
69
- args = [ OpenStruct.new(:size => total, :local => name, :remote => name), sent, '' ]
51
+ true
52
+ end
70
53
 
71
- opened or (options.on_progress.nil? or options.on_progress.call(:open, args) and (opened = true))
72
54
 
73
- options.ui.logger.debug { "get(#{args[0].remote}, sent #{args[1]}, total #{args[0].size})" }
74
- options.on_progress.nil? or options.on_progress.call(:get, args)
75
- end
55
+ private
76
56
 
57
+ def sftp_download(remote, local, options={})
58
+ sftp.download!(remote.to_s, local.to_s, options.send(:table)) do |event, downloader, *args|
59
+ case event
60
+ when :open
61
+ options.ui.logger.debug { "download(#{args[0].remote} -> #{args[0].local})" }
62
+ options.on_progress.nil? or options.on_progress.call(:open, args)
63
+ when :close
64
+ options.ui.logger.debug { "close(#{args[0].local})" }
65
+ options.on_progress.nil? or options.on_progress.call(:close, args)
66
+ when :mkdir
67
+ options.ui.logger.debug { "mkdir(#{args[0]})" }
68
+ options.on_progress.nil? or options.on_progress.call(:mkdir, args)
69
+ when :get
70
+ options.ui.logger.debug { "get(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
71
+ options.on_progress.nil? or options.on_progress.call(:get, args)
72
+ when :finish
77
73
  options.ui.logger.debug { "finish" }
78
74
  options.on_progress.nil? or options.on_progress.call(:finish, args)
79
75
  end
80
76
  end
77
+ end
81
78
 
82
- true
79
+ def scp_download(remote, local, options={})
80
+ opened = false
81
+ args = []
82
+
83
+ scp.download!(remote.to_s, local.to_s, options.send(:table)) do |ch, name, sent, total|
84
+ args = [ OpenStruct.new(:size => total, :local => name, :remote => name), sent, '' ]
85
+
86
+ opened or (options.on_progress.nil? or options.on_progress.call(:open, args) and (opened = true))
87
+
88
+ options.ui.logger.debug { "get(#{args[0].remote}, sent #{args[1]}, total #{args[0].size})" }
89
+ options.on_progress.nil? or options.on_progress.call(:get, args)
90
+ end
91
+
92
+ options.ui.logger.debug { "finish" }
93
+ options.on_progress.nil? or options.on_progress.call(:finish, args)
83
94
  end
84
95
 
85
96
  end
@@ -39,45 +39,56 @@ module ZTK
39
39
  config.ui.logger.info { "upload(#{local.inspect}, #{remote.inspect})" }
40
40
 
41
41
  ZTK::RescueRetry.try(:ui => config.ui, :tries => ZTK::SSH::RESCUE_RETRY_ATTEMPTS, :on_retry => method(:on_retry)) do
42
- if (options.use_scp == false)
43
- sftp.upload!(local.to_s, remote.to_s) do |event, uploader, *args|
44
- case event
45
- when :open
46
- options.ui.logger.debug { "upload(#{args[0].local} -> #{args[0].remote})" }
47
- options.on_progress.nil? or options.on_progress.call(:open, args)
48
- when :close
49
- options.ui.logger.debug { "close(#{args[0].remote})" }
50
- options.on_progress.nil? or options.on_progress.call(:close, args)
51
- when :mkdir
52
- options.ui.logger.debug { "mkdir(#{args[0]})" }
53
- options.on_progress.nil? or options.on_progress.call(:mkdir, args)
54
- when :put
55
- options.ui.logger.debug { "put(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
56
- options.on_progress.nil? or options.on_progress.call(:put, args)
57
- when :finish
58
- options.ui.logger.debug { "finish" }
59
- options.on_progress.nil? or options.on_progress.call(:finish, args)
60
- end
61
- end
42
+ if (options.use_scp == true)
43
+ scp_upload(local, remote, options)
62
44
  else
63
- opened = false
64
- args = []
45
+ sftp_upload(local, remote, options)
46
+ end
47
+ end
65
48
 
66
- scp.upload!(local.to_s, remote.to_s, options.send(:table)) do |ch, name, sent, total|
67
- args = [ OpenStruct.new(:size => total, :local => name, :remote => name), sent, '' ]
49
+ true
50
+ end
68
51
 
69
- opened or (options.on_progress.nil? or options.on_progress.call(:open, args) and (opened = true))
70
52
 
71
- options.ui.logger.debug { "put(#{args[0].remote}, sent #{args[1]}, total #{args[0].size})" }
72
- options.on_progress.nil? or options.on_progress.call(:put, args)
73
- end
53
+ private
74
54
 
55
+ def sftp_upload(local, remote, options={})
56
+ sftp.upload!(local.to_s, remote.to_s) do |event, uploader, *args|
57
+ case event
58
+ when :open
59
+ options.ui.logger.debug { "upload(#{args[0].local} -> #{args[0].remote})" }
60
+ options.on_progress.nil? or options.on_progress.call(:open, args)
61
+ when :close
62
+ options.ui.logger.debug { "close(#{args[0].remote})" }
63
+ options.on_progress.nil? or options.on_progress.call(:close, args)
64
+ when :mkdir
65
+ options.ui.logger.debug { "mkdir(#{args[0]})" }
66
+ options.on_progress.nil? or options.on_progress.call(:mkdir, args)
67
+ when :put
68
+ options.ui.logger.debug { "put(#{args[0].remote}, size #{args[2].size} bytes, offset #{args[1]})" }
69
+ options.on_progress.nil? or options.on_progress.call(:put, args)
70
+ when :finish
75
71
  options.ui.logger.debug { "finish" }
76
72
  options.on_progress.nil? or options.on_progress.call(:finish, args)
77
73
  end
78
74
  end
75
+ end
79
76
 
80
- true
77
+ def scp_upload(local, remote, options={})
78
+ opened = false
79
+ args = []
80
+
81
+ scp.upload!(local.to_s, remote.to_s, options.send(:table)) do |ch, name, sent, total|
82
+ args = [ OpenStruct.new(:size => total, :local => name, :remote => name), sent, '' ]
83
+
84
+ opened or (options.on_progress.nil? or options.on_progress.call(:open, args) and (opened = true))
85
+
86
+ options.ui.logger.debug { "put(#{args[0].remote}, sent #{args[1]}, total #{args[0].size})" }
87
+ options.on_progress.nil? or options.on_progress.call(:put, args)
88
+ end
89
+
90
+ options.ui.logger.debug { "finish" }
91
+ options.on_progress.nil? or options.on_progress.call(:finish, args)
81
92
  end
82
93
 
83
94
  end
data/lib/ztk/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module ZTK
2
2
 
3
3
  # ZTK Version String
4
- VERSION = "1.14.1"
4
+ VERSION = "1.15.0"
5
5
 
6
6
  end
@@ -87,6 +87,7 @@ describe ZTK::Background do
87
87
  it "should respond false when the process is still running" do
88
88
  subject.process do
89
89
  sleep(WAIT_SMALL)
90
+ WAIT_SMALL
90
91
  end
91
92
  subject.dead?.should be false
92
93
 
@@ -46,10 +46,6 @@ describe ZTK::Logger do
46
46
  subject.should be_an_instance_of ZTK::Logger
47
47
  end
48
48
 
49
- it "should provide access to the raw log file handle" do
50
- subject.logdev.should be_an_instance_of File
51
- end
52
-
53
49
  end
54
50
 
55
51
  describe "general logging functionality" do
@@ -85,17 +81,16 @@ describe ZTK::Logger do
85
81
 
86
82
  it "should allow writing directly to the log device" do
87
83
  data = "Hello World"
88
- subject.logdev.write(data)
84
+ subject << data
89
85
  IO.read(@logfile).match(data).should_not be nil
90
86
  end
91
87
 
92
88
  it "should allow us to echo log statements to STDOUT" do
93
89
  data = "Hello World"
94
90
  stdout = StringIO.new
95
-
96
91
  $stdout = stdout
92
+ subject.loggers = [ ::Logger.new($stdout) ]
97
93
 
98
- subject.stdout_echo = true
99
94
  subject.debug { data }
100
95
 
101
96
  stdout.rewind
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ztk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.14.1
4
+ version: 1.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zachary Patten
@@ -288,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
288
288
  version: '0'
289
289
  requirements: []
290
290
  rubyforge_project:
291
- rubygems_version: 2.2.1
291
+ rubygems_version: 2.1.11
292
292
  signing_key:
293
293
  specification_version: 4
294
294
  summary: Zachary's (DevOp) Tool Kit