Class: NicInfo::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/nicinfo/nicinfo_logger.rb

Overview

A logger for this application.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nicinfo/nicinfo_logger.rb', line 81

def initialize

  @message_level = MessageLevel::SOME_MESSAGES
  @data_amount = DataAmount::NORMAL_DATA
  @color_scheme = ColorScheme::DARK
  @message_out = $stdout
  @data_out = $stdout
  @rainbow = Rainbow.new
  @item_name_length = 25
  @item_name_rjust = true
  @prose_name_length = 10
  @prose_name_rjust = true

  @message_last_written_to = false
  @data_last_written_to = false

  return if Gem.win_platform?
  #else
  @columns = get_terminal_columns( `stty -a`, @default_width )
end

Instance Attribute Details

#auto_wrapObject

Returns the value of attribute auto_wrap.



78
79
80
# File 'lib/nicinfo/nicinfo_logger.rb', line 78

def auto_wrap
  @auto_wrap
end

#color_schemeObject

Returns the value of attribute color_scheme.



79
80
81
# File 'lib/nicinfo/nicinfo_logger.rb', line 79

def color_scheme
  @color_scheme
end

#data_amountObject

Returns the value of attribute data_amount.



77
78
79
# File 'lib/nicinfo/nicinfo_logger.rb', line 77

def data_amount
  @data_amount
end

#data_outObject

Returns the value of attribute data_out.



77
78
79
# File 'lib/nicinfo/nicinfo_logger.rb', line 77

def data_out
  @data_out
end

#default_widthObject

Returns the value of attribute default_width.



78
79
80
# File 'lib/nicinfo/nicinfo_logger.rb', line 78

def default_width
  @default_width
end

#detect_widthObject

Returns the value of attribute detect_width.



78
79
80
# File 'lib/nicinfo/nicinfo_logger.rb', line 78

def detect_width
  @detect_width
end

#is_less_availableObject

Returns the value of attribute is_less_available.



79
80
81
# File 'lib/nicinfo/nicinfo_logger.rb', line 79

def is_less_available
  @is_less_available
end

#item_name_lengthObject

Returns the value of attribute item_name_length.



77
78
79
# File 'lib/nicinfo/nicinfo_logger.rb', line 77

def item_name_length
  @item_name_length
end

#item_name_rjustObject

Returns the value of attribute item_name_rjust.



77
78
79
# File 'lib/nicinfo/nicinfo_logger.rb', line 77

def item_name_rjust
  @item_name_rjust
end

#message_levelObject

Returns the value of attribute message_level.



77
78
79
# File 'lib/nicinfo/nicinfo_logger.rb', line 77

def message_level
  @message_level
end

#message_outObject

Returns the value of attribute message_out.



77
78
79
# File 'lib/nicinfo/nicinfo_logger.rb', line 77

def message_out
  @message_out
end

#pagerObject

Returns the value of attribute pager.



77
78
79
# File 'lib/nicinfo/nicinfo_logger.rb', line 77

def pager
  @pager
end

#prose_name_lengthObject

Returns the value of attribute prose_name_length.



78
79
80
# File 'lib/nicinfo/nicinfo_logger.rb', line 78

def prose_name_length
  @prose_name_length
end

#prose_name_rjustObject

Returns the value of attribute prose_name_rjust.



78
79
80
# File 'lib/nicinfo/nicinfo_logger.rb', line 78

def prose_name_rjust
  @prose_name_rjust
end

#rainbowObject

Returns the value of attribute rainbow.



79
80
81
# File 'lib/nicinfo/nicinfo_logger.rb', line 79

def rainbow
  @rainbow
end

Instance Method Details

#break_up_line(line, width) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/nicinfo/nicinfo_logger.rb', line 339

def break_up_line line, width
  retval = Array.new
  i = line.rindex( /\s/, width )
  if i == nil
    i = line.rindex( /\s/ )
  end
  while i != nil do
    retval << line[ 0, i ]
    line = line[ i+1..-1 ]
    i = line.rindex( /\s/, width )
    if i == nil
      i = line.rindex( /\s/ )
    end
  end
  if line != nil
    if retval.length > 0 && retval.last.length + line.length + 1 <= width
      retval.last << " " + line
    else
      retval << line
    end
  end
  return retval
end

#data_title(title, attention_type = nil) ⇒ Object



187
188
189
190
191
# File 'lib/nicinfo/nicinfo_logger.rb', line 187

def data_title title, attention_type = nil
  validate_data_amount()
  log_just title, " ", @item_name_length, @item_name_rjust, "", attention_type
  return true
end

#datum(item_name, item_value, attention_type = nil) ⇒ Object

Outputs a data at :NORMAL_DATA level



169
170
171
172
173
174
175
176
# File 'lib/nicinfo/nicinfo_logger.rb', line 169

def datum item_name, item_value, attention_type = nil
  validate_data_amount()
  if (@data_amount != DataAmount::TERSE_DATA)
    log_data(item_name, item_value, attention_type )
    return true
  end
  return false
end

#end_data_itemObject



133
134
135
# File 'lib/nicinfo/nicinfo_logger.rb', line 133

def end_data_item
  #do nothing for now
end

#end_runObject



137
138
139
# File 'lib/nicinfo/nicinfo_logger.rb', line 137

def end_run
  start_data_item
end

#extra(item_name, item_value, attention_type = nil) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/nicinfo/nicinfo_logger.rb', line 178

def extra item_name, item_value, attention_type = nil
  validate_data_amount()
  if (@data_amount != DataAmount::TERSE_DATA && @data_amount != DataAmount::NORMAL_DATA)
    log_data(item_name, item_value, attention_type )
    return true
  end
  return false
end

#get_pagerObject



291
292
293
# File 'lib/nicinfo/nicinfo_logger.rb', line 291

def get_pager
  ENV['PAGER'] || is_less_available? || 'more'
end

#get_terminal_columns(stty_output, default_columns) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/nicinfo/nicinfo_logger.rb', line 325

def get_terminal_columns stty_output, default_columns
  rx1 = /\s*columns\s*=\s*(\d*);/
  m = rx1.match( stty_output )
  return m[ 1 ].to_i if m
  #else
  rx2 = /\s*(\d*)\s*columns;/
  m = rx2.match( stty_output )
  return m[ 1 ].to_i if m
  rx3 = /\s*columns\s*(\d*);/
  m = rx3.match( stty_output )
  return m[ 1 ].to_i if m
  return default_columns
end

#get_widthObject



102
103
104
105
106
# File 'lib/nicinfo/nicinfo_logger.rb', line 102

def get_width
  return @default_width if !@detect_width
  return @columns if @columns != nil
  return @default_width
end

#info(data_amount, item_name, item_value, attention_type = nil) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/nicinfo/nicinfo_logger.rb', line 193

def info data_amount, item_name, item_value, attention_type = nil
  retval = false
  validate_data_amount()
  case data_amount
    when DataAmount::TERSE_DATA
      log_data(item_name, item_value, attention_type )
      retval = true
    when DataAmount::NORMAL_DATA
      if (@data_amount != DataAmount::TERSE_DATA)
        log_data( item_name, item_value, attention_type )
        retval = true
      end
    when DataAmount::EXTRA_DATA
      if (@data_amount != DataAmount::TERSE_DATA && @data_amount != DataAmount::NORMAL_DATA)
        log_data( item_name, item_value, attention_type )
        retval = true
      end
  end
  return retval
end

#is_less_available?Boolean

Returns:

  • (Boolean)


277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/nicinfo/nicinfo_logger.rb', line 277

def is_less_available?
  if @is_less_available == nil
    avail = ENV['PATH'].split(File::PATH_SEPARATOR).any? do |dir|
      File.executable?(File.join(dir, "less"))
    end
    if avail
      @is_less_available = "less"
    else
      @is_less_available = false
    end
  end
  return @is_less_available
end

#log_tree_item(data_amount, tree_item, attention_type = nil) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/nicinfo/nicinfo_logger.rb', line 256

def log_tree_item data_amount, tree_item, attention_type = nil
  retval = false
  validate_data_amount()
  case data_amount
    when DataAmount::TERSE_DATA
      log_raw(tree_item, true, attention_type )
      retval = true
    when DataAmount::NORMAL_DATA
      if (@data_amount != DataAmount::TERSE_DATA)
        log_raw(tree_item, true, attention_type )
        retval = true
      end
    when DataAmount::EXTRA_DATA
      if (@data_amount != DataAmount::TERSE_DATA && @data_amount != DataAmount::NORMAL_DATA)
        log_raw(tree_item, true, attention_type )
        retval = true
      end
  end
  return retval
end

#mesg(message, attention_type = nil) ⇒ Object

Outputs at the :SOME_MESSAGES level



142
143
144
145
146
147
148
149
# File 'lib/nicinfo/nicinfo_logger.rb', line 142

def mesg message, attention_type = nil
  validate_message_level()
  if (@message_level != MessageLevel::NO_MESSAGES)
    log_info("# " + message.to_s, attention_type )
    return true
  end
  return false
end

#prose(data_amount, prose_name, prose_value, attention_type = nil) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/nicinfo/nicinfo_logger.rb', line 235

def prose data_amount, prose_name, prose_value, attention_type = nil
  retval = false
  validate_data_amount()
  case data_amount
    when DataAmount::TERSE_DATA
      log_prose prose_name, prose_value, attention_type
      retval = true
    when DataAmount::NORMAL_DATA
      if (@data_amount != DataAmount::TERSE_DATA)
        log_prose prose_name, prose_value, attention_type
        retval = true
      end
    when DataAmount::EXTRA_DATA
      if (@data_amount != DataAmount::TERSE_DATA && @data_amount != DataAmount::NORMAL_DATA)
        log_prose prose_name, prose_value, attention_type
        retval = true
      end
  end
  return retval
end

#raw(data_amount, raw_data, wrap = true, attention_type = nil) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/nicinfo/nicinfo_logger.rb', line 214

def raw data_amount, raw_data, wrap = true, attention_type = nil
  retval = false
  validate_data_amount()
  case data_amount
    when DataAmount::TERSE_DATA
      log_raw(raw_data, wrap, attention_type )
      retval = true
    when DataAmount::NORMAL_DATA
      if (@data_amount != DataAmount::TERSE_DATA)
        log_raw(raw_data, wrap, attention_type )
        retval = true
      end
    when DataAmount::EXTRA_DATA
      if (@data_amount != DataAmount::TERSE_DATA && @data_amount != DataAmount::NORMAL_DATA)
        log_raw(raw_data, wrap, attention_type )
        retval = true
      end
  end
  return retval
end

#run_pagerObject



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/nicinfo/nicinfo_logger.rb', line 296

def run_pager
  return unless @pager
  return if Gem.win_platform?
  return unless STDOUT.tty?

  @color_scheme = ColorScheme::NONE unless get_pager == "less"

  read, write = IO.pipe

  unless Kernel.fork # Child process
    STDOUT.reopen(write)
    STDERR.reopen(write) if STDERR.tty?
    read.close
    write.close
    return
  end

  # Parent process, become pager
  STDIN.reopen(read)
  read.close
  write.close

  ENV['LESS'] = 'FSRX' # Don't page if the input is short enough

  Kernel.select [STDIN] # Wait until we have input before we start the pager
  pager = get_pager
  exec pager rescue exec "/bin/sh", "-c", pager
end

#start_data_itemObject



123
124
125
126
127
128
129
130
131
# File 'lib/nicinfo/nicinfo_logger.rb', line 123

def start_data_item
  if (@data_last_written_to)
    @data_out.puts
  elsif (@data_out == $stdout && @message_out == $stderr && @message_last_written_to)
    @data_out.puts
  elsif (@data_out == @message_out && @message_last_written_to)
    @data_out.puts
  end
end

#terse(item_name, item_value, attention_type = nil) ⇒ Object

Outputs a datum at :TERSE_DATA level



162
163
164
165
166
# File 'lib/nicinfo/nicinfo_logger.rb', line 162

def terse item_name, item_value, attention_type = nil
  validate_data_amount()
  log_data(item_name, item_value, attention_type )
  return true
end

#trace(message, attention_type = nil) ⇒ Object

Outputs at the :ALL_MESSAGES level



152
153
154
155
156
157
158
159
# File 'lib/nicinfo/nicinfo_logger.rb', line 152

def trace message, attention_type = nil
  validate_message_level()
  if (@message_level != MessageLevel::NO_MESSAGES && @message_level != MessageLevel::SOME_MESSAGES)
    log_info("## " + message.to_s, attention_type )
    return true
  end
  return false
end

#validate_color_schemeObject

Raises:

  • (ArgumentError)


118
119
120
121
# File 'lib/nicinfo/nicinfo_logger.rb', line 118

def validate_color_scheme
  raise ArgumentError, "Color scheme not defined" if @color_scheme == nil
  raise ArgumentError, "Unknown color scheme '" + @color_scheme.to_s + "'" unless ColorScheme.has_value?(@color_scheme.to_s)
end

#validate_data_amountObject

Raises:

  • (ArgumentError)


113
114
115
116
# File 'lib/nicinfo/nicinfo_logger.rb', line 113

def validate_data_amount
  raise ArgumentError, "Data log level not defined" if @data_amount == nil
  raise ArgumentError, "Unknown data log level '" + @data_amount.to_s + "'" unless DataAmount.has_value?(@data_amount.to_s)
end

#validate_message_levelObject

Raises:

  • (ArgumentError)


108
109
110
111
# File 'lib/nicinfo/nicinfo_logger.rb', line 108

def validate_message_level
  raise ArgumentError, "Message log level not defined" if @message_level == nil
  raise ArgumentError, "Unknown message log level '" + @message_level.to_s + "'" unless MessageLevel.has_value?(@message_level.to_s)
end