Class: Yast::Y2Logger

Inherits:
Logger
  • Object
show all
Includes:
Singleton
Defined in:
src/ruby/yast/y2logger.rb

Overview

A Ruby Logger which wraps Yast.y2*() calls

Constant Summary collapse

CALL_FRAME =

location of the caller

2

Instance Method Summary collapse

Constructor Details

#initialize(*_args) ⇒ Y2Logger

Returns a new instance of Y2Logger.



75
76
77
78
79
80
# File 'src/ruby/yast/y2logger.rb', line 75

def initialize(*_args)
  # do not write to any file, the actual logging is implemented in add()
  super(nil)
  # process also debug messages but might not be logged in the end
  self.level = ::Logger::DEBUG
end

Instance Method Details

#add(severity, _progname = nil, message = nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'src/ruby/yast/y2logger.rb', line 54

def add(severity, _progname = nil, message = nil, &block)
  message = block.call if block

  case severity
  when DEBUG
    Yast.y2debug(CALL_FRAME, message)
  when INFO
    Yast.y2milestone(CALL_FRAME, message)
  when WARN
    Yast.y2warning(CALL_FRAME, message)
  when ERROR
    Yast.y2error(CALL_FRAME, message)
  when FATAL
    Yast.y2error(CALL_FRAME, message)
  when UNKNOWN
    Yast.y2internal(CALL_FRAME, message)
  else
    Yast.y2internal(CALL_FRAME, "Unknown error level #{severity}: Error: #{message}")
  end
end

#group(description, &block) {|group| ... } ⇒ Object

log a block of commands, adds a special begin and end markers into the log, the block should be one big logical step in the process, can be used recursively, e.g. log.group might call another log.group inside

Parameters:

  • description (String)

    short description of the block

  • block (Proc)

    block to call

Yield Parameters:

  • group (LogGroupResult)

    can be optionally used to pass result details

Yield Returns:

Returns:

  • (Object)

    whatever the block returned



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'src/ruby/yast/y2logger.rb', line 92

def group(description, &block)
  details = LogGroupResult.new
  # mark start of the group
  info "::group::#{Process.clock_gettime(Process::CLOCK_MONOTONIC)}::#{description}"

  if block_given?
    ret = block.call(details)
  else
    raise ArgumentError, "Missing a block"
  end
  details.result = ret

  ret
rescue StandardError => e
  # mark a failure
  details.success = false
  details.summary = "Raised exception: #{e}"
  # reraise the original exception
  raise
ensure
  # mark end of the group with result data, if it failed log as an error
  level = details.success? ? :info : :error
  public_send(level, "::endgroup::#{Process.clock_gettime(Process::CLOCK_MONOTONIC)}::#{details.summary}")
end