sappho-basics 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,13 +21,23 @@ module Sappho
21
21
  'fatal' => Logger::FATAL
22
22
  }
23
23
  LOG_DETAIL = {
24
- 'message' => proc { |severity, datetime, progname, message| "#{message}\n" }
24
+ 'message' => proc { |severity, datetime, progname, message| "#{message}\n" },
25
+ 'test' => proc { |severity, datetime, progname, message| "#{severity} #{message}\n" }
25
26
  }
27
+ @@file = nil
28
+
29
+ def AutoFlushLog.file file
30
+ @@file = file
31
+ end
26
32
 
27
33
  def initialize
28
34
  @mutex = Mutex.new
29
- filename = ENV['application.log.filename']
30
- @log = Logger.new(filename ? File.open(filename, 'a') : $stdout)
35
+ unless @@file
36
+ filename = ENV['application.log.filename']
37
+ mode = File.exists?(filename) ? 'a' : 'w' if filename
38
+ @@file = filename ? File.open(filename, mode) : $stdout
39
+ end
40
+ @log = Logger.new @@file
31
41
  level = ENV['application.log.level']
32
42
  @log.level = LOG_LEVELS.has_key?(level) ? LOG_LEVELS[level] : Logger::INFO
33
43
  detail = ENV['application.log.detail']
@@ -65,7 +75,7 @@ module Sappho
65
75
 
66
76
  def fatal error
67
77
  @mutex.synchronize do
68
- @log.fatal "fatal error! #{error.message}"
78
+ @log.fatal "error! #{error.message}"
69
79
  error.backtrace.each { |error| @log.fatal error }
70
80
  $stdout.flush
71
81
  end if @log.fatal?
@@ -5,7 +5,7 @@
5
5
 
6
6
  module Sappho
7
7
  NAME = 'sappho-basics'
8
- VERSION = '0.0.6'
8
+ VERSION = '0.0.7'
9
9
  AUTHORS = ['Andrew Heald']
10
10
  EMAILS = ['andrew@heald.co.uk']
11
11
  HOMEPAGE = 'https://github.com/sappho/sappho-basics/wiki'
@@ -0,0 +1,47 @@
1
+ # See https://github.com/sappho/sappho-basics/wiki for project documentation.
2
+ # This software is licensed under the GNU Affero General Public License, version 3.
3
+ # See http://www.gnu.org/licenses/agpl.html for full details of the license terms.
4
+ # Copyright 2012 Andrew Heald.
5
+
6
+ require 'test/unit'
7
+ require 'stringio'
8
+ require 'sappho-basics/auto_flush_log'
9
+
10
+ class AutoFlushLogTest < Test::Unit::TestCase
11
+
12
+ include Sappho::LogUtilities
13
+
14
+ def test_logging
15
+ file = StringIO.new
16
+ Sappho::AutoFlushLog.file file
17
+ assert_raises NoMethodError do
18
+ Sappho::AutoFlushLog.new
19
+ end
20
+ ENV['application.log.level'] = 'debug'
21
+ ENV['application.log.detail'] = 'test'
22
+ log = Sappho::AutoFlushLog.instance
23
+ assert log.debug?
24
+ log.debug 'Test 1'
25
+ log.info 'Test 2'
26
+ log.warn 'Test 3'
27
+ begin
28
+ raise 'Test 4'
29
+ rescue => error
30
+ log.error error
31
+ end
32
+ begin
33
+ raise 'Test 5'
34
+ rescue => error
35
+ log.fatal error
36
+ end
37
+ assert_match /DEBUG Test 1/m, file.string
38
+ assert_match /INFO Test 2/m, file.string
39
+ assert_match /WARN Test 3/m, file.string
40
+ assert_match /ERROR error! Test 4/m, file.string
41
+ assert_match /ERROR .+\/auto_flush_log_test.rb:\d+:in `test_logging'/m, file.string
42
+ assert_match /FATAL error! Test 5/m, file.string
43
+ assert_match /FATAL .+\/auto_flush_log_test.rb:\d+:in `test_logging'/m, file.string
44
+ assert_equal '61 62 63 78 79 7a ', hexString('abcxyz'.unpack('c*'))
45
+ end
46
+
47
+ end
@@ -0,0 +1,62 @@
1
+ # See https://github.com/sappho/sappho-basics/wiki for project documentation.
2
+ # This software is licensed under the GNU Affero General Public License, version 3.
3
+ # See http://www.gnu.org/licenses/agpl.html for full details of the license terms.
4
+ # Copyright 2012 Andrew Heald.
5
+
6
+ require 'test/unit'
7
+ require 'sappho-basics/module_register'
8
+
9
+ class ModuleRegisterTest < Test::Unit::TestCase
10
+
11
+ def test_module_register
12
+ assert_raises NoMethodError do
13
+ Sappho::ModuleRegister.new
14
+ end
15
+ modules = Sappho::ModuleRegister.instance
16
+ assert_nil modules.get(:missing)
17
+ assert !modules.set?(:missing)
18
+ assert modules.set?(:log)
19
+ assert_instance_of Sappho::AutoFlushLog, modules.get(:log)
20
+ test1 = TestModule.new
21
+ test2 = TestModule.new
22
+ modules.set :test1, test1
23
+ modules.set :test2, test2
24
+ modules.set :test1, test1
25
+ assert_equal test1, modules.get(:test1)
26
+ assert_equal test2, modules.get(:test2)
27
+ assert modules.set?(:test1)
28
+ assert modules.set?(:test2)
29
+ assert test1.alive
30
+ assert test2.alive
31
+ modules.shutdown
32
+ assert !test1.alive
33
+ assert !test2.alive
34
+ assert_instance_of Sappho::AutoFlushLog, modules.get(:log)
35
+ assert_equal test1, modules.get(:test1)
36
+ assert_equal test2, modules.get(:test2)
37
+ test1.alive = true
38
+ test2.alive = true
39
+ modules.shutdown
40
+ assert !test1.alive
41
+ assert !test2.alive
42
+ assert_instance_of Sappho::AutoFlushLog, modules.get(:log)
43
+ assert_equal test1, modules.get(:test1)
44
+ assert_equal test2, modules.get(:test2)
45
+ end
46
+
47
+ class TestModule
48
+
49
+ attr_accessor :alive
50
+
51
+ def initialize
52
+ @alive = true
53
+ end
54
+
55
+ def shutdown
56
+ @alive = false
57
+ raise 'ignored error'
58
+ end
59
+
60
+ end
61
+
62
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sappho-basics
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Heald
@@ -47,6 +47,8 @@ files:
47
47
  - lib/sappho-basics/auto_flush_log.rb
48
48
  - lib/sappho-basics/module_register.rb
49
49
  - lib/sappho-basics/version.rb
50
+ - test/ruby/auto_flush_log_test.rb
51
+ - test/ruby/module_register_test.rb
50
52
  homepage: https://github.com/sappho/sappho-basics/wiki
51
53
  licenses: []
52
54
 
@@ -80,5 +82,6 @@ rubygems_version: 1.8.11
80
82
  signing_key:
81
83
  specification_version: 3
82
84
  summary: Provides a small set of basic classes and modules to Sapphic apps
83
- test_files: []
84
-
85
+ test_files:
86
+ - test/ruby/auto_flush_log_test.rb
87
+ - test/ruby/module_register_test.rb