dumb-logger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ # -*- coding: utf-8 -*-
2
+ #--
3
+ # Copyright © 2015 Ken Coar
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #++
17
+
18
+ Proc.new {
19
+ libdir = File.join(File.dirname(__FILE__), 'lib')
20
+ xlibdir = File.expand_path(libdir)
21
+ $:.unshift(xlibdir) unless ($:.include?(libdir) || $:.include?(xlibdir))
22
+ }.call
23
+ require('dumb-logger/version')
24
+
25
+ Gem::Specification.new do |spec|
26
+ spec.name = 'dumb-logger'
27
+ spec.version = DumbLogger::VERSION
28
+ spec.authors = [
29
+ 'Ken Coar',
30
+ ]
31
+ spec.email = [
32
+ 'kcoar@redhat.com',
33
+ ]
34
+ spec.summary = %q{Primitive level/mask-driven stream logger.}
35
+ spec.description = %q{Primitive no-frills level/mask-driven stream logger.}
36
+ spec.homepage = ''
37
+ spec.license = 'Apache 2.0'
38
+
39
+ spec.files = `git ls-files -z`.split("\x0")
40
+ spec.executables = spec.files.grep(%r!^bin/!) { |f| File.basename(f) }
41
+ spec.test_files = spec.files.grep(%r!^(test|spec|features)/!)
42
+ spec.require_paths = [
43
+ 'lib',
44
+ ]
45
+
46
+ spec.add_development_dependency('bundler', '~> 1.7')
47
+ spec.add_development_dependency('rake', '~> 10.0')
48
+
49
+ spec.add_dependency('versionomy')
50
+ end
@@ -0,0 +1,70 @@
1
+ Feature: Test default attributes and setting/pinging them
2
+ In order to use the basic functionality of the DumbLogger class
3
+ A developer
4
+ Should be able to manipulate all of the logger's attributes
5
+
6
+ # Things to test:
7
+ # o constructor
8
+ # - no args
9
+ # - :append => {true,false,bogus}
10
+ # - :level_style => {USE_LEVELS,USE_BITMASK,bogus}
11
+ # - :loglevel => {number,bogus}
12
+ # - :logmask => {number,bogus}
13
+ # - :prefix => {string,bogus}
14
+ # - :sink => {path,IO,bogus}
15
+
16
+ Background:
17
+ Given I have a DumbLogger object
18
+
19
+ Scenario: Check the defaults
20
+ Then the log-level should be 0
21
+ And the sink should be :$stderr
22
+ And the style should be DumbLogger::USE_LEVELS
23
+ And the prefix should be ''
24
+ And append-mode should be true
25
+
26
+ Scenario: Test setting the logging level to an integer
27
+ When I set attribute loglevel to 5
28
+ Then the return value should be 5
29
+ And the log-level should be 5
30
+
31
+ Scenario: Test setting the logging level to a float
32
+ When I set attribute loglevel to 5.7
33
+ Then the return value should be 5
34
+ And the log-level should be 5
35
+
36
+ Scenario: Test setting the logging level to a string
37
+ When I set attribute loglevel to "5"
38
+ Then the return value should be 5
39
+ And the log-level should be 5
40
+
41
+ Scenario: Test setting the logging level to something bogus
42
+ When I set attribute loglevel to Object
43
+ Then it should raise an exception of type ArgumentError
44
+ And the log-level should be 0
45
+
46
+ Scenario: Test setting the logging style
47
+ When I set attribute level_style to DumbLogger::USE_BITMASK
48
+ Then the return value should be DumbLogger::USE_BITMASK
49
+ And the style should be DumbLogger::USE_BITMASK
50
+
51
+ Scenario: Test setting the prefix
52
+ When I set attribute prefix to 'Testing: '
53
+ Then the return value should be 'Testing: '
54
+ And the prefix should be 'Testing: '
55
+
56
+ Scenario: Test turning off append-mode
57
+ When I set attribute append to false
58
+ Then the return value should be false
59
+ And append-mode should be false
60
+
61
+ Scenario: Test changing the sink to a path
62
+ When I set attribute 'sink' to '/dev/null'
63
+ Then the return value should be '/dev/null'
64
+ And the sink should be '/dev/null'
65
+
66
+ Scenario: Test changing the sink to stdout
67
+ When I set attribute 'sink' to :$stdout
68
+ Then the return value should be :$stdout
69
+ And the sink should be :$stdout
70
+
@@ -0,0 +1,44 @@
1
+ Feature: Test reporting using bitmasks
2
+ In order to use the basic functionality of the DumbLogger class
3
+ A developer
4
+ Should be able to assign meaning to bits in a mask
5
+
6
+ Background:
7
+ Given I have a DumbLogger object
8
+ And I set attribute 'sink' to :$stderr
9
+ And I set attribute level_style to DumbLogger::USE_BITMASK
10
+ And I set attribute loglevel to 0b01101
11
+
12
+ Scenario: Default Level-0 1-liner text always gets sent and returns 0)
13
+ When I invoke the logger with ("a message")
14
+ Then the return value should be 0
15
+ And stderr should contain exactly "a message\n"
16
+
17
+ Scenario: Default Level-0 multi-line text always gets sent and returns 0
18
+ When I invoke the logger with ("a message line 1","message line 2")
19
+ Then the return value should be 0
20
+ And stderr should contain exactly:
21
+ """
22
+ a message line 1
23
+ message line 2
24
+
25
+ """
26
+ #
27
+ # Note the final blank line above indicating the trailing newline
28
+ #
29
+
30
+ Scenario: Explicit out-of-mask message gets ignored and returns nil
31
+ When I invoke the logger with (0b10010, "a message")
32
+ Then the return value should be nil
33
+ And stderr should contain exactly ""
34
+
35
+ Scenario: Explicit out-of-mask multi-line text is ignored and returns nil
36
+ When I invoke the logger with (0b10010, "message line 1", "message line 2")
37
+ Then the return value should be nil
38
+ And stderr should contain exactly ""
39
+
40
+ Scenario: Explicit in-mask message is sent and the matching mask returned
41
+ When I invoke the logger with (0b10111, "a message")
42
+ Then the return value should be 0b00101
43
+ And stderr should contain exactly "a message\n"
44
+
@@ -0,0 +1,39 @@
1
+ Feature: Test reporting using labeled levels
2
+ In order to report using different methods,
3
+ A developer
4
+ Should be able to assign labels to loglevels.
5
+
6
+ Background:
7
+ Given I have a DumbLogger object
8
+ And I set 'sink' to :$stderr
9
+ And I set 'level_style' to DumbLogger::USE_LEVELS
10
+ And I set 'loglevel' to 5
11
+
12
+ Scenario: Assign 'ok' to level 0
13
+ When I label level 0 with name "ok"
14
+ And I invoke the logger with ok("a message")
15
+ Then the return value should be 0
16
+ And stderr should contain exactly "a message\n"
17
+
18
+ Scenario: Assign 'ok' to level 0 and 'debug' to level 1
19
+ When I label level 0 with name "ok"
20
+ And I label level 1 with name "debug"
21
+ And I invoke the logger with debug("a message")
22
+ Then the return value should be 1
23
+ And stderr should contain exactly "a message\n"
24
+
25
+ Scenario: Assign 'ok' to level 0, 'debug' to 1, and 'silent' to 6
26
+ When I label level 0 with name "ok"
27
+ And I label level 1 with name "debug"
28
+ And I label level 6 with name "silent"
29
+ And I invoke the logger with silent("a message")
30
+ Then the return value should be nil
31
+ And stderr should contain exactly ""
32
+
33
+ Scenario: Check the label assignments
34
+ When I label level 0 with name "ok"
35
+ And I label level 1 with name "debug"
36
+ And I label level 6 with name "silent"
37
+ And I query method "labeled_levels"
38
+ Then the return value should be {:debug=>1, :ok=>0, :silent=>6}
39
+
@@ -0,0 +1,46 @@
1
+ Feature: Test reporting using numeric levels
2
+ In order to use the basic functionality of the DumbLogger class
3
+ A developer
4
+ Should be able to specify at what levels messages should be written
5
+
6
+ Background:
7
+ Given I have a DumbLogger object
8
+ And I set attribute 'sink' to :$stderr
9
+ And I set attribute level_style to DumbLogger::USE_LEVELS
10
+ And I set attribute loglevel to 5
11
+
12
+ Scenario: Default Level-0 1-liner text always gets sent and returns 0)
13
+ When I invoke the logger with ("a message")
14
+ Then the return value should be 0
15
+ And stderr should contain exactly:
16
+ """
17
+ a message
18
+
19
+ """
20
+ #
21
+ # Note the final blank line above indicating the trailing newline
22
+ #
23
+
24
+ Scenario: Default Level-0 multi-line text always gets sent and returns 0
25
+ When I invoke the logger with ("a message line 1","message line 2")
26
+ Then the return value should be 0
27
+ And stderr should contain exactly:
28
+ """
29
+ a message line 1
30
+ message line 2
31
+
32
+ """
33
+ #
34
+ # Note the final blank line above indicating the trailing newline
35
+ #
36
+
37
+ Scenario: Explicit level-too-high 1-liner text is ignored and returns nil
38
+ When I invoke the logger with (6, "a message")
39
+ Then stderr should contain exactly ""
40
+ And the return value should be nil
41
+
42
+ Scenario: Explicit level-too-high multi-line text is ignored and returns nil
43
+ When I invoke the logger with (6, "a message line 1", "message line 2")
44
+ Then stderr should contain exactly ""
45
+ And the return value should be nil
46
+
@@ -0,0 +1,136 @@
1
+ Feature: Test the output generated
2
+ In order to use the basic functionality of the DumbLogger class
3
+ A developer
4
+ Should be able to depend on the correct output
5
+
6
+ Background:
7
+ Given I have a DumbLogger object
8
+ And I set attribute 'sink' to :$stderr
9
+ And I set attribute level_style to DumbLogger::USE_LEVELS
10
+ And I set attribute loglevel to 5
11
+
12
+ Scenario: Single-line message sent to stderr
13
+ When I invoke the logger with ("a message")
14
+ Then the return value should be 0
15
+ And stderr should contain exactly "a message\n"
16
+ And stdout should contain exactly ""
17
+
18
+ Scenario: Single-line message sent to stdout
19
+ When I set the sink to :$stdout
20
+ And I invoke the logger with ("a message")
21
+ Then the return value should be 0
22
+ And stderr should contain exactly ""
23
+ And stdout should contain exactly "a message\n"
24
+
25
+ Scenario: Multi-line message sent to stderr
26
+ When I invoke the logger with ("line 1","line 2")
27
+ Then the return value should be 0
28
+ And stderr should contain exactly "line 1\nline 2\n"
29
+ And stdout should contain exactly ""
30
+
31
+ Scenario: Multi-line message sent to stdout
32
+ When I set the sink to :$stdout
33
+ And I invoke the logger with ("line 1","line 2")
34
+ Then the return value should be 0
35
+ And stderr should contain exactly ""
36
+ And stdout should contain exactly "line 1\nline 2\n"
37
+
38
+ Scenario: Single-line message with no newline
39
+ When I invoke the logger with ("a message", :no_nl)
40
+ Then the return value should be 0
41
+ And stderr should contain exactly "a message"
42
+ And stdout should contain exactly ""
43
+
44
+ Scenario: Single-line message with no newline (alternate)
45
+ When I invoke the logger with ("a message", :newline=>false)
46
+ Then the return value should be 0
47
+ And stderr should contain exactly "a message"
48
+ And stdout should contain exactly ""
49
+
50
+ Scenario: Multi-line message with no newline
51
+ When I invoke the logger with ("line 1",:no_nl,"line 2")
52
+ Then the return value should be 0
53
+ And stderr should contain exactly "line 1\nline 2"
54
+ And stdout should contain exactly ""
55
+
56
+ Scenario: Multi-line message with no newline (alternate)
57
+ When I invoke the logger with ("line 1",{:newline=>false},"line 2")
58
+ Then the return value should be 0
59
+ And stderr should contain exactly "line 1\nline 2"
60
+ And stdout should contain exactly ""
61
+
62
+ Scenario: Single-line message with instance prefix
63
+ When I set the prefix to '[instance-prefix] '
64
+ And I invoke the logger with ("a message")
65
+ Then the prefix should be '[instance-prefix] '
66
+ And the return value should be 0
67
+ And stderr should contain exactly "[instance-prefix] a message\n"
68
+ And stdout should contain exactly ""
69
+
70
+ Scenario: Multi-line message with instance prefix
71
+ When I set the prefix to '[instance-prefix] '
72
+ And I invoke the logger with ("line 1","line 2")
73
+ Then the prefix should be '[instance-prefix] '
74
+ And the return value should be 0
75
+ And stderr should contain exactly:
76
+ """
77
+ [instance-prefix] line 1
78
+ [instance-prefix] line 2
79
+
80
+ """
81
+ And stdout should contain exactly ""
82
+
83
+ Scenario: Single-line message with method prefix
84
+ When I invoke the logger with ("a message", :prefix => "[method-prefix] ")
85
+ Then the prefix should be ''
86
+ And the return value should be 0
87
+ And stderr should contain exactly "[method-prefix] a message\n"
88
+ And stdout should contain exactly ""
89
+
90
+ Scenario: Multi-line message with method prefix
91
+ When I invoke the logger with ("line 1","line 2",:prefix => "[method-prefix] ")
92
+ Then the prefix should be ''
93
+ And the return value should be 0
94
+ And stderr should contain exactly:
95
+ """
96
+ [method-prefix] line 1
97
+ [method-prefix] line 2
98
+
99
+ """
100
+ And stdout should contain exactly ""
101
+
102
+ Scenario: Single-line message with differing instance and method prefices
103
+ When I set the prefix to '[instance-prefix] '
104
+ And I invoke the logger with ("a message", :prefix => "[method-prefix] ")
105
+ Then the prefix should be '[instance-prefix] '
106
+ And the return value should be 0
107
+ And stderr should contain exactly "[method-prefix] a message\n"
108
+ And stdout should contain exactly ""
109
+
110
+ Scenario: Multi-line message with differing method and instance prefices
111
+ When I set the prefix to '[instance-prefix] '
112
+ And I invoke the logger with ("line 1","line 2",:prefix => "[method-prefix] ")
113
+ Then the prefix should be '[instance-prefix] '
114
+ And the return value should be 0
115
+ And stderr should contain exactly:
116
+ """
117
+ [method-prefix] line 1
118
+ [method-prefix] line 2
119
+
120
+ """
121
+ And stdout should contain exactly ""
122
+
123
+ Scenario: Single-line message with multiple options
124
+ When I invoke the logger with ("line 1",:newline=>false,:prefix=>"[method-prefix] ")
125
+ Then the prefix should be ''
126
+ And the return value should be 0
127
+ And stderr should contain exactly "[method-prefix] line 1"
128
+ And stdout should contain exactly ""
129
+
130
+ Scenario: Multi-line message with method prefix
131
+ When I invoke the logger with ("line 1",{:newline=>false,:prefix=>"[method-prefix] "},"line 2")
132
+ Then the prefix should be ''
133
+ And the return value should be 0
134
+ And stderr should contain exactly "[method-prefix] line 1\n[method-prefix] line 2"
135
+ And stdout should contain exactly ""
136
+
@@ -0,0 +1,97 @@
1
+ Given(/^I have a DumbLogger object$/) do
2
+ @duml = DumbLogger.new
3
+ end
4
+
5
+ And(/^it is sinking to (\S+)$/) do |sink|
6
+ @return_value = @duml.sink = eval(sink)
7
+ end
8
+
9
+ And(/^the logging style is (\S+)$/) do |style|
10
+ @return_value = @duml.level_style = DumbLogger.const_get(style)
11
+ end
12
+
13
+ And(/^append mode is set to (\S+)$/) do |mode|
14
+ @return_value = @duml.append = eval(mode)
15
+ end
16
+
17
+ And(/^the prefix is set to "([^"]*)"$/) do |prefix|
18
+ @return_value = @duml.prefix = prefix
19
+ end
20
+
21
+ And(/^the loglevel is set to (\S+)$/) do |level|
22
+ @return_value = @duml.loglevel = level.to_i
23
+ end
24
+
25
+ And(/^I label (?:level|mask|bitmask) (\d+) with name "(.+)"$/) do |level,label|
26
+ @return_value = @duml.label_levels(eval("{#{label.to_sym.inspect}=>#{level}}"))
27
+ end
28
+
29
+ And(/^I label loglevels with:$/) do |labelhash|
30
+ @return_value = @duml.label_levels(eval("{#{labelhash}}"))
31
+ end
32
+
33
+ And(/^I invoke the logger with (\S*)\((.*)\)$/) do |label,args|
34
+ traffic = capture_streams(:$stdout, :$stderr) {
35
+ invocation = "@duml.#{label.empty? ? 'message' : label}(#{args})"
36
+ @return_value = eval(invocation)
37
+ }
38
+ @stdout_text = traffic[:$stdout]
39
+ @stderr_text = traffic[:$stderr]
40
+ end
41
+
42
+ Then(/^stdout should contain exactly (.+)$/) do |xval|
43
+ expect(@stdout_text).to eq eval(xval)
44
+ end
45
+
46
+ Then(/^stdout should contain exactly:$/) do |xval|
47
+ expect(@stdout_text).to eq xval
48
+ end
49
+
50
+ Then(/^stderr should contain exactly (.+)$/) do |xval|
51
+ expect(@stderr_text).to eq eval(xval)
52
+ end
53
+
54
+ Then(/^stderr should contain exactly:$/) do |xval|
55
+ expect(@stderr_text).to eq xval
56
+ end
57
+
58
+ Then(/^the return value should be (.*)$/) do |xval|
59
+ expect(@return_value).to eq eval(xval)
60
+ end
61
+
62
+ Then(/^the (?:log-level|logging-mask) should\s+(?:still)?\s*be (\d+)$/) do |xval|
63
+ expect(@duml.loglevel).to eq xval.to_i
64
+ end
65
+
66
+ Then(/^the sink should be (.+)$/) do |xval|
67
+ expect(@duml.sink).to eq eval(xval)
68
+ end
69
+
70
+ Then(/^the style should be (.+)$/) do |xval|
71
+ expect(@duml.level_style).to eq eval(xval)
72
+ end
73
+
74
+ Then(/^the prefix should be (.+)$/) do |xval|
75
+ expect(@duml.prefix).to eq eval(xval)
76
+ end
77
+
78
+ Then(/^append-mode should be (.+)$/) do |xval|
79
+ expect(@duml.append?).to eq eval(xval)
80
+ end
81
+
82
+ When(/^I query (?:attribute|method) ["']?([_A-Za-z0-9?]+)["']?$/) do |attr|
83
+ @return_value = @duml.send(attr.to_sym)
84
+ end
85
+
86
+ When(/^I set (?:attribute|the)?\s*["']?([_A-Za-z0-9]+)["']? to (.+?)$/) do |attr,val|
87
+ begin
88
+ @return_value = @duml.send((attr + '=').to_sym, eval(val))
89
+ rescue Exception => e
90
+ @exception_raised = e
91
+ @return_value = nil
92
+ end
93
+ end
94
+
95
+ Then(/^it should raise an exception of type (\S+)$/) do |xval|
96
+ expect(@exception_raised.class).to eq eval(xval)
97
+ end