Class: RuntimeCommand::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/runtime_command/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Object

Returns RuntimeCommand::Logger.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • colors (Hash)
  • output (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/runtime_command/logger.rb', line 11

def initialize(options = {})
  @options = options
  @has_color = options[:colors] != :none

  if @has_color
    @stdin_color = options.dig(:colors, :stdin) || HighLine::Style.rgb(204, 204, 0)
    @stdout_color = options.dig(:colors, :stdout) || HighLine::Style.rgb(64, 64, 64)
    @stderr_color = options.dig(:colors, :stderr) || HighLine::Style.rgb(255, 51, 51)
  end

  flash
end

Instance Attribute Details

#buffered_logObject (readonly)

Returns the value of attribute buffered_log.



5
6
7
# File 'lib/runtime_command/logger.rb', line 5

def buffered_log
  @buffered_log
end

#buffered_stderrObject (readonly)

Returns the value of attribute buffered_stderr.



5
6
7
# File 'lib/runtime_command/logger.rb', line 5

def buffered_stderr
  @buffered_stderr
end

#buffered_stdoutObject (readonly)

Returns the value of attribute buffered_stdout.



5
6
7
# File 'lib/runtime_command/logger.rb', line 5

def buffered_stdout
  @buffered_stdout
end

Instance Method Details

#flashObject



68
69
70
71
72
73
74
# File 'lib/runtime_command/logger.rb', line 68

def flash
  @buffered_log = ''
  @buffered_stdout = ''
  @buffered_stderr = ''

  nil
end

#stderr(line) ⇒ Object

Parameters:

  • line (String)


57
58
59
60
61
62
63
64
65
66
# File 'lib/runtime_command/logger.rb', line 57

def stderr(line)
  trim_line = line.chomp
  puts @has_color ? HighLine.color(trim_line, @stderr_color) : line if @options[:output]

  @options[:logger].error(trim_line) if @options[:logger]
  @buffered_log << line
  @buffered_stderr << line

  nil
end

#stderr?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/runtime_command/logger.rb', line 52

def stderr?
  !@buffered_stderr.empty?
end

#stdin(line) ⇒ Object

Parameters:

  • line (String)


25
26
27
28
29
30
31
32
# File 'lib/runtime_command/logger.rb', line 25

def stdin(line)
  puts @has_color ? HighLine.color(line, @stdin_color) : line if @options[:output]

  @options[:logger].info(line) if @options[:logger]
  @buffered_log << line + "\n"

  nil
end

#stdout(line) ⇒ Object

Parameters:

  • line (String)


40
41
42
43
44
45
46
47
48
49
# File 'lib/runtime_command/logger.rb', line 40

def stdout(line)
  trim_line = line.chomp
  puts @has_color ? HighLine.color(trim_line, @stdout_color) : line if @options[:output]

  @options[:logger].info(trim_line) if @options[:logger]
  @buffered_log << line
  @buffered_stdout << line

  nil
end

#stdout?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/runtime_command/logger.rb', line 35

def stdout?
  !@buffered_stdout.empty?
end