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.
Files changed (3) hide show
  1. data/lib/simplelog.rb +32 -12
  2. data/test/simplelogtest.rb +24 -20
  3. 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
- @@loggtext = ""
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.loggtext
34
- @@loggtext
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
- @loggtext = ""
57
+ @logtext = ""
39
58
  end
40
59
 
41
60
  def Simplelog.puts(text)
42
61
  old_puts text
43
- @@loggtext << text << "\n"
62
+ @@logtext << text << "\n"
44
63
  end
45
64
 
65
+ # Directory and filename. Subsequent underscores are removed.
46
66
  def Simplelog.filename
47
- "#@@dir\\LOG_#{@@name}_#{Date.today.to_s}.txt".sub("__", "_")
67
+ "#@@dir\\#{@@name}_#{Date.today.to_s}.txt".sub("__", "_")
48
68
  end
49
69
 
50
70
  def Simplelog.save
51
- if !@@loggtext.empty?
71
+ if !@@logtext.empty?
52
72
  Dir::mkdir(@@dir) if !FileTest.directory?(@@dir)
53
73
  File.open(filename, "w") do |f|
54
- f.puts @@loggtext
74
+ f.puts @@logtext
55
75
  end
56
76
  end
57
77
  end
@@ -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 test_loggtext
23
+ def test_puts
28
24
  puts "Some more text for tests"
29
- puts "We are still testing"
30
- assert_match(/testing\n$/, Simplelog.loggtext)
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
- output = "output.txt"
35
- not_logged = "This should not be logged in the log"
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 not_logged
41
+ file.puts "This should not be logged"
39
42
  end
40
- assert_match(/should be logged\n/, Simplelog.loggtext)
41
- assert_no_match(/output.txt/, Simplelog.loggtext)
42
- assert(FileTest.file?(output))
43
- File.open(output) do |file|
44
- assert_equal(not_logged, file.readline.chomp)
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(output)
47
- assert(!FileTest.file?(output))
49
+ File.delete(output_file)
50
+ assert(!FileTest.file?(output_file))
48
51
  end
49
52
 
50
53
  def test_filename
51
- fname = "#@dirname\\LOG_#{@logname}_#{Date.today.to_s}.txt"
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.1
7
- date: 2006-07-19 00:00:00 +02:00
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