Class: Vedeu::Logging::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/logging/log.rb

Overview

Provides the ability to log anything to the Vedeu log file.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.countFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Used by tests to access the count instance variable.

Returns:

  • (Fixnum)

    Used by tests to access the count instance variable.



23
24
25
# File 'lib/vedeu/logging/log.rb', line 23

def count
  @count
end

Class Method Details

.colours(type) ⇒ Array<Symbol> (private)

Parameters:

  • type (Symbol)

    The type of log message.

Returns:

  • (Array<Symbol>)


120
121
122
# File 'lib/vedeu/logging/log.rb', line 120

def colours(type)
  Vedeu::LOG_TYPES.fetch(type, [:default, :default])
end

.formatted_message(message) ⇒ String (private)

Parameters:

  • message (String)

    The message you wish to emit, useful for debugging.

Returns:

  • (String)


127
128
129
# File 'lib/vedeu/logging/log.rb', line 127

def formatted_message(message)
  "#{timestamp}#{message}\n" if message
end

.indent(&block) ⇒ NilClass|void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used by Events::Trigger to indent log messages to show activity which occurs as part of that event triggering.

Parameters:

  • block (Proc)

Returns:

  • (NilClass|void)


31
32
33
34
35
36
37
38
# File 'lib/vedeu/logging/log.rb', line 31

def indent(&block)
  @count ||= 0
  @count += 1

  yield if block_given?
ensure
  outdent
end

.indentationString (private)

Returns:

  • (String)


132
133
134
# File 'lib/vedeu/logging/log.rb', line 132

def indentation
  ' ' * (@count ||= 0) * 2
end

.log(message:, type: :info) ⇒ String

Write a message to the Vedeu log file.

Examples:

Vedeu.log(type:    :debug,
          message: 'A useful debugging message: Error!')

Parameters:

  • message (String)

    The message you wish to emit, useful for debugging.

  • type (Symbol) (defaults to: :info)

    Colour code messages in the log file depending on their source. See Vedeu::LOG_TYPES

Returns:

  • (String)


51
52
53
54
55
56
57
# File 'lib/vedeu/logging/log.rb', line 51

def log(message:, type: :info)
  if Vedeu.config.loggable?(type)
    output = log_entry(type, message)
    logger.debug(output)
    output
  end
end

.log_entry(type, message) ⇒ String (private)

Returns the message:

[type] message

Parameters:

  • type (Symbol)

    The type of log message.

  • message (String)

    The message you wish to emit, useful for debugging.

Returns:

  • (String)


143
144
145
# File 'lib/vedeu/logging/log.rb', line 143

def log_entry(type, message)
  log_type(type) + log_message(type, message)
end

.log_message(type, message) ⇒ String (private)

Parameters:

  • type (Symbol)

    The type of log message.

  • message (String)

    The message you wish to emit, useful for debugging.

Returns:

  • (String)


156
157
158
# File 'lib/vedeu/logging/log.rb', line 156

def log_message(type, message)
  Vedeu.esc.colour(colours(type)[1]) { indentation + message }
end

.log_stderr(message:, type: :error) ⇒ String

Parameters:

  • message (String)

    The message you wish to emit, useful for debugging.

  • type (Symbol) (defaults to: :error)

    See Vedeu::LOG_TYPES for valid values.

Returns:

  • (String)


75
76
77
78
79
# File 'lib/vedeu/logging/log.rb', line 75

def log_stderr(message:, type: :error)
  log(message: message, type: type)

  $stderr.puts log_entry(type, message)
end

.log_stdout(message:, type: :info) ⇒ String

Parameters:

  • message (String)

    The message you wish to emit, useful for debugging.

  • type (Symbol) (defaults to: :info)

    See Vedeu::LOG_TYPES for valid values.

Returns:

  • (String)


64
65
66
67
68
# File 'lib/vedeu/logging/log.rb', line 64

def log_stdout(message:, type: :info)
  log(message: message, type: type)

  $stdout.puts log_entry(type, message)
end

.log_type(type) ⇒ String (private)

Parameters:

  • type (Symbol)

    The type of log message.

Returns:

  • (String)


149
150
151
# File 'lib/vedeu/logging/log.rb', line 149

def log_type(type)
  Vedeu.esc.colour(colours(type)[0]) { "[#{type}]".ljust(11) }
end

.loggerBoolean (private)

Returns:



161
162
163
164
165
166
167
# File 'lib/vedeu/logging/log.rb', line 161

def logger
  MonoLogger.new(Vedeu.config.log).tap do |log|
    log.formatter = proc do |_, _, _, message|
      formatted_message(message)
    end
  end
end

.outdent(&block) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Used by Events::Trigger to outdent log messages to show activity which occurs as part of the previous event triggering.

Parameters:

  • block (Proc)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vedeu/logging/log.rb', line 88

def outdent(&block)
  result = yield if block_given?

  if @count && @count > 0
    @count -= 1
  else
    @count = 0
  end

  result
end

.timestampString Also known as: log_timestamp

Returns:

  • (String)


102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/vedeu/logging/log.rb', line 102

def timestamp
  @now  = Vedeu.clock_time
  @time ||= 0.0
  @last ||= @now

  unless @last == @time
    @time += (@now - @last).round(4)
    @last = @now
  end

  "[#{format('%7.4f', @time.to_s)}] ".rjust(7)
end