class Pocolog::Logger

Constants

Sampling

Attributes

mutex[R]
on_demand[R]
output[R]
periodic[R]

Public Class Methods

new(output) click to toggle source

Create a new logger which will log into the output Logfiles object.

# File lib/pocolog/logger.rb, line 11
def initialize(output)
    @mutex = Mutex.new 
    @on_demand = []
    @periodic  = []
    @output = output
end

Public Instance Methods

add(source, period) click to toggle source

Add a new source to load

# File lib/pocolog/logger.rb, line 25
def add(source, period)
    stream = output.stream source.full_name, source.type, true

    spec = Sampling.new(nil, stream, source, nil)
    if period == :on_demand
        on_demand << spec
    else 
        mutex.synchronize do
            spec.period      = Float(period)
            spec.next_sample = Time.now + spec.period
            periodic << spec
        end
    end
end
close() click to toggle source
# File lib/pocolog/logger.rb, line 18
def close
    @quit = true
    @logging_thread.join if @logging_thread
    output.close
end
log_pending_sources() click to toggle source
# File lib/pocolog/logger.rb, line 50
def log_pending_sources
    while true
        # Loop until there is no pending sample to write
        mutex.synchronize do
            @periodic = periodic.sort_by { |spec| spec.next_sample }
            next_read = periodic.first
            return if next_read.next_sample > Time.now
        end

        log_source(next_read)
        next_read.next_sample += next_read.period
    end
end
log_sample(spec, force = false) click to toggle source
# File lib/pocolog/logger.rb, line 64
def log_sample(spec, force = false)
    source = spec.source
    if force || (source.timestamp != spec.last_write)
        spec.stream.write(Time.now, source.timestamp, source.read)
        spec.last_write = source.timestamp
    end
end
now(force = false) click to toggle source

Log all configured sources once, now. If force is true, read even if the sources have not been updated

# File lib/pocolog/logger.rb, line 75
def now(force = false)
    mutex.synchronize do
        (on_demand + periodic).each do |spec|
            log_sample(spec, force)
        end
    end

end
start() click to toggle source
# File lib/pocolog/logger.rb, line 40
def start
    @logging_thread = Thread.new do
        while true
            break if @quit
            log_pending_sources
            sleep(periodic.first.next_sample - Time.now)
        end
    end
end