Class: Crew::Logger

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

Instance Method Summary collapse

Constructor Details

#initialize(mute = false) ⇒ Logger

Returns a new instance of Logger.



3
4
5
6
7
# File 'lib/crew/logger.rb', line 3

def initialize(mute = false)
  @indent = 0
  @mute = mute
  writer.sync = true
end

Instance Method Details

#cd(dir) ⇒ Object



27
28
29
# File 'lib/crew/logger.rb', line 27

def cd(dir)
  log "cd #{dir}".color("#aaaaaa")
end

#context(name) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/crew/logger.rb', line 97

def context(name)
  log "--> [#{name.color(:cyan)}] starting"
  start_time = Time.new.to_f
  indent do
    yield
  end
  end_time = Time.new.to_f
  log "<-- [#{name.color(:cyan)}] ended in %0.2f seconds" % [end_time - start_time]
end

#fail_test(name) ⇒ Object



89
90
91
# File 'lib/crew/logger.rb', line 89

def fail_test(name)
  log "#{''.color(:red)} Failing test #{name.inverse}"
end

#indentObject



116
117
118
119
120
121
# File 'lib/crew/logger.rb', line 116

def indent
  @indent += 1
  yield
ensure
  @indent -= 1
end

#info(text) ⇒ Object



19
20
21
# File 'lib/crew/logger.rb', line 19

def info(text)
  log text
end

#log(line) ⇒ Object



107
108
109
110
# File 'lib/crew/logger.rb', line 107

def log(line)
  writer.write "  " * @indent
  writer.puts line
end

#mutedObject



9
10
11
12
13
14
15
16
17
# File 'lib/crew/logger.rb', line 9

def muted
  original_mute = @mute
  @mute = true
  begin
    yield
  ensure
    @mute = original_mute
  end
end

#no_test(name) ⇒ Object



93
94
95
# File 'lib/crew/logger.rb', line 93

def no_test(name)
  log "#{''.color(:blue)} No tests for #{name}"
end

#pass_test(name) ⇒ Object



81
82
83
# File 'lib/crew/logger.rb', line 81

def pass_test(name)
  log "#{''.color(:green)} Passed test #{name.inverse}"
end

#set_logging(enabled) ⇒ Object



112
113
114
# File 'lib/crew/logger.rb', line 112

def set_logging(enabled)
  @logging = enabled
end

#sh(command) ⇒ Object



23
24
25
# File 'lib/crew/logger.rb', line 23

def sh(command)
  log "#{'$'.color(:blue)} #{command}"
end

#skip_test(name) ⇒ Object



85
86
87
# File 'lib/crew/logger.rb', line 85

def skip_test(name)
  log "#{'/'.color(:yellow)} Skipping test #{name.inverse}"
end

#spinner(label) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/crew/logger.rb', line 31

def spinner(label)
  muted do
    $stderr.write "  " * @indent
    index = 0
    states = %w(   )
    spin = proc do |val|
      if index > 0
        $stderr.write "\b" * (label.size + 2)
      end
      if val
        $stderr.write "* ".color(:green)
        $stderr.puts label
      else
        $stderr.write states[index % states.size].color(:yellow)
        $stderr.write " #{label}"
        index += 1
      end
    end
    spin[false]
    spin
  end
end

#task(command, args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/crew/logger.rb', line 54

def task(command, args)
  log "#{command} #{args.to_s}".color("#aaaaaa")
  out = nil
  begin
    indent do
      out = yield
    end
    pass_line = "#{''.color(:green)} #{command}"
    pass_line << " #{'# =>'.color(:magenta)} #{out.inspect}"
    log pass_line
    out
  rescue AssertionError
    log "#{''.color(:red)} #{command}"
    raise
  rescue => e
    log "#{'!!!'.color(:blue)} #{command} ([#{e.class}] #{e.message})"
    raise
  end
end

#test(name) ⇒ Object



74
75
76
77
78
79
# File 'lib/crew/logger.rb', line 74

def test(name)
  log "Testing #{name.inverse}"
  indent do
    yield
  end
end

#writerObject



123
124
125
# File 'lib/crew/logger.rb', line 123

def writer
  @mute ? StringIO.new : $stderr
end