2006-08-21 23:08:36 +00:00
|
|
|
require 'rex/sync'
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/logging/log_sink'
|
2005-06-04 08:15:10 +00:00
|
|
|
|
2006-08-21 23:03:58 +00:00
|
|
|
module Rex
|
2005-06-04 08:15:10 +00:00
|
|
|
module Logging
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# The log dispatcher associates log sources with log sinks. A log source
|
|
|
|
# is a unique identity that is associated with one and only one log sink.
|
|
|
|
# For instance, the framework-core registers the 'core'
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class LogDispatcher
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Creates the global log dispatcher instance and initializes it for use.
|
|
|
|
#
|
2005-06-04 08:15:10 +00:00
|
|
|
def initialize()
|
|
|
|
self.log_sinks = {}
|
2005-10-30 23:40:27 +00:00
|
|
|
self.log_levels = {}
|
2005-06-04 08:15:10 +00:00
|
|
|
self.log_sinks_rwlock = ReadWriteLock.new
|
|
|
|
end
|
|
|
|
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-11-19 15:09:41 +00:00
|
|
|
# Returns the sink that is associated with the supplied source.
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-06-04 08:15:10 +00:00
|
|
|
def [](src)
|
|
|
|
sink = nil
|
|
|
|
|
|
|
|
log_sinks_rwlock.synchronize_read {
|
|
|
|
sink = log_sinks[src]
|
|
|
|
}
|
|
|
|
|
|
|
|
return sink
|
|
|
|
end
|
|
|
|
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-11-19 15:09:41 +00:00
|
|
|
# Calls the source association routie.
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-06-04 08:15:10 +00:00
|
|
|
def []=(src, sink)
|
|
|
|
store(src, sink)
|
|
|
|
end
|
|
|
|
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-11-19 15:09:41 +00:00
|
|
|
# Associates the supplied source with the supplied sink. If a log level
|
|
|
|
# has already been defined for the source, the level argument is ignored.
|
|
|
|
# Use set_log_level to alter it.
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-11-02 23:11:09 +00:00
|
|
|
def store(src, sink, level = 0)
|
2005-06-04 08:15:10 +00:00
|
|
|
log_sinks_rwlock.synchronize_write {
|
|
|
|
if (log_sinks[src] == nil)
|
|
|
|
log_sinks[src] = sink
|
2005-11-02 23:11:09 +00:00
|
|
|
|
2005-11-19 15:09:41 +00:00
|
|
|
set_log_level(src, level) if (log_levels[src] == nil)
|
2005-06-04 08:15:10 +00:00
|
|
|
else
|
|
|
|
raise(
|
|
|
|
RuntimeError,
|
|
|
|
"The supplied log source #{src} is already registered.",
|
|
|
|
caller)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-11-19 15:09:41 +00:00
|
|
|
# Removes a source association if one exists.
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-06-04 08:15:10 +00:00
|
|
|
def delete(src)
|
|
|
|
sink = nil
|
|
|
|
|
|
|
|
log_sinks_rwlock.synchronize_write {
|
|
|
|
sink = log_sinks[src]
|
|
|
|
|
|
|
|
log_sinks.delete(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sink)
|
|
|
|
sink.cleanup
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-06-04 08:15:10 +00:00
|
|
|
# Performs the actual log operation against the supplied source
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
2005-06-04 08:15:10 +00:00
|
|
|
def log(sev, src, level, msg, from)
|
|
|
|
log_sinks_rwlock.synchronize_read {
|
|
|
|
if ((sink = log_sinks[src]))
|
2005-11-19 15:09:41 +00:00
|
|
|
next if (log_levels[src] and level > log_levels[src])
|
2005-10-30 23:40:27 +00:00
|
|
|
|
2005-06-04 08:15:10 +00:00
|
|
|
sink.log(sev, src, level, msg, from)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-10-30 23:40:27 +00:00
|
|
|
#
|
|
|
|
# This method sets the log level threshold for a given source.
|
|
|
|
#
|
|
|
|
def set_level(src, level)
|
|
|
|
log_levels[src] = level.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method returns the log level threshold of a given source.
|
|
|
|
#
|
|
|
|
def get_level(src)
|
|
|
|
log_levels[src]
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :log_sinks, :log_sinks_rwlock # :nodoc:
|
|
|
|
attr_accessor :log_levels # :nodoc:
|
2005-06-04 08:15:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# An instance of the log dispatcher exists in the global namespace, along
|
|
|
|
# with stubs for many of the common logging methods. Various sources can
|
|
|
|
# register themselves as a log sink such that logs can be directed at
|
|
|
|
# various targets depending on where they're sourced from. By doing it
|
|
|
|
# this way, things like sessions can use the global logging stubs and
|
|
|
|
# still be directed at the correct log file.
|
|
|
|
#
|
|
|
|
###
|
2005-09-23 05:51:09 +00:00
|
|
|
ExceptionCallStack = "__EXCEPTCALLSTACK__"
|
|
|
|
|
2005-06-04 08:15:10 +00:00
|
|
|
def dlog(msg, src = 'core', level = 0, from = caller)
|
|
|
|
$dispatcher.log(LOG_DEBUG, src, level, msg, from)
|
|
|
|
end
|
|
|
|
|
|
|
|
def elog(msg, src = 'core', level = 0, from = caller)
|
|
|
|
$dispatcher.log(LOG_ERROR, src, level, msg, from)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wlog(msg, src = 'core', level = 0, from = caller)
|
|
|
|
$dispatcher.log(LOG_WARN, src, level, msg, from)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ilog(msg, src = 'core', level = 0, from = caller)
|
|
|
|
$dispatcher.log(LOG_INFO, src, level, msg, from)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rlog(msg, src = 'core', level = 0, from = caller)
|
2005-09-23 05:51:09 +00:00
|
|
|
if (msg == ExceptionCallStack)
|
|
|
|
msg = "\nCall stack:\n" + $@.join("\n") + "\n"
|
|
|
|
end
|
|
|
|
|
2005-06-04 08:15:10 +00:00
|
|
|
$dispatcher.log(LOG_RAW, src, level, msg, from)
|
|
|
|
end
|
|
|
|
|
2005-11-28 16:52:05 +00:00
|
|
|
def log_source_registered?(src)
|
|
|
|
($dispatcher[src] != nil)
|
|
|
|
end
|
|
|
|
|
2005-11-22 03:20:09 +00:00
|
|
|
def register_log_source(src, sink, level = nil)
|
2005-06-04 08:15:10 +00:00
|
|
|
$dispatcher[src] = sink
|
2005-11-22 03:20:09 +00:00
|
|
|
|
|
|
|
set_log_level(src, level) if (level)
|
2005-06-04 08:15:10 +00:00
|
|
|
end
|
|
|
|
|
2005-10-02 03:21:26 +00:00
|
|
|
def deregister_log_source(src)
|
2005-06-04 08:15:10 +00:00
|
|
|
$dispatcher.delete(src)
|
|
|
|
end
|
|
|
|
|
2005-10-30 23:40:27 +00:00
|
|
|
def set_log_level(src, level)
|
|
|
|
$dispatcher.set_level(src, level)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_log_level(src)
|
|
|
|
$dispatcher.get_level(src)
|
|
|
|
end
|
|
|
|
|
2005-06-04 08:15:10 +00:00
|
|
|
# Creates the global log dispatcher
|
|
|
|
$dispatcher = Rex::Logging::LogDispatcher.new
|
2005-09-23 05:51:09 +00:00
|
|
|
|