Class: Fhlow::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/module_fhlow/log.rb

Overview

A Log object records all the messages to the specified log file and optional direkt to the console.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_logfilename, _loglevel, _pen, _cwidth = 25) ⇒ Log

Initializes the members and opens the logfile.

_logfilename

Filename of the logfile (may include the path).

_loglevel

See attrubute loglevel

_pen

Reference to the console output object.

_cwidth

Width of the caller collumn



52
53
54
55
56
57
# File 'lib/module_fhlow/log.rb', line 52

def initialize(_logfilename, _loglevel, _pen, _cwidth=25)
    @loglevel       = _loglevel
    @pen            = _pen
    @cwidth         = _cwidth 
    @logfile        = File.new(_logfilename,'w')
end

Instance Attribute Details

#loglevel=(value) ⇒ Object (writeonly)

The loglevel specifies which messages are logged. These loglevels are valid:

3

logfile: error;

2

logfile: error, warning;

1

logfile: error, warning, info;

0

logfile: error, warning, info, debug;

-1

logfile: error, warning, info, debug; console: error;

-2

logfile: error, warning, info, debug; console: error, warning;

-3

logfile: error, warning, info, debug; console: error, warning, info;

-4

logfile: error, warning, info, debug; console: error, warning, info, debug;



44
45
46
# File 'lib/module_fhlow/log.rb', line 44

def loglevel=(value)
  @loglevel = value
end

Instance Method Details

#closeObject

Closes the logfile.



61
62
63
# File 'lib/module_fhlow/log.rb', line 61

def close
    @logfile.close if @logfile.instance_of?(File)
end

#debug(_caller, _debugmsg) ⇒ Object

Logs a debug message if the loglevel fits.

_caller

Information about where this function was invoked.

_debugmsg

The actual message.



68
69
70
71
# File 'lib/module_fhlow/log.rb', line 68

def debug(_caller, _debugmsg)
    @logfile.puts("["+Time.now.to_s+"] DebugMsg by "+_caller.to_s.ljust(@cwidth)+" -> "+_debugmsg.to_s) if @loglevel <= 0
    @pen.print("- DebugMsg -  by "+_caller.to_s.ljust(@cwidth)+" -> "+_debugmsg.to_s+"\n") if @loglevel <= -4
end

#error(_caller, _error) ⇒ Object

Logs an error message if the loglevel fits.

_caller

Information about where this function was invoked.

_error

The actual message.



92
93
94
95
# File 'lib/module_fhlow/log.rb', line 92

def error(_caller, _error)
    @logfile.puts("["+Time.now.to_s+"] Error    by "+_caller.to_s.ljust(@cwidth)+" -> "+_error.to_s) if @loglevel <= 3
    @pen.print(@pen.lightred, "- Error -     by "+_caller.to_s.ljust(@cwidth)+" -> ",_error.to_s,"\n", @pen.clear) if @loglevel <= -1
end

#info(_caller, _info) ⇒ Object

Logs an info message if the loglevel fits.

_caller

Information about where this function was invoked.

_info

The actual message.



76
77
78
79
# File 'lib/module_fhlow/log.rb', line 76

def info(_caller, _info)
    @logfile.puts("["+Time.now.to_s+"] Info     by "+_caller.to_s.ljust(@cwidth)+" -> "+_info.to_s) if @loglevel <= 1
    @pen.print("- Info -      by "+_caller.to_s.ljust(@cwidth)+" -> "+_info.to_s+"\n") if @loglevel <= -3
end

#warning(_caller, _warning) ⇒ Object

Logs a warning message if the loglevel fits.

_caller

Information about where this function was invoked.

_warning

The actual message.



84
85
86
87
# File 'lib/module_fhlow/log.rb', line 84

def warning(_caller, _warning)
    @logfile.puts("["+Time.now.to_s+"] Warning  by "+_caller.to_s.ljust(@cwidth)+" -> "+_warning.to_s) if @loglevel <= 2
    @pen.print(@pen.lightyellow, "- Warning -   by "+_caller.to_s.ljust(@cwidth)+" -> "+_warning.to_s+"\n", @pen.clear) if @loglevel <= -2
end