Class: Pwnlib::Logger::LoggerType
- Inherits:
-
Logger
- Object
- Logger
- Pwnlib::Logger::LoggerType
- Extended by:
- MethodSource::CodeHelpers
- Includes:
- Context
- Defined in:
- lib/pwnlib/logger.rb
Overview
The type for logger which inherits Ruby builtin Logger. Main difference is using context.log_level
instead of level
in logging methods.
Constant Summary collapse
- SEV_COLOR =
Color codes for pretty logging.
{ 'DEBUG' => '#ff5f5f', 'INFO' => '#87ff00', 'WARN' => '#ffff00', 'ERROR' => '#ff5f00', 'FATAL' => '#ff0000' }.freeze
Instance Method Summary collapse
-
#dump(*args) ⇒ Object
Log the arguments and their evalutated results.
-
#indented(message, level: DEBUG) ⇒ Object
Log the message with indent.
-
#initialize ⇒ LoggerType
constructor
Instantiate a LoggerType object.
Constructor Details
#initialize ⇒ LoggerType
Instantiate a Pwnlib::Logger::LoggerType object.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pwnlib/logger.rb', line 36 def initialize super($stdout) @formatter = proc do |severity, _datetime, progname, msg| format("[%s] %s\n", Rainbow(progname || severity).color(SEV_COLOR[severity]), msg) end # Cache the file content so we can modify the file when it's running. # If the source is modified before the first call, parsing source file # might still fail. @source_of_file_cache = Hash.new do |h, key| next if key.nil? h[key] = IO.read(key) end # As a naive heuristic for the most common single file use case, adding # the last file from the execution stack. @source_of_file_cache[caller_locations.last.absolute_path] end |
Instance Method Details
#dump(*args) ⇒ Object
Note:
This method does NOT work in a REPL shell (such as irb and pry).
Note:
Log the arguments and their evalutated results.
This method has same severity as INFO
.
The difference between using arguments and passing a block is the block will be executed if the logger's level is sufficient to log a message.
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/pwnlib/logger.rb', line 122 def dump(*args) severity = INFO # Don't invoke the block if it's unnecessary. return true if severity < context.log_level caller_ = caller_locations(1, 1).first src = source_of(caller_.absolute_path, caller_.lineno) results = args.empty? ? [[yield, source_of_block(src)]] : args.zip(source_of_args(src)) msg = results.map { |res, expr| "#{expr.strip} = #{res.inspect}" }.join(', ') # do indent if msg contains multiple lines first, *remain = msg.split("\n") add(severity, ([first] + remain.map { |r| '[DUMP] '.gsub(/./, ' ') + r }).join("\n"), 'DUMP') end |
#indented(message, level: DEBUG) ⇒ Object
Log the message with indent.
62 63 64 65 66 67 68 69 |
# File 'lib/pwnlib/logger.rb', line 62 def indented(, level: DEBUG) return if @logdev.nil? || level < context.log_level @logdev.write( "#{.lines.map { |s| " #{s}" }.join}\n" ) true end |