Module: BasicLogging

Included in:
Action, Cell, Column, Menu, Row, Scrollable, SheetData, SheetInterface, ViewWorkBook
Defined in:
lib/basic_logging.rb

Overview

require ‘time’

Constant Summary collapse

DEBUG =
0
INFO =
1
WARN =
2
ERROR =
3
FATAL =
4
UNKNOWN =
nil
Levels =

this is mainly for the translation of method calls into log levels

{:debug => DEBUG, :info => INFO, :warn => WARN, :error => ERROR,
:fatal => FATAL, :unknown => UNKNOWN}
@@log_level =
UNKNOWN
@@target =
STDOUT
@@muted =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#log_levelObject (readonly)

Returns the value of attribute log_level.



110
111
112
# File 'lib/basic_logging.rb', line 110

def log_level
  @log_level
end

#targetObject (readonly)

Returns the value of attribute target.



110
111
112
# File 'lib/basic_logging.rb', line 110

def target
  @target
end

Class Method Details

.is_muted?(obj) ⇒ Boolean



49
50
51
52
# File 'lib/basic_logging.rb', line 49

def self.is_muted?(obj)
  name = obj.class == Class ? obj.name.dup : obj.class.name
  @@muted.include?(name)  
end

.mute(obj) ⇒ Object

do not log, if caller is obj (class or instance)



44
45
46
47
# File 'lib/basic_logging.rb', line 44

def self.mute(obj)
  name = obj.class == Class ? obj.name.dup : obj.class.name
  @@muted << name
end

.set_level(lv) ⇒ Object

set the log level



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/basic_logging.rb', line 55

def self::set_level(lv)
  if lv.respond_to?(:to_str)
    lv = Levels[lv.to_sym]
  end
  if(!lv || (lv.respond_to?(:to_int) && lv >= DEBUG && lv <= FATAL) )
    @@log_level = lv
  else
    STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << lv.to_s << "\""
    STDERR.puts "Keepinng old log level " << Levels.keys.detect {| k| Levels[k] == @@log_level}.to_s
  end
end

.set_target(tg) ⇒ Object

set the log target



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/basic_logging.rb', line 72

def self::set_target(tg)
  if tg.respond_to?(:to_io) 
    @@target = tg
  elsif(!File::exist?(tg) || ( File.file?(tg) && File.writable?(tg) ) )
    @@target = File.open(tg, 'w+')  
  else
    STDERR.puts __FILE__.dup << ': ERROR : target ' << tg << ' cannot be set'
    STDERR.puts "Keeping old target " << @@target.inspect
    return
  end
end

Instance Method Details

#log(message) ⇒ Object Also known as: debug, info, warn, error, fatal

Output of log messages, depending on the log level set for the calling class and the name of the alias method which is actually called.



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/basic_logging.rb', line 90

def log(message)
  if !BasicLogging.is_muted?(self)
    # how has this method been called?
    mlevel = __callee__
    if Levels.has_key?(mlevel) && Levels[mlevel] <=  FATAL
      # output only for levels equal or above the value that corresponds to
      # the calling alias.
      format_log( message, mlevel) if @@log_level && Levels[mlevel] >= @@log_level 
    else
      STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << mlevel.to_s << "\""
    end
  end
end

#set_level(lv) ⇒ Object



67
68
69
# File 'lib/basic_logging.rb', line 67

def set_level(lv)
  BasicLogging::set_level(lv)
end

#set_target(tg) ⇒ Object



84
85
86
# File 'lib/basic_logging.rb', line 84

def set_target(tg)
  BasicLogging::set_target(tg)
end