Class: Docuvator::Log

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/docuvator/log.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Returns a new instance of Log.



10
11
12
13
14
15
16
17
18
# File 'lib/docuvator/log.rb', line 10

def initialize
  @base_directory = File.expand_path("../..", __FILE__) + "/"
  @debugging = false
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::ERROR
  @logger.formatter = proc do |sev, datetime, progname, msg|
    "#{msg}\n"
  end
end

Instance Attribute Details

#base_directoryObject

Returns the value of attribute base_directory.



8
9
10
# File 'lib/docuvator/log.rb', line 8

def base_directory
  @base_directory
end

#debuggingObject

Returns the value of attribute debugging.



8
9
10
# File 'lib/docuvator/log.rb', line 8

def debugging
  @debugging
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/docuvator/log.rb', line 8

def logger
  @logger
end

Class Method Details

.method_missing(method, *args, &blk) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/docuvator/log.rb', line 37

def self.method_missing(method, *args, &blk)
  if valid_method? method
    instance.logger.progname = parse_caller(caller(1).first) if instance.debugging
    instance.logger.send(method, *args, &blk)
  else
    super
  end
end

.parse_caller(message) ⇒ Object

Determine the file, method, line number of the caller



28
29
30
31
32
33
34
35
# File 'lib/docuvator/log.rb', line 28

def self.parse_caller(message)
  if /^(?<file>.+?):(?<line>\d+)(?::in `(?<method>.*)')?/ =~ message
    file = Regexp.last_match[:file]
    line = Regexp.last_match[:line]
    method = Regexp.last_match[:method]
    "#{file.sub(instance.base_directory, "")}:#{line}"
  end
end

.respond_to_missing?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/docuvator/log.rb', line 46

def self.respond_to_missing?(method, include_all=false)
  if valid_method? method
    true
  else
    super
  end
end

.use_debugObject



20
21
22
23
24
25
# File 'lib/docuvator/log.rb', line 20

def self.use_debug
  instance.debugging = true
  instance.logger.formatter = proc do |sev, datetime, progname, msg|
    "#{sev} [#{progname}]: #{msg}\n"
  end
end

.valid_method?(method) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/docuvator/log.rb', line 54

def self.valid_method?(method)
  instance.logger.respond_to? method
end