ztk 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -94,8 +94,12 @@ module ZTK
94
94
 
95
95
  if !block_given?
96
96
  raise BaseError, "You must supply a block to the log method!"
97
- elsif (@config.logger.level == ZTK::Logger.const_get(log_level.to_s.upcase))
98
- @config.logger and @config.logger.instance_variable_get(:@logdev).instance_variable_get(:@dev).write(yield)
97
+ elsif (@config.logger.level <= ZTK::Logger.const_get(log_level.to_s.upcase))
98
+ if @config.logger.respond_to?(:logdev)
99
+ @config.logger.logdev.write(yield)
100
+ else
101
+ @config.logger.instance_variable_get(:@logdev).instance_variable_get(:@dev).write(yield)
102
+ end
99
103
  end
100
104
  end
101
105
 
@@ -64,6 +64,14 @@ module ZTK
64
64
  # cmd = ZTK::Command.new
65
65
  # puts cmd.exec("hostname -f").inspect
66
66
  def exec(command, options={})
67
+
68
+ def log_header(tag)
69
+ count = 8
70
+ sep = ("=" * count)
71
+ header = [sep, "[ #{tag} ]", sep, "[ #{self.inspect} ]", sep, "[ #{tag} ]", sep].join
72
+ "#{header}\n"
73
+ end
74
+
67
75
  options = OpenStruct.new({ :exit_code => 0, :silence => false }.merge(options))
68
76
  log(:debug) { "options(#{options.inspect})" }
69
77
  log(:debug) { "command(#{command.inspect})" }
@@ -94,7 +102,7 @@ module ZTK
94
102
  reader_writer_key = {parent_stdout_reader => :stdout, parent_stderr_reader => :stderr}
95
103
  reader_writer_map = {parent_stdout_reader => @config.stdout, parent_stderr_reader => @config.stderr}
96
104
 
97
- direct_log(:debug) { "===[STARTED]===[STARTED]===[#{self.inspect}]===[STARTED]===[STARTED]===\n" }
105
+ direct_log(:debug) { log_header("STARTED") }
98
106
  loop do
99
107
  break if reader_writer_map.keys.all?{ |reader| reader.eof? }
100
108
 
@@ -106,26 +114,27 @@ module ZTK
106
114
  case reader_writer_key[socket]
107
115
  when :stdout then
108
116
  if !stdout_header
109
- direct_log(:debug) { "===[STDOUT]===[STDOUT]===[#{self.inspect}]===[STDOUT]===[STDOUT]===\n" }
117
+ direct_log(:debug) { log_header("STDOUT") }
110
118
  stdout_header = true
111
119
  stderr_header = false
112
120
  end
113
121
  reader_writer_map[socket].write(data) unless options.silence
122
+ direct_log(:debug) { data }
114
123
 
115
124
  when :stderr then
116
125
  if !stderr_header
117
- direct_log(:debug) { "===[STDERR]===[STDERR]===[#{self.inspect}]===[STDERR]===[STDERR]===\n" }
126
+ direct_log(:warn) { log_header("STDERR") }
118
127
  stderr_header = true
119
128
  stdout_header = false
120
129
  end
121
130
  reader_writer_map[socket].write(data) unless options.silence
131
+ direct_log(:warn) { data }
122
132
  end
123
133
 
124
- direct_log(:debug) { data }
125
134
  output += data
126
135
  end
127
136
  end
128
- direct_log(:debug) { "===[FINISHED]===[FINISHED]===[#{self.inspect}]===[FINISHED]===[FINISHED]===\n" }
137
+ direct_log(:debug) { log_header("STOPPED") }
129
138
 
130
139
  Process.waitpid(pid)
131
140
 
@@ -175,6 +175,14 @@ module ZTK
175
175
  # end
176
176
  # puts ssh.exec("hostname -f").inspect
177
177
  def exec(command, options={})
178
+
179
+ def log_header(tag)
180
+ count = 8
181
+ sep = ("=" * count)
182
+ header = [sep, "[ #{tag} ]", sep, "[ #{self.inspect} ]", sep, "[ #{tag} ]", sep].join
183
+ "#{header}\n"
184
+ end
185
+
178
186
  log(:debug) { "config(#{@config.inspect})" }
179
187
  log(:info) { "exec(#{command.inspect}, #{options.inspect})" }
180
188
 
@@ -190,14 +198,14 @@ module ZTK
190
198
 
191
199
  channel = ssh.open_channel do |chan|
192
200
  log(:debug) { "Channel opened." }
193
- direct_log(:debug) { "===[OPENED]===[OPENED]===[#{self.inspect}]===[OPENED]===[OPENED]===\n" }
201
+ direct_log(:debug) { log_header("OPENED") }
194
202
 
195
203
  chan.exec(command) do |ch, success|
196
204
  raise SSHError, "Could not execute '#{command}'." unless success
197
205
 
198
206
  ch.on_data do |c, data|
199
207
  if !stdout_header
200
- direct_log(:debug) { "===[STDOUT]===[STDOUT]===[#{self.inspect}]===[STDOUT]===[STDOUT]===\n" }
208
+ direct_log(:debug) { log_header("STDOUT") }
201
209
  stdout_header = true
202
210
  stderr_header = false
203
211
  end
@@ -209,11 +217,11 @@ module ZTK
209
217
 
210
218
  ch.on_extended_data do |c, type, data|
211
219
  if !stderr_header
212
- direct_log(:debug) { "===[STDERR]===[STDERR]===[#{self.inspect}]===[STDERR]===[STDERR]===\n" }
220
+ direct_log(:warn) { log_header("STDERR") }
213
221
  stderr_header = true
214
222
  stdout_header = false
215
223
  end
216
- direct_log(:debug) { data }
224
+ direct_log(:warn) { data }
217
225
 
218
226
  @config.stderr.print(data) unless options.silence
219
227
  output += data
@@ -227,7 +235,7 @@ module ZTK
227
235
  end
228
236
  channel.wait
229
237
 
230
- direct_log(:debug) { "===[CLOSED]===[CLOSED]===[#{self.inspect}]===[CLOSED]===[CLOSED]===\n" }
238
+ direct_log(:debug) { log_header("CLOSED") }
231
239
  log(:debug) { "Channel closed." }
232
240
  end
233
241
 
@@ -19,5 +19,5 @@
19
19
  ################################################################################
20
20
 
21
21
  module ZTK
22
- VERSION = "0.2.1" unless const_defined?(:VERSION)
22
+ VERSION = "0.2.2" unless const_defined?(:VERSION)
23
23
  end
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: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -212,7 +212,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
212
212
  version: '0'
213
213
  segments:
214
214
  - 0
215
- hash: -2277072188839223318
215
+ hash: -3237864687113483412
216
216
  required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  none: false
218
218
  requirements:
@@ -221,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
221
  version: '0'
222
222
  segments:
223
223
  - 0
224
- hash: -2277072188839223318
224
+ hash: -3237864687113483412
225
225
  requirements: []
226
226
  rubyforge_project:
227
227
  rubygems_version: 1.8.24