Class: Overcommit::Logger

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

Overview

Encapsulates all communication to an output source.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out) ⇒ Logger

Creates a logger that will write to the given output stream.

Parameters:

  • out (IO)


14
15
16
17
18
19
20
21
22
# File 'lib/overcommit/logger.rb', line 14

def initialize(out)
  @out = out
  @colorize =
    if ENV.key?('OVERCOMMIT_COLOR')
      !%w[0 false no].include?(ENV['OVERCOMMIT_COLOR'])
    else
      @out.tty?
    end
end

Class Method Details

.silentObject

Helper for creating a logger which outputs nothing.



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

def self.silent
  new(File.open(File::NULL, 'w'))
end

Instance Method Details

#bold(*args) ⇒ Object

Write a line of output that is intended to be emphasized.



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

def bold(*args)
  color('1', *args)
end

#bold_error(*args) ⇒ Object

Write a line of output indicating a problem or error which is emphasized over a regular problem or error.



63
64
65
# File 'lib/overcommit/logger.rb', line 63

def bold_error(*args)
  color('1;31', *args)
end

#bold_warning(*args) ⇒ Object

Write a line of output indicating a potential cause for concern, but with greater emphasize compared to other warnings.



80
81
82
# File 'lib/overcommit/logger.rb', line 80

def bold_warning(*args)
  color('1;33', *args)
end

#debug(*args) ⇒ Object

Write a line of output if debug mode is enabled.



47
48
49
# File 'lib/overcommit/logger.rb', line 47

def debug(*args)
  color('35', *args) unless ENV.fetch('OVERCOMMIT_DEBUG') { '' }.empty?
end

#error(*args) ⇒ Object

Write a line of output indicating a problem or error.



57
58
59
# File 'lib/overcommit/logger.rb', line 57

def error(*args)
  color(31, *args)
end

#flushObject

Flushes the [IO] object for partial lines



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

def flush
  @out.flush if @out.respond_to? :flush
end

#log(*args) ⇒ Object

Write a line of output.

A newline character will always be appended.



42
43
44
# File 'lib/overcommit/logger.rb', line 42

def log(*args)
  @out.puts(*args)
end

#newlineObject

Prints a newline character (alias for readability).



30
31
32
# File 'lib/overcommit/logger.rb', line 30

def newline
  log
end

#partial(*args) ⇒ Object

Write output without a trailing newline.



25
26
27
# File 'lib/overcommit/logger.rb', line 25

def partial(*args)
  @out.print(*args)
end

#success(*args) ⇒ Object

Write a line of output indicating a successful or noteworthy event.



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

def success(*args)
  color(32, *args)
end

#warning(*args) ⇒ Object

Write a line of output indicating a potential cause for concern, but not an actual error.



74
75
76
# File 'lib/overcommit/logger.rb', line 74

def warning(*args)
  color(33, *args)
end