Class: Rex::Logging::Sinks::Stream

Inherits:
Object
  • Object
show all
Includes:
LogSink
Defined in:
lib/rex/logging/sinks/stream.rb

Overview

This class implements the LogSink interface and backs it against a stream

Direct Known Subclasses

Flatfile, Stderr, Stdout, StdoutWithoutTimestamps

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LogSink

#get_current_timestamp

Constructor Details

#initialize(stream) ⇒ Stream

Returns a new instance of Stream.



16
17
18
# File 'lib/rex/logging/sinks/stream.rb', line 16

def initialize(stream)
  @stream = stream
end

Instance Attribute Details

#streamObject (protected)

:nodoc:



39
40
41
# File 'lib/rex/logging/sinks/stream.rb', line 39

def stream
  @stream
end

Instance Method Details

#cleanupObject

:nodoc:



33
34
35
# File 'lib/rex/logging/sinks/stream.rb', line 33

def cleanup # :nodoc:
  stream.close
end

#log(sev, src, level, msg) ⇒ Object

Writes log data to a stream



23
24
25
26
27
28
29
30
31
# File 'lib/rex/logging/sinks/stream.rb', line 23

def log(sev, src, level, msg) # :nodoc:
  if sev == LOG_RAW
    stream.write(msg)
  else
    stream.write("[#{get_current_timestamp}] [#{log_code_for(sev)}(#{level})] #{src}: #{msg}\n")
  end

  stream.flush
end

#log_code_for(sev) ⇒ Object (protected)

This method returns the corresponding log code for the given severity



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rex/logging/sinks/stream.rb', line 44

def log_code_for(sev)
  code = 'i'

  case sev
  when LOG_DEBUG
    code = 'd'
  when LOG_ERROR
    code = 'e'
  when LOG_INFO
    code = 'i'
  when LOG_WARN
    code = 'w'
  end

  code
end