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.



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

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.



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

def sync
  @sync
end

#verbosityObject

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



15
16
17
# File 'lib/webgen/logger.rb', line 15

def verbosity
  @verbosity
end

Instance Method Details

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

Utiltity method for logging a debug message.



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

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

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

Utiltity method for logging an error message.



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

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

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

Utiltity method for logging an informational message.



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

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

#levelObject

The severity threshold level.



57
58
59
# File 'lib/webgen/logger.rb', line 57

def level
  @logger.level
end

#level=(value) ⇒ Object

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



62
63
64
# File 'lib/webgen/logger.rb', line 62

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.



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

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.



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

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.



50
51
52
53
54
# File 'lib/webgen/logger.rb', line 50

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

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

Utiltity method for writing a normal output message.



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

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

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

Utiltity method for writing a verbose output message.



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

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

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

Utiltity method for logging a warning message.



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

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