Module: Rofl

Includes:
RoflTrace
Defined in:
lib/rofl.rb

Overview

little happy logger module

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RoflTrace

#rofl_disable_trace, #rofl_enable_silent_trace, #rofl_enable_trace, #rofl_trace_event_callback

Instance Attribute Details

#debugnameObject

Returns the value of attribute debugname.



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

def debugname
  @debugname
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

Instance Method Details

#dlog(text = "debug") ⇒ Object

debug message



54
55
56
57
# File 'lib/rofl.rb', line 54

def dlog text="debug"
  rofl_logger_check #check if logger is setup
  @logger.debug "#{@debugname}.#{rofl_meth_trace.to_s}: #{text.to_s}"
end

#elog(text = "error") ⇒ Object

error message



36
37
38
39
# File 'lib/rofl.rb', line 36

def elog text="error" 
  rofl_logger_check #check if logger is setup
  @logger.error "#{@debugname}.#{rofl_meth_trace.to_s}: #{text.to_s}"
end

#ilog(text = "info") ⇒ Object

info message



48
49
50
51
# File 'lib/rofl.rb', line 48

def ilog text="info"
  rofl_logger_check #check if logger is setup
  @logger.info "#{@debugname}.#{rofl_meth_trace.to_s}: #{text.to_s}"
end

#rofl?(object = self) ⇒ Boolean

check if we or an object are ready to rofl

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/rofl.rb', line 72

def rofl? object=self
  has_rofl = (defined? object.rofl?).eql? "method"
  dlog "object of class: #{object.class} is rock'n'rofl." if has_rofl
  return has_rofl
end

#rofl_log_level(level = "") ⇒ Object

set the debug level



27
28
29
30
31
32
33
# File 'lib/rofl.rb', line 27

def rofl_log_level level=""
  @logger.level = Logger::DEBUG if level.eql? "debug"
  @logger.level = Logger::INFO if level.eql? "info"
  @logger.level = Logger::WARN if level.eql? "warning"
  @logger.level = Logger::ERROR if level.eql? "error"
  puts "ROFL-LOG-LEVEL: #{@logger.level}" 
end

#rofl_logger_checkObject

check if there already is a logger, kind of a constructor



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rofl.rb', line 10

def rofl_logger_check
  if @logger.nil?
    #to stop output from getting messy, we use a logger
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::DEBUG
    #@logger.datetime_format = "%Y-%m-%d %H:%M:%S" #useful for logging to a file
    @logger.datetime_format = "%H:%M:%S" #useful for debugging
    @debugname = self.class if @debugname.nil? #only used to inform the user
    @tracing = false
    #enable tracing
    if @tracing
      rofl_enable_silent_trace
    end
  end
end

#rofl_meth_traceObject

get method call trace



60
61
62
63
64
65
66
67
68
69
# File 'lib/rofl.rb', line 60

def rofl_meth_trace
  last_meth_name = "notrace"
  skip = 2 #indicates how many items we skip in the execution stack trace
  call_trace = caller(skip)
  regexp = /\`.*?\'/
  last_meth = call_trace[0][regexp]
  last_meth_name = last_meth.delete("\`") unless last_meth.nil?
  last_meth_name = last_meth_name.delete("\'") unless last_meth_name.nil?
  return last_meth_name
end

#wlog(text = "warning") ⇒ Object

warning



42
43
44
45
# File 'lib/rofl.rb', line 42

def wlog text="warning"
  rofl_logger_check #check if logger is setup
  @logger.warn "#{@debugname}.#{rofl_meth_trace.to_s}: #{text.to_s}"
end