Class: Pocolog::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/pocolog/logger.rb

Defined Under Namespace

Classes: Sampling

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Logger

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



11
12
13
14
15
16
# File 'lib/pocolog/logger.rb', line 11

def initialize(output)
    @mutex = Mutex.new 
    @on_demand = []
    @periodic  = []
    @output = output
end

Instance Attribute Details

#mutexObject (readonly)

Returns the value of attribute mutex



5
6
7
# File 'lib/pocolog/logger.rb', line 5

def mutex
  @mutex
end

#on_demandObject (readonly)

Returns the value of attribute on_demand



7
8
9
# File 'lib/pocolog/logger.rb', line 7

def on_demand
  @on_demand
end

#outputObject (readonly)

Returns the value of attribute output



6
7
8
# File 'lib/pocolog/logger.rb', line 6

def output
  @output
end

#periodicObject (readonly)

Returns the value of attribute periodic



8
9
10
# File 'lib/pocolog/logger.rb', line 8

def periodic
  @periodic
end

Instance Method Details

#add(source, period) ⇒ Object

Add a new source to load



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

#closeObject



18
19
20
21
22
# File 'lib/pocolog/logger.rb', line 18

def close
    @quit = true
    @logging_thread.join if @logging_thread
    output.close
end

#log_pending_sourcesObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



64
65
66
67
68
69
70
# 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) ⇒ Object

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



75
76
77
78
79
80
81
82
# 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

#startObject



40
41
42
43
44
45
46
47
48
# 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