simplelog 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/simplelog.rb +32 -12
- data/test/simplelogtest.rb +24 -20
- metadata +3 -3
data/lib/simplelog.rb
CHANGED
@@ -1,7 +1,16 @@
|
|
1
|
+
# =Simplelog
|
2
|
+
# Jon Egil Strand
|
3
|
+
#
|
4
|
+
# Simplelog is the simplest of loggers, built to log when your needs are simple.
|
5
|
+
# To use, just require 'simplelog' and all your puts statements are saved to a
|
6
|
+
# LOG_.txt file as well as printed on screen. That's it and that's that.
|
7
|
+
#
|
8
|
+
# This makes it ideal for situations where you need simple logging, or when an
|
9
|
+
# existing application grows and you need to save output in a hurry.
|
1
10
|
|
2
11
|
require 'date'
|
3
12
|
|
4
|
-
|
13
|
+
# Here we redefine puts
|
5
14
|
module Kernel
|
6
15
|
alias old_puts puts
|
7
16
|
def puts(text)
|
@@ -9,49 +18,60 @@ module Kernel
|
|
9
18
|
end
|
10
19
|
end
|
11
20
|
|
21
|
+
#
|
22
|
+
# All puts statements are logged to file and shown on screen.
|
23
|
+
#
|
24
|
+
# To use:
|
25
|
+
#
|
26
|
+
# require 'simplelog'
|
27
|
+
#
|
12
28
|
class Simplelog
|
13
29
|
@@dir = "."
|
14
|
-
@@name = ""
|
15
|
-
@@
|
30
|
+
@@name = "LOG_"
|
31
|
+
@@logtext = ""
|
16
32
|
|
17
33
|
def Simplelog.name
|
18
34
|
@@name
|
19
35
|
end
|
20
36
|
|
37
|
+
# The logs name defaults to LOG_. This can be redefined at wish.
|
21
38
|
def Simplelog.name=(name)
|
22
39
|
@@name = name
|
23
40
|
end
|
24
|
-
|
41
|
+
|
25
42
|
def Simplelog.dir
|
26
43
|
@@dir
|
27
44
|
end
|
28
|
-
|
45
|
+
|
46
|
+
# Where will you save your log? Defaults to "."
|
29
47
|
def Simplelog.dir=(dir)
|
30
48
|
@@dir = dir.sub(/\\*$/, "")
|
31
49
|
end
|
32
50
|
|
33
|
-
def Simplelog.
|
34
|
-
@@
|
51
|
+
def Simplelog.logtext
|
52
|
+
@@logtext
|
35
53
|
end
|
36
54
|
|
55
|
+
# Allows you to clear your log if you so wish
|
37
56
|
def Simplelog.clear
|
38
|
-
@
|
57
|
+
@logtext = ""
|
39
58
|
end
|
40
59
|
|
41
60
|
def Simplelog.puts(text)
|
42
61
|
old_puts text
|
43
|
-
@@
|
62
|
+
@@logtext << text << "\n"
|
44
63
|
end
|
45
64
|
|
65
|
+
# Directory and filename. Subsequent underscores are removed.
|
46
66
|
def Simplelog.filename
|
47
|
-
"#@@dir
|
67
|
+
"#@@dir\\#{@@name}_#{Date.today.to_s}.txt".sub("__", "_")
|
48
68
|
end
|
49
69
|
|
50
70
|
def Simplelog.save
|
51
|
-
if !@@
|
71
|
+
if !@@logtext.empty?
|
52
72
|
Dir::mkdir(@@dir) if !FileTest.directory?(@@dir)
|
53
73
|
File.open(filename, "w") do |f|
|
54
|
-
f.puts @@
|
74
|
+
f.puts @@logtext
|
55
75
|
end
|
56
76
|
end
|
57
77
|
end
|
data/test/simplelogtest.rb
CHANGED
@@ -3,10 +3,6 @@ require 'lib/simplelog'
|
|
3
3
|
|
4
4
|
class Simplelogtest < Test::Unit::TestCase
|
5
5
|
|
6
|
-
def test_naive
|
7
|
-
assert (2 < 3)
|
8
|
-
end
|
9
|
-
|
10
6
|
def setup
|
11
7
|
@dirname = "testlogs-please_delete"
|
12
8
|
@logname = "name_for_a_log"
|
@@ -24,31 +20,38 @@ class Simplelogtest < Test::Unit::TestCase
|
|
24
20
|
assert_equal(Simplelog.dir, @dirname)
|
25
21
|
end
|
26
22
|
|
27
|
-
def
|
23
|
+
def test_puts
|
28
24
|
puts "Some more text for tests"
|
29
|
-
puts "We are
|
30
|
-
assert_match(/testing\n$/, Simplelog.
|
25
|
+
puts "We are testing"
|
26
|
+
assert_match(/testing\n$/, Simplelog.logtext)
|
31
27
|
end
|
32
28
|
|
29
|
+
def test_p
|
30
|
+
p "Don't log this"
|
31
|
+
assert_no_match(/Don't log this/, Simplelog.logtext)
|
32
|
+
puts "But log this"
|
33
|
+
assert_match(/But log this/, Simplelog.logtext)
|
34
|
+
end
|
35
|
+
|
36
|
+
#Make sure that file.puts works as it
|
33
37
|
def test_file_puts
|
34
|
-
|
35
|
-
|
36
|
-
File.open(output, "w") do |file|
|
38
|
+
output_file = "output.txt"
|
39
|
+
File.open(output_file, "w") do |file|
|
37
40
|
puts "This should be logged"
|
38
|
-
file.puts
|
41
|
+
file.puts "This should not be logged"
|
39
42
|
end
|
40
|
-
assert_match(/should be logged\n/, Simplelog.
|
41
|
-
assert_no_match(/
|
42
|
-
assert(FileTest.file?(
|
43
|
-
File.open(
|
44
|
-
assert_equal(
|
43
|
+
assert_match(/should be logged\n/, Simplelog.logtext)
|
44
|
+
assert_no_match(/should not be logged/, Simplelog.logtext)
|
45
|
+
assert(FileTest.file?(output_file))
|
46
|
+
File.open(output_file) do |file|
|
47
|
+
assert_equal("This should not be logged", file.readline.chomp)
|
45
48
|
end
|
46
|
-
File.delete(
|
47
|
-
assert(!FileTest.file?(
|
49
|
+
File.delete(output_file)
|
50
|
+
assert(!FileTest.file?(output_file))
|
48
51
|
end
|
49
52
|
|
50
53
|
def test_filename
|
51
|
-
fname = "#@dirname
|
54
|
+
fname = "#@dirname\\#{@logname}_#{Date.today.to_s}.txt"
|
52
55
|
assert_equal(Simplelog.filename, fname)
|
53
56
|
end
|
54
57
|
|
@@ -59,7 +62,8 @@ class Simplelogtest < Test::Unit::TestCase
|
|
59
62
|
assert(FileTest.file?(Simplelog.filename))
|
60
63
|
File.delete(Simplelog.filename)
|
61
64
|
Dir.delete(Simplelog.dir)
|
65
|
+
Simplelog.dir
|
62
66
|
assert(!FileTest.directory?(Simplelog.dir))
|
63
67
|
assert(!FileTest.file?(Simplelog.filename))
|
64
68
|
end
|
65
|
-
end
|
69
|
+
end
|
metadata
CHANGED
@@ -3,9 +3,9 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: simplelog
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2006-07-
|
8
|
-
summary: The simplest logger ever.
|
6
|
+
version: 0.1.2
|
7
|
+
date: 2006-07-20 00:00:00 +02:00
|
8
|
+
summary: The simplest logger ever. Just require 'simplelog' and all puts statements are logged to .txt as well as shown on screen.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: jes@luretanker.no
|