Class: Simplelog

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

Overview

All puts statements are logged to file and shown on screen.

To use:

require ‘simplelog’

Constant Summary collapse

@@dir =
"."
@@name =
"LOG_"
@@logtext =
""

Class Method Summary collapse

Class Method Details

.clearObject

Allows you to clear your log if you so wish



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

def Simplelog.clear
  @@logtext = ""
end

.dirObject



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

def Simplelog.dir
  @@dir
end

.dir=(dir) ⇒ Object

Where will you save your log? Defaults to “.”



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

def Simplelog.dir=(dir)
  @@dir = dir.sub(/\\*$/, "")
end

.filenameObject

Directory and filename. Subsequent underscores are removed.



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

def Simplelog.filename
  "#@@dir/" << "#{@@name}_#{Time.now.strftime("%Y-%m-%d_%H%M")}.txt".gsub(/_{2,}/, "_")    
end

.logtextObject



52
53
54
# File 'lib/simplelog.rb', line 52

def Simplelog.logtext
  @@logtext
end

.nameObject



34
35
36
# File 'lib/simplelog.rb', line 34

def Simplelog.name
  @@name
end

.name=(name) ⇒ Object

The logs name defaults to LOG_. This can be redefined at wish.



39
40
41
# File 'lib/simplelog.rb', line 39

def Simplelog.name=(name)
  @@name = name  
end

.puts(text) ⇒ Object



61
62
63
64
# File 'lib/simplelog.rb', line 61

def Simplelog.puts(text)
  old_puts text
  @@logtext << text.to_s << "\n"
end

.saveObject



71
72
73
74
75
76
77
78
# File 'lib/simplelog.rb', line 71

def Simplelog.save
  if !@@logtext.empty?
    File.makedirs(@@dir) if !File.directory?(@@dir)
    File.open(filename, "w") do |f|
      f.puts @@logtext
    end
  end
end