CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/logging/sinks/stream.rb
Views: 11784
1
# -*- coding: binary -*-
2
3
4
module Rex
5
module Logging
6
module Sinks
7
8
###
9
#
10
# This class implements the LogSink interface and backs it against a stream
11
###
12
class Stream
13
14
include Rex::Logging::LogSink
15
16
def initialize(stream)
17
@stream = stream
18
end
19
20
#
21
# Writes log data to a stream
22
#
23
def log(sev, src, level, msg) # :nodoc:
24
if sev == LOG_RAW
25
stream.write(msg)
26
else
27
stream.write("[#{get_current_timestamp}] [#{log_code_for(sev)}(#{level})] #{src}: #{msg}\n")
28
end
29
30
stream.flush
31
end
32
33
def cleanup # :nodoc:
34
stream.close
35
end
36
37
protected
38
39
attr_accessor :stream # :nodoc:
40
41
#
42
# This method returns the corresponding log code for the given severity
43
#
44
def log_code_for(sev)
45
code = 'i'
46
47
case sev
48
when LOG_DEBUG
49
code = 'd'
50
when LOG_ERROR
51
code = 'e'
52
when LOG_INFO
53
code = 'i'
54
when LOG_WARN
55
code = 'w'
56
end
57
58
code
59
end
60
61
end
62
63
end end end
64
65