Class: LogBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/debugtrace/log_buffer.rb

Overview

Buffers logs.

Defined Under Namespace

Classes: LevelAndLog

Instance Method Summary collapse

Constructor Details

#initialize(maximum_data_output_width) ⇒ LogBuffer

Initializes this object.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/debugtrace/log_buffer.rb', line 28

def initialize(maximum_data_output_width)
  @maximum_data_output_width = Common.check_type('maximum_data_output_width', maximum_data_output_width, Integer)
  @nest_level = 0
  @append_nest_level = 0

  # tuples of data indentation level && log string
  @lines = []

  # buffer for a line of logs
  @last_line = ""
end

Instance Method Details

#append(value, nest_level = 0, no_break = false) ⇒ Object

Appends a string representation of the value.

Parameters:

  • value (Object)

    : The value to append

  • nest_level (int, optional) (defaults to: 0)

    : The nest level of the value. Defaults to 0

  • no_break (bool, optional) (defaults to: false)

    : If true, does not break even if the maximum width is exceeded. Defaults to false

Returns:

  • LogBuffer: This object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/debugtrace/log_buffer.rb', line 63

def append(value, nest_level = 0, no_break = false)
  Common.check_type('nest_level', nest_level, Integer)
  Common.check_type('no_break', no_break, TrueClass)
  if value != nil
    string = value.to_s
    if !no_break && length > 0 && length + string.length > @maximum_data_output_width
      line_feed()
    end
    @append_nest_level = nest_level
    @last_line += string
  end
  self
end

#append_buffer(separator, buff) ⇒ Object

Appends lines of another LogBuffer.

Parameters:

  • separator (String)

    : The separator string to append if not “”

  • buff (LogBuffer)

    : Another LogBuffer



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/debugtrace/log_buffer.rb', line 90

def append_buffer(separator, buff)
  Common.check_type('separator', separator, String)
  Common.check_type('buff', buff, LogBuffer)
  if separator != ""
    append(separator, 0, true)
  end
  index = 0
  for line in buff.lines
    if index > 0
      line_feed()
    end
    append(line.nest_level, line.log, index == 0 && separator != "")
    index += 1
  end
  self
end

#down_nestObject

Downs the data nest level.



53
54
55
# File 'lib/debugtrace/log_buffer.rb', line 53

def down_nest
  @nest_level -= 1
end

#lengthObject

The length of the last line.



108
109
110
# File 'lib/debugtrace/log_buffer.rb', line 108

def length
  @last_line.length
end

#line_feedObject

Breaks the current line.



41
42
43
44
45
# File 'lib/debugtrace/log_buffer.rb', line 41

def line_feed
  @lines << LevelAndLog.new(@nest_level + @append_nest_level, @last_line.rstrip())
  @append_nest_level = 0
  @last_line = ''
end

#linesObject

A list of tuple of data indentation level && log string.



118
119
120
121
122
123
124
# File 'lib/debugtrace/log_buffer.rb', line 118

def lines
  lines = @lines.dup
  if length > 0
    lines << LevelAndLog.new(@nest_level, @last_line)
  end
  lines
end

#multi_lines?Boolean

true if multiple line, false otherwise.

Returns:

  • (Boolean)


113
114
115
# File 'lib/debugtrace/log_buffer.rb', line 113

def multi_lines?
  @lines.length > 1 || @lines.length == 1 && length > 0
end

#no_break_append(value) ⇒ Object

Appends a string representation of the value. Does not break even if the maximum width is exceeded.

Parameters:

  • value (Object)

    : The value to append

Returns:

  • LogBuffer: This object



81
82
83
# File 'lib/debugtrace/log_buffer.rb', line 81

def no_break_append(value)
  append(value, 0, true)
end

#up_nestObject

Ups the data nest level.



48
49
50
# File 'lib/debugtrace/log_buffer.rb', line 48

def up_nest
  @nest_level += 1
end