Class: Webgen::Logger

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

Overview

The class used by a Website to do the logging and the normal output.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(outdev = $stdout, sync = false) ⇒ Logger

Create a new Logger object which uses outdev as output device. If sync is set to true, log messages are interspersed with normal output.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/webgen/logger.rb', line 17

def initialize(outdev=$stdout, sync=false)
  @sync = sync
  @outdev = outdev
  @logger = (@sync ? ::Logger.new(@outdev) : ::Logger.new(@logio = StringIO.new))
  @logger.formatter = Proc.new do |severity, timestamp, progname, msg|
    if self.level == ::Logger::DEBUG
      "%5s -- %s: %s\n" % [severity, progname, msg ]
    else
      "%5s -- %s\n" % [severity, msg]
    end
  end
  self.level = ::Logger::WARN
  self.verbosity = :normal
  @marks = []
end

Instance Attribute Details

#syncObject (readonly)

Specifies whether log output should be synchronous with normal output.



10
11
12
# File 'lib/webgen/logger.rb', line 10

def sync
  @sync
end

#verbosityObject

Normal output verbosity (:normal, :verbose, :quiet).



13
14
15
# File 'lib/webgen/logger.rb', line 13

def verbosity
  @verbosity
end

Instance Method Details

#debug(source = '', &block) ⇒ Object

Utiltity method for logging a debug message.



85
# File 'lib/webgen/logger.rb', line 85

def debug(source='', &block); log(:debug, source, &block); end

#error(source = '', &block) ⇒ Object

Utiltity method for logging an error message.



76
# File 'lib/webgen/logger.rb', line 76

def error(source='', &block); log(:error, source, &block); end

#info(source = '', &block) ⇒ Object

Utiltity method for logging an informational message.



82
# File 'lib/webgen/logger.rb', line 82

def info(source='', &block); log(:info, source, &block); end

#levelObject

The severity threshold level.



55
56
57
# File 'lib/webgen/logger.rb', line 55

def level
  @logger.level
end

#level=(value) ⇒ Object

Set the severity threshold to value which can be one of the stdlib Logger severity levels.



60
61
62
# File 'lib/webgen/logger.rb', line 60

def level=(value)
  @logger.level = value
end

#log(sev_level, source = '', &block) ⇒ Object

Log a message of sev_level from source. The mandatory block has to return the message.



65
66
67
68
69
70
71
72
73
# File 'lib/webgen/logger.rb', line 65

def log(sev_level, source='', &block)
  if sev_level == :stdout
    @outdev.write(block.call + "\n") if @verbosity == :normal || @verbosity == :verbose
  elsif sev_level == :verbose
    @outdev.write(block.call + "\n") if @verbosity == :verbose
  else
    @logger.send(sev_level, source, &block)
  end
end

#log_outputObject

Returns the output of the logger when #sync is false. Otherwise an empty string is returned.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/webgen/logger.rb', line 34

def log_output
  if @sync
    ''
  else
    out = @logio.string.dup
    @marks.reverse.each_with_index do |mark, index|
      out.insert(mark, " INFO -- Log messages for run #{@marks.length - index} are following\n")
    end if out.length > 0
    out
  end
end

#mark_new_cycleObject

Only used when #sync is +false: Mark the location in the log stream where a new update/write run begins.



48
49
50
51
52
# File 'lib/webgen/logger.rb', line 48

def mark_new_cycle
  if !@sync
    @marks << @logio.string.length
  end
end

#stdout(source = '', &block) ⇒ Object

Utiltity method for writing a normal output message.



88
# File 'lib/webgen/logger.rb', line 88

def stdout(source='', &block); log(:stdout, source, &block); end

#verbose(source = '', &block) ⇒ Object

Utiltity method for writing a verbose output message.



91
# File 'lib/webgen/logger.rb', line 91

def verbose(source='', &block); log(:verbose, source, &block); end

#warn(source = '', &block) ⇒ Object

Utiltity method for logging a warning message.



79
# File 'lib/webgen/logger.rb', line 79

def warn(source='', &block); log(:warn, source, &block); end