simplelog 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/simplelog.rb +61 -0
  2. data/license.txt +18 -0
  3. data/test/simplelogtest.rb +65 -0
  4. metadata +47 -0
data/lib/simplelog.rb ADDED
@@ -0,0 +1,61 @@
1
+
2
+ require 'date'
3
+
4
+
5
+ module Kernel
6
+ alias old_puts puts
7
+ def puts(text)
8
+ Simplelog.puts(text)
9
+ end
10
+ end
11
+
12
+ class Simplelog
13
+ @@dir = "."
14
+ @@name = ""
15
+ @@loggtext = ""
16
+
17
+ def Simplelog.name
18
+ @@name
19
+ end
20
+
21
+ def Simplelog.name=(name)
22
+ @@name = name
23
+ end
24
+
25
+ def Simplelog.dir
26
+ @@dir
27
+ end
28
+
29
+ def Simplelog.dir=(dir)
30
+ @@dir = dir.sub(/\\*$/, "")
31
+ end
32
+
33
+ def Simplelog.loggtext
34
+ @@loggtext
35
+ end
36
+
37
+ def Simplelog.clear
38
+ @loggtext = ""
39
+ end
40
+
41
+ def Simplelog.puts(text)
42
+ old_puts text
43
+ @@loggtext << text << "\n"
44
+ end
45
+
46
+ def Simplelog.filename
47
+ "#@@dir\\LOG_#{@@name}_#{Date.today.to_s}.txt".sub("__", "_")
48
+ end
49
+
50
+ def Simplelog.save
51
+ if !@@loggtext.empty?
52
+ Dir::mkdir(@@dir) if !FileTest.directory?(@@dir)
53
+ File.open(filename, "w") do |f|
54
+ f.puts @@loggtext
55
+ end
56
+ end
57
+ end
58
+
59
+ at_exit { save }
60
+ end
61
+
data/license.txt ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2006 Jon Egil Strand (jes@luretanker.no)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,65 @@
1
+ require 'test/unit'
2
+ require 'lib/simplelog'
3
+
4
+ class Simplelogtest < Test::Unit::TestCase
5
+
6
+ def test_naive
7
+ assert (2 < 3)
8
+ end
9
+
10
+ def setup
11
+ @dirname = "testlogs-please_delete"
12
+ @logname = "name_for_a_log"
13
+ Simplelog.name = @logname
14
+ Simplelog.dir = @dirname
15
+ end
16
+
17
+ def test_name
18
+ assert_equal(Simplelog.name, @logname)
19
+ end
20
+
21
+ def test_dir
22
+ assert_equal(Simplelog.dir, @dirname)
23
+ Simplelog.dir= @dirname + "\\\\\\"
24
+ assert_equal(Simplelog.dir, @dirname)
25
+ end
26
+
27
+ def test_loggtext
28
+ puts "Some more text for tests"
29
+ puts "We are still testing"
30
+ assert_match(/testing\n$/, Simplelog.loggtext)
31
+ end
32
+
33
+ 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|
37
+ puts "This should be logged"
38
+ file.puts not_logged
39
+ 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)
45
+ end
46
+ File.delete(output)
47
+ assert(!FileTest.file?(output))
48
+ end
49
+
50
+ def test_filename
51
+ fname = "#@dirname\\LOG_#{@logname}_#{Date.today.to_s}.txt"
52
+ assert_equal(Simplelog.filename, fname)
53
+ end
54
+
55
+ def test_save
56
+ puts "Oh test, how we love thy"
57
+ Simplelog.save
58
+ assert(FileTest.directory?(Simplelog.dir))
59
+ assert(FileTest.file?(Simplelog.filename))
60
+ File.delete(Simplelog.filename)
61
+ Dir.delete(Simplelog.dir)
62
+ assert(!FileTest.directory?(Simplelog.dir))
63
+ assert(!FileTest.file?(Simplelog.filename))
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: simplelog
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.
9
+ require_paths:
10
+ - lib
11
+ email: jes@luretanker.no
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Jon Egil Strand
31
+ files:
32
+ - lib/simplelog.rb
33
+ - license.txt
34
+ test_files:
35
+ - test/simplelogtest.rb
36
+ rdoc_options: []
37
+
38
+ extra_rdoc_files:
39
+ - license.txt
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ requirements: []
45
+
46
+ dependencies: []
47
+