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.


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

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

Instance Attribute Details

#log_levelsObject

:nodoc:


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

def log_levels
  @log_levels
end

#log_sinksObject

:nodoc:


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

def log_sinks
  @log_sinks
end

#log_sinks_lockObject

:nodoc:


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

def log_sinks_lock
  @log_sinks_lock
end

Instance Method Details

#[](src) ⇒ Object

Returns the sink that is associated with the supplied source.


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

def [](src)
  sink = nil

  log_sinks_lock.synchronize {
    sink = log_sinks[src]
  }

  return sink
end

#[]=(src, sink) ⇒ Object

Calls the source association routie.


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

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

#delete(src) ⇒ Object

Removes a source association if one exists.


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

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.


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

def get_level(src)
  log_levels[src]
end

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

Performs the actual log operation against the supplied source


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

def log(sev, src, level, msg, from)
  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, from)
    end
  }
end

#set_level(src, level) ⇒ Object

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


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

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.


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

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