Class: Rex::Logging::LogDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/logging/log_dispatcher.rb

Overview

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’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogDispatcher

Creates the global log dispatcher instance and initializes it for use.



19
20
21
22
23
# File 'lib/rex/logging/log_dispatcher.rb', line 19

def initialize()
  self.log_sinks      = {}
  self.log_levels     = {}
  self.log_sinks_lock = Mutex.new
end

Instance Attribute Details

#log_levelsObject

:nodoc:



114
115
116
# File 'lib/rex/logging/log_dispatcher.rb', line 114

def log_levels
  @log_levels
end

#log_sinksObject

:nodoc:



113
114
115
# File 'lib/rex/logging/log_dispatcher.rb', line 113

def log_sinks
  @log_sinks
end

#log_sinks_lockObject

:nodoc:



113
114
115
# File 'lib/rex/logging/log_dispatcher.rb', line 113

def log_sinks_lock
  @log_sinks_lock
end

Instance Method Details

#[](src) ⇒ Object

Returns the sink that is associated with the supplied source.



28
29
30
31
32
33
34
35
36
# File 'lib/rex/logging/log_dispatcher.rb', line 28

def [](src)
  sink = nil

  log_sinks_lock.synchronize {
    sink = log_sinks[src]
  }

  return sink
end

#[]=(src, sink) ⇒ Object

Calls the source association routie.



41
42
43
# File 'lib/rex/logging/log_dispatcher.rb', line 41

def []=(src, sink)
  store(src, sink)
end

#delete(src) ⇒ Object

Removes a source association if one exists.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rex/logging/log_dispatcher.rb', line 68

def delete(src)
  sink = nil

  log_sinks_lock.synchronize {
    sink = log_sinks[src]

    log_sinks.delete(src)
  }

  if (sink)
    sink.cleanup

    return true
  end

  return false
end

#get_level(src) ⇒ Object

This method returns the log level threshold of a given source.



109
110
111
# File 'lib/rex/logging/log_dispatcher.rb', line 109

def get_level(src)
  log_levels.fetch(src, DEFAULT_LOG_LEVEL)
end

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

Performs the actual log operation against the supplied source



89
90
91
92
93
94
95
96
97
# File 'lib/rex/logging/log_dispatcher.rb', line 89

def log(sev, src, level, msg)
  log_sinks_lock.synchronize {
    if ((sink = log_sinks[src]))
      next if (log_levels[src] and level > log_levels[src])

      sink.log(sev, src, level, msg)
    end
  }
end

#set_level(src, level) ⇒ Object

This method sets the log level threshold for a given source.



102
103
104
# File 'lib/rex/logging/log_dispatcher.rb', line 102

def set_level(src, level)
  log_levels[src] = level.to_i
end

#store(src, sink, level = 0) ⇒ Object

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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rex/logging/log_dispatcher.rb', line 50

def store(src, sink, level = 0)
  log_sinks_lock.synchronize {
    if (log_sinks[src] == nil)
      log_sinks[src] = sink

      set_log_level(src, level) if (log_levels[src] == nil)
    else
      raise(
        RuntimeError,
        "The supplied log source #{src} is already registered.",
        caller)
    end
  }
end