Class: Inprovise::Logger

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

Overview

Logger for Inprovise

Author

Martin Corino

License

Distributes under the same license as Ruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, task) ⇒ Logger

Returns a new instance of Logger.



10
11
12
13
14
# File 'lib/inprovise/logger.rb', line 10

def initialize(node, task)
  @node = node
  @nl = true
  set_task(task)
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node.



7
8
9
# File 'lib/inprovise/logger.rb', line 7

def node
  @node
end

#taskObject (readonly)

Returns the value of attribute task.



8
9
10
# File 'lib/inprovise/logger.rb', line 8

def task
  @task
end

Instance Method Details

#cached(cmd) ⇒ Object



44
45
46
# File 'lib/inprovise/logger.rb', line 44

def cached(cmd)
  execute(cmd)
end

#clone_for_node(node) ⇒ Object



16
17
18
19
20
# File 'lib/inprovise/logger.rb', line 16

def clone_for_node(node)
  copy = self.dup
  copy.node = node
  copy
end

#command(msg) ⇒ Object



28
29
30
# File 'lib/inprovise/logger.rb', line 28

def command(msg)
  say(msg, :yellow)
end

#execute(cmd) ⇒ Object



36
37
38
# File 'lib/inprovise/logger.rb', line 36

def execute(cmd)
  say(cmd, :cyan)
end

#local(cmd) ⇒ Object



32
33
34
# File 'lib/inprovise/logger.rb', line 32

def local(cmd)
  say(cmd, :bold)
end

#log(msg) ⇒ Object



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

def log(msg)
  say(msg)
end

#mock_execute(cmd) ⇒ Object



40
41
42
# File 'lib/inprovise/logger.rb', line 40

def mock_execute(cmd)
  execute(cmd)
end


56
57
58
59
60
61
62
# File 'lib/inprovise/logger.rb', line 56

def print(msg)
  Thread.exclusive do
    $stdout.print "#{@node.to_s} [#{@task.bold}] " if @nl
    $stdout.print msg.sub("\r", "\r".to_eol << "#{@node.to_s} [#{@task.bold}] ")
  end
  @nl = false
end

#remote(cmd) ⇒ Object



48
49
50
# File 'lib/inprovise/logger.rb', line 48

def remote(cmd)
  say(cmd, :blue)
end

#say(msg, color = nil, stream = $stdout) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/inprovise/logger.rb', line 72

def say(msg, color=nil, stream=$stdout)
  msg.to_s.split("\n").each do |line|
    out = color ? line.send(color) : line
    Thread.exclusive { stream.puts unless @nl; stream.puts "#{@node.to_s} [#{@task.bold}] #{out}" }
    @nl = true
  end
end

#set_task(task) ⇒ Object



22
23
24
25
26
# File 'lib/inprovise/logger.rb', line 22

def set_task(task)
  oldtask = @task
  @task = task.to_s
  oldtask
end

#stderr(msg, force = false) ⇒ Object



68
69
70
# File 'lib/inprovise/logger.rb', line 68

def stderr(msg, force=false)
  say(msg, :red, $stderr) if force || Inprovise.verbosity>0
end

#stdout(msg, force = false) ⇒ Object



64
65
66
# File 'lib/inprovise/logger.rb', line 64

def stdout(msg, force=false)
  say(msg, :green) if force || Inprovise.verbosity>0
end