Class: Rundock::Logger::Formatter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Formatter

Returns a new instance of Formatter.



39
40
41
42
43
44
45
# File 'lib/rundock/logger.rb', line 39

def initialize(*args)
  super
  @indent_depth = 0
  @buffer = []
  @rec = false
  @lock = false
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



37
38
39
# File 'lib/rundock/logger.rb', line 37

def buffer
  @buffer
end

#colorObject

Returns the value of attribute color.



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

def color
  @color
end

#coloredObject

Returns the value of attribute colored.



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

def colored
  @colored
end

#date_headerObject

Returns the value of attribute date_header.



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

def date_header
  @date_header
end

#indent_depthObject

Returns the value of attribute indent_depth.



31
32
33
# File 'lib/rundock/logger.rb', line 31

def indent_depth
  @indent_depth
end

#short_headerObject

Returns the value of attribute short_header.



34
35
36
# File 'lib/rundock/logger.rb', line 34

def short_header
  @short_header
end

#show_headerObject

Returns the value of attribute show_header.



33
34
35
# File 'lib/rundock/logger.rb', line 33

def show_header
  @show_header
end

#suppress_loggingObject

Returns the value of attribute suppress_logging.



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

def suppress_logging
  @suppress_logging
end

Instance Method Details

#add_indentObject



65
66
67
# File 'lib/rundock/logger.rb', line 65

def add_indent
  @indent_depth += 1
end

#call(severity, datetime, progname, msg) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/rundock/logger.rb', line 47

def call(severity, datetime, progname, msg)
  out = formatted_message(severity, datetime, progname, msg)
  @buffer << LogEntity.new(severity, datetime, progname, msg, indent_depth, self) if @rec

  if colored
    colorize(out, severity)
  else
    out
  end
end

#flushObject



81
82
83
84
85
86
# File 'lib/rundock/logger.rb', line 81

def flush
  return nil if @lock
  ret = @buffer.dup
  @buffer.clear
  ret
end

#formatted_message(severity, datetime, progname, msg) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rundock/logger.rb', line 108

def formatted_message(severity, datetime, progname, msg)
  out = if @suppress_logging
        elsif !@show_header
          "%s\n" % [msg2str(msg)]
        elsif !@date_header
          "%5s: %s%s\n" % [
            severity,
            ' ' * 2 * indent_depth,
            msg2str(msg)
          ]
        elsif @short_header
          "%s: %s%s\n" % [severity[0, 1], ' ' * 2 * indent_depth, msg2str(msg)]
        else
          "[%s] %5s: %s%s\n" % [
            datetime.strftime('%Y-%m-%dT%H:%M:%S.%L'),
            severity,
            ' ' * 2 * indent_depth,
            msg2str(msg)
          ]
        end

  out
end

#indentObject



58
59
60
61
62
63
# File 'lib/rundock/logger.rb', line 58

def indent
  add_indent
  yield
ensure
  reduce_indent
end

#new_color(code) ⇒ Object



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

def new_color(code)
  prev_color = @color
  @color = code
  yield
ensure
  @color = prev_color
end

#off_recObject



92
93
94
# File 'lib/rundock/logger.rb', line 92

def off_rec
  @rec = false unless @lock
end

#on_recObject



88
89
90
# File 'lib/rundock/logger.rb', line 88

def on_rec
  @rec = true unless @lock
end

#rec_lockObject



96
97
98
# File 'lib/rundock/logger.rb', line 96

def rec_lock
  @lock = true
end

#rec_unlockObject



100
101
102
# File 'lib/rundock/logger.rb', line 100

def rec_unlock
  @lock = false
end

#reduce_indentObject



69
70
71
# File 'lib/rundock/logger.rb', line 69

def reduce_indent
  @indent_depth -= 1 if @indent_depth > 0
end

#simple_output(msg) ⇒ Object



104
105
106
# File 'lib/rundock/logger.rb', line 104

def simple_output(msg)
  puts msg2str(msg)
end