devdnsd 1.1.2 → 1.1.9
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +9 -7
- data/devdnsd.gemspec +2 -3
- data/lib/devdnsd/application.rb +11 -9
- data/lib/devdnsd/configuration.rb +7 -26
- data/lib/devdnsd/errors.rb +0 -4
- data/lib/devdnsd/version.rb +1 -1
- data/lib/devdnsd.rb +2 -6
- data/spec/devdnsd/application_spec.rb +4 -6
- data/spec/devdnsd/configuration_spec.rb +2 -32
- data/utils/tester.rb +3 -5
- metadata +14 -33
- data/lib/devdnsd/logger.rb +0 -90
- data/spec/devdnsd/logger_spec.rb +0 -100
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
devdnsd (1.1.
|
5
|
-
|
4
|
+
devdnsd (1.1.9)
|
5
|
+
bovem (>= 0.5.0)
|
6
6
|
gli (~> 1.6.0)
|
7
|
-
rainbow (~> 1.1.0)
|
8
7
|
rexec (~> 1.4.1)
|
9
8
|
rubydns (~> 0.4.0)
|
10
9
|
|
@@ -27,12 +26,11 @@ GEM
|
|
27
26
|
activesupport (3.2.7)
|
28
27
|
i18n (~> 0.6)
|
29
28
|
multi_json (~> 1.0)
|
29
|
+
bovem (0.5.0)
|
30
|
+
lazier (~> 1.0)
|
31
|
+
rainbow (~> 1.1.0)
|
30
32
|
builder (3.0.0)
|
31
33
|
coderay (1.0.7)
|
32
|
-
cowtech-extensions (2.7.1)
|
33
|
-
actionpack (~> 3.0)
|
34
|
-
json (~> 1.7.0)
|
35
|
-
tzinfo (~> 0.3.0)
|
36
34
|
diff-lcs (1.1.3)
|
37
35
|
erubis (2.7.0)
|
38
36
|
eventmachine (0.12.10)
|
@@ -42,6 +40,10 @@ GEM
|
|
42
40
|
i18n (0.6.0)
|
43
41
|
journey (1.0.4)
|
44
42
|
json (1.7.4)
|
43
|
+
lazier (1.0.1)
|
44
|
+
actionpack (~> 3.0)
|
45
|
+
json (~> 1.7.0)
|
46
|
+
tzinfo (~> 0.3.0)
|
45
47
|
method_source (0.8)
|
46
48
|
multi_json (1.3.6)
|
47
49
|
net-dns (0.7.1)
|
data/devdnsd.gemspec
CHANGED
@@ -22,11 +22,10 @@ Gem::Specification.new do |gem|
|
|
22
22
|
gem.require_paths = ["lib"]
|
23
23
|
#gem.platform = Gem::Platform::CURRENT
|
24
24
|
|
25
|
-
gem.add_dependency("
|
26
|
-
gem.add_dependency("cowtech-extensions", "~> 2.7.0")
|
25
|
+
gem.add_dependency("bovem", ">= 0.5.0")
|
27
26
|
gem.add_dependency("gli", "~> 1.6.0")
|
27
|
+
gem.add_dependency("rubydns", "~> 0.4.0")
|
28
28
|
gem.add_dependency("rexec", "~> 1.4.1")
|
29
|
-
gem.add_dependency("rainbow", "~> 1.1.0")
|
30
29
|
|
31
30
|
gem.add_development_dependency("rspec", "~> 2.11.0")
|
32
31
|
gem.add_development_dependency("rake", "~> 0.9.0")
|
data/lib/devdnsd/application.rb
CHANGED
@@ -20,7 +20,7 @@ module DevDNSd
|
|
20
20
|
# The arguments passed via command-line.
|
21
21
|
attr_reader :args
|
22
22
|
|
23
|
-
# The
|
23
|
+
# The logger for this application.
|
24
24
|
attr_accessor :logger
|
25
25
|
|
26
26
|
# Creates a new application.
|
@@ -36,23 +36,25 @@ module DevDNSd
|
|
36
36
|
}
|
37
37
|
|
38
38
|
# Setup logger
|
39
|
-
|
40
|
-
@logger =
|
39
|
+
Bovem::Logger.start_time = Time.now
|
40
|
+
@logger = Bovem::Logger.create(Bovem::Logger.get_real_file(@args[:global][:log_file]) || Bovem::Logger.default_file, Logger::INFO)
|
41
41
|
|
42
42
|
# Open configuration
|
43
43
|
begin
|
44
|
-
|
44
|
+
overrides = {
|
45
45
|
:foreground => @args[:local][:foreground],
|
46
46
|
:log_file => @args[:global][:log_file],
|
47
47
|
:log_level => @args[:global][:log_level],
|
48
48
|
:tld => @args[:global][:tld],
|
49
49
|
:port => @args[:global][:port]
|
50
|
-
}
|
50
|
+
}.reject {|k,v| v.nil? }
|
51
|
+
|
52
|
+
@config = DevDNSd::Configuration.new(@args[:global][:config], overrides, @logger)
|
53
|
+
|
51
54
|
@logger = nil
|
52
55
|
@logger = self.get_logger
|
53
|
-
|
54
|
-
|
55
|
-
@logger ? @logger.fatal(e.message) : DevDNSd::Logger.create("STDERR").fatal("Cannot log to #{config.log_file}. Exiting...")
|
56
|
+
rescue Bovem::Errors::InvalidConfiguration, DevDNSd::Errors::InvalidRule => e
|
57
|
+
@logger ? @logger.fatal(e.message) : Bovem::Logger.create("STDERR").fatal("Cannot log to #{config.log_file}. Exiting...")
|
56
58
|
raise ::SystemExit
|
57
59
|
end
|
58
60
|
|
@@ -71,7 +73,7 @@ module DevDNSd
|
|
71
73
|
#
|
72
74
|
# @return [Logger] The current logger of the application.
|
73
75
|
def get_logger
|
74
|
-
@logger ||=
|
76
|
+
@logger ||= Bovem::Logger.create(@config.foreground ? Bovem::Logger.default_file : @config.log_file, @config.log_level, @log_formatter)
|
75
77
|
end
|
76
78
|
|
77
79
|
# Gets the path for the resolver file.
|
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
module DevDNSd
|
8
8
|
# This class holds the configuration of the applicaton.
|
9
|
-
class Configuration
|
9
|
+
class Configuration < Bovem::Configuration
|
10
10
|
# If to run the server in foreground. Default: `false`.
|
11
11
|
attr_accessor :foreground
|
12
12
|
|
@@ -38,9 +38,10 @@ module DevDNSd
|
|
38
38
|
# ```
|
39
39
|
#
|
40
40
|
# @param file [String] The file to read.
|
41
|
-
# @param application [Application] The application which this configuration is attached to.
|
42
41
|
# @param overrides [Hash] A set of values which override those set in the configuration file.
|
43
|
-
|
42
|
+
# @param logger [Logger] The logger to use for notifications.
|
43
|
+
# @see parse
|
44
|
+
def initialize(file = nil, overrides = {}, logger = nil)
|
44
45
|
@address = "0.0.0.0"
|
45
46
|
@port = 7771
|
46
47
|
@tld = "dev"
|
@@ -49,31 +50,11 @@ module DevDNSd
|
|
49
50
|
@rules = []
|
50
51
|
@foreground = false
|
51
52
|
|
52
|
-
|
53
|
-
begin
|
54
|
-
# Open the file
|
55
|
-
path = ::Pathname.new(file).realpath
|
56
|
-
application.logger.info("Using configuration file #{path}.") if application
|
57
|
-
self.tap do |config|
|
58
|
-
eval(::File.read(path))
|
59
|
-
end
|
60
|
-
|
61
|
-
@log_file = $stdout if @log_file == "STDOUT"
|
62
|
-
@log_file = $stderr if @log_file == "STDERR"
|
63
|
-
rescue ::Errno::ENOENT, ::LoadError
|
64
|
-
rescue ::Exception => e
|
65
|
-
raise DevDNSd::Errors::InvalidConfiguration.new("Config file #{file} is not valid.")
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
# Apply overrides
|
70
|
-
if overrides.is_a?(::Hash) then
|
71
|
-
overrides.each_pair do |k, v|
|
72
|
-
self.send("#{k}=", v) if self.respond_to?("#{k}=") && !v.nil?
|
73
|
-
end
|
74
|
-
end
|
53
|
+
super(file, overrides, logger)
|
75
54
|
|
76
55
|
# Make sure some arguments are of correct type
|
56
|
+
@log_file = $stdout if @log_file == "STDOUT"
|
57
|
+
@log_file = $stderr if @log_file == "STDERR"
|
77
58
|
@port = @port.to_integer
|
78
59
|
@log_level = @log_level.to_integer
|
79
60
|
|
data/lib/devdnsd/errors.rb
CHANGED
@@ -10,9 +10,5 @@ module DevDNSd
|
|
10
10
|
# This exception is raised if a {Rule Rule} is invalid.
|
11
11
|
class InvalidRule < ::ArgumentError
|
12
12
|
end
|
13
|
-
|
14
|
-
# This exception is raised if a {Configuration Configuration} is invalid.
|
15
|
-
class InvalidConfiguration < ::ArgumentError
|
16
|
-
end
|
17
13
|
end
|
18
14
|
end
|
data/lib/devdnsd/version.rb
CHANGED
data/lib/devdnsd.rb
CHANGED
@@ -5,19 +5,15 @@
|
|
5
5
|
#
|
6
6
|
|
7
7
|
require "rubygems"
|
8
|
+
require "bovem"
|
8
9
|
require "rubydns"
|
9
10
|
require "gli"
|
10
11
|
require "rexec/daemon"
|
11
|
-
require "pathname"
|
12
|
-
require "rainbow"
|
13
|
-
require "cowtech-extensions"
|
14
|
-
require "rbconfig"
|
15
12
|
|
16
|
-
|
13
|
+
Lazier.load!(:object)
|
17
14
|
|
18
15
|
require "devdnsd/application"
|
19
16
|
require "devdnsd/configuration"
|
20
17
|
require "devdnsd/errors"
|
21
|
-
require "devdnsd/logger"
|
22
18
|
require "devdnsd/rule"
|
23
19
|
require "devdnsd/version" if !defined?(DevDNSd::Version)
|
@@ -8,7 +8,7 @@ require "spec_helper"
|
|
8
8
|
|
9
9
|
describe DevDNSd::Application do
|
10
10
|
before(:each) do
|
11
|
-
|
11
|
+
Bovem::Logger.stub(:default_file).and_return("/dev/null")
|
12
12
|
end
|
13
13
|
|
14
14
|
let(:log_file) { "/tmp/devdnsd-test-log-#{Time.now.strftime("%Y%m%d-%H%M%S")}" }
|
@@ -53,11 +53,9 @@ describe DevDNSd::Application do
|
|
53
53
|
end
|
54
54
|
|
55
55
|
describe "#perform_server" do
|
56
|
-
let(:application){ DevDNSd::Application.instance({:log_file => log_file, :config => sample_config}, {},
|
56
|
+
let(:application){ DevDNSd::Application.instance({:log_file => log_file, :config => sample_config}, {}, [], true) }
|
57
57
|
|
58
58
|
before(:each) do
|
59
|
-
DevDNSd::Logger.stub(:default_file).and_return($stdout)
|
60
|
-
|
61
59
|
class DevDNSd::Application
|
62
60
|
def on_start
|
63
61
|
Thread.main[:resolver].wakeup if Thread.main[:resolver].try(:alive?)
|
@@ -170,7 +168,7 @@ describe DevDNSd::Application do
|
|
170
168
|
end
|
171
169
|
end
|
172
170
|
|
173
|
-
let(:application){ DevDNSd::Application.instance({:log_file => log_file, :config => sample_config}, {},
|
171
|
+
let(:application){ DevDNSd::Application.instance({:log_file => log_file, :config => sample_config}, {}, [], true) }
|
174
172
|
let(:transaction){ FakeTransaction.new }
|
175
173
|
|
176
174
|
it "should match a valid string request" do
|
@@ -394,7 +392,7 @@ describe DevDNSd::Application do
|
|
394
392
|
::File.unlink(application.resolver_path) if ::File.exists?(application.resolver_path)
|
395
393
|
::File.unlink(application.launch_agent_path) if ::File.exists?(application.launch_agent_path)
|
396
394
|
|
397
|
-
|
395
|
+
Bovem::Logger.stub(:default_file).and_return($stdout)
|
398
396
|
application.action_install
|
399
397
|
application.action_uninstall
|
400
398
|
expect(::File.exists?(application.launch_agent_path)).to be_false
|
@@ -10,7 +10,7 @@ require "tempfile"
|
|
10
10
|
describe DevDNSd::Configuration do
|
11
11
|
class DevDNSd::Application
|
12
12
|
def logger
|
13
|
-
|
13
|
+
Bovem::Logger.new("/dev/null")
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -18,7 +18,7 @@ describe DevDNSd::Configuration do
|
|
18
18
|
|
19
19
|
let(:new_application) {
|
20
20
|
app = DevDNSd::Application.new({:log_file => log_file})
|
21
|
-
app.logger =
|
21
|
+
app.logger = Bovem::Logger.create("/dev/null", Bovem::Logger::DEBUG)
|
22
22
|
app
|
23
23
|
}
|
24
24
|
|
@@ -33,36 +33,6 @@ describe DevDNSd::Configuration do
|
|
33
33
|
expect(config.rules.count).to eq(1)
|
34
34
|
expect(config.foreground).to eq(false)
|
35
35
|
end
|
36
|
-
|
37
|
-
it "reads a valid configuration file" do
|
38
|
-
file = ::Tempfile.new('devdnsd-test')
|
39
|
-
file.write("config.port = 7772")
|
40
|
-
file.close
|
41
|
-
|
42
|
-
config = DevDNSd::Configuration.new(file.path, new_application)
|
43
|
-
expect(config.port).to eq(7772)
|
44
|
-
file.unlink
|
45
|
-
end
|
46
|
-
|
47
|
-
it "reject an invalid configuration" do
|
48
|
-
file = ::Tempfile.new('devdnsd-test')
|
49
|
-
file.write("config.port = ")
|
50
|
-
file.close
|
51
|
-
|
52
|
-
expect { config = DevDNSd::Configuration.new(file.path, new_application)}.to raise_error(DevDNSd::Errors::InvalidConfiguration)
|
53
|
-
file.unlink
|
54
|
-
end
|
55
|
-
|
56
|
-
it "allows overrides" do
|
57
|
-
file = ::Tempfile.new('devdnsd-test')
|
58
|
-
file.write("config.port = 7772")
|
59
|
-
file.close
|
60
|
-
|
61
|
-
config = DevDNSd::Configuration.new(file.path, new_application, {:foreground => true, :port => 7773})
|
62
|
-
expect(config.port).to eq(7773)
|
63
|
-
config.foreground = true
|
64
|
-
file.unlink
|
65
|
-
end
|
66
36
|
end
|
67
37
|
|
68
38
|
describe "#add_rule" do
|
data/utils/tester.rb
CHANGED
@@ -7,11 +7,9 @@
|
|
7
7
|
|
8
8
|
basedir = File.expand_path(File.dirname(__FILE__))
|
9
9
|
require "rubygems"
|
10
|
-
require "
|
10
|
+
require "bovem"
|
11
11
|
require "rainbow"
|
12
|
-
require "active_support/all"
|
13
12
|
require "net/dns"
|
14
|
-
require basedir + "/../lib/devdnsd/logger"
|
15
13
|
|
16
14
|
# Patch to avoid resolving of hostname containing numbers.
|
17
15
|
class Net::DNS::Resolver
|
@@ -74,7 +72,7 @@ end
|
|
74
72
|
def devdnsd_resolv(address = "match.dev", type = "ANY", nameserver = "127.0.0.1", port = 7771, logger = nil)
|
75
73
|
rv = []
|
76
74
|
|
77
|
-
logger =
|
75
|
+
logger = Bovem::Logger.new("/dev/null", Bovem::Logger::DEBUG) if !logger
|
78
76
|
logger.info("Resolving address #{address.bright} with type #{type.to_s.bright} at nameserver #{nameserver.bright}:#{port.to_s.bright} ...")
|
79
77
|
tmpfile = "/tmp/devdnsd-test-tester-#{Time.now.strftime("%Y%m%d-%H:%M:%S")}"
|
80
78
|
|
@@ -121,7 +119,7 @@ if __FILE__ == $0 then
|
|
121
119
|
type = (ARGV[1] || "ANY").upcase
|
122
120
|
nameserver = ARGV[2] || "127.0.0.1"
|
123
121
|
port = ARGV[3] || 7771
|
124
|
-
logger =
|
122
|
+
logger = Bovem::Logger.create($stdout, Logger::DEBUG)
|
125
123
|
|
126
124
|
devdnsd_resolv(address, type, nameserver, port, logger)
|
127
125
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: devdnsd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -12,37 +12,21 @@ cert_chain: []
|
|
12
12
|
date: 2012-08-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: bovem
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: 0.4.0
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: 0.4.0
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: cowtech-extensions
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ~>
|
19
|
+
- - ! '>='
|
36
20
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
21
|
+
version: 0.5.0
|
38
22
|
type: :runtime
|
39
23
|
prerelease: false
|
40
24
|
version_requirements: !ruby/object:Gem::Requirement
|
41
25
|
none: false
|
42
26
|
requirements:
|
43
|
-
- -
|
27
|
+
- - ! '>='
|
44
28
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
29
|
+
version: 0.5.0
|
46
30
|
- !ruby/object:Gem::Dependency
|
47
31
|
name: gli
|
48
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -60,13 +44,13 @@ dependencies:
|
|
60
44
|
- !ruby/object:Gem::Version
|
61
45
|
version: 1.6.0
|
62
46
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
47
|
+
name: rubydns
|
64
48
|
requirement: !ruby/object:Gem::Requirement
|
65
49
|
none: false
|
66
50
|
requirements:
|
67
51
|
- - ~>
|
68
52
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
53
|
+
version: 0.4.0
|
70
54
|
type: :runtime
|
71
55
|
prerelease: false
|
72
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -74,15 +58,15 @@ dependencies:
|
|
74
58
|
requirements:
|
75
59
|
- - ~>
|
76
60
|
- !ruby/object:Gem::Version
|
77
|
-
version:
|
61
|
+
version: 0.4.0
|
78
62
|
- !ruby/object:Gem::Dependency
|
79
|
-
name:
|
63
|
+
name: rexec
|
80
64
|
requirement: !ruby/object:Gem::Requirement
|
81
65
|
none: false
|
82
66
|
requirements:
|
83
67
|
- - ~>
|
84
68
|
- !ruby/object:Gem::Version
|
85
|
-
version: 1.1
|
69
|
+
version: 1.4.1
|
86
70
|
type: :runtime
|
87
71
|
prerelease: false
|
88
72
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -90,7 +74,7 @@ dependencies:
|
|
90
74
|
requirements:
|
91
75
|
- - ~>
|
92
76
|
- !ruby/object:Gem::Version
|
93
|
-
version: 1.1
|
77
|
+
version: 1.4.1
|
94
78
|
- !ruby/object:Gem::Dependency
|
95
79
|
name: rspec
|
96
80
|
requirement: !ruby/object:Gem::Requirement
|
@@ -242,13 +226,11 @@ files:
|
|
242
226
|
- lib/devdnsd/application.rb
|
243
227
|
- lib/devdnsd/configuration.rb
|
244
228
|
- lib/devdnsd/errors.rb
|
245
|
-
- lib/devdnsd/logger.rb
|
246
229
|
- lib/devdnsd/rule.rb
|
247
230
|
- lib/devdnsd/version.rb
|
248
231
|
- spec/coverage_helper.rb
|
249
232
|
- spec/devdnsd/application_spec.rb
|
250
233
|
- spec/devdnsd/configuration_spec.rb
|
251
|
-
- spec/devdnsd/logger_spec.rb
|
252
234
|
- spec/devdnsd/rule_spec.rb
|
253
235
|
- spec/spec_helper.rb
|
254
236
|
- utils/tester.rb
|
@@ -266,7 +248,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
266
248
|
version: '0'
|
267
249
|
segments:
|
268
250
|
- 0
|
269
|
-
hash: -
|
251
|
+
hash: -1047347418538266130
|
270
252
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
271
253
|
none: false
|
272
254
|
requirements:
|
@@ -275,7 +257,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
275
257
|
version: '0'
|
276
258
|
segments:
|
277
259
|
- 0
|
278
|
-
hash: -
|
260
|
+
hash: -1047347418538266130
|
279
261
|
requirements: []
|
280
262
|
rubyforge_project: devdnsd
|
281
263
|
rubygems_version: 1.8.24
|
@@ -286,7 +268,6 @@ test_files:
|
|
286
268
|
- spec/coverage_helper.rb
|
287
269
|
- spec/devdnsd/application_spec.rb
|
288
270
|
- spec/devdnsd/configuration_spec.rb
|
289
|
-
- spec/devdnsd/logger_spec.rb
|
290
271
|
- spec/devdnsd/rule_spec.rb
|
291
272
|
- spec/spec_helper.rb
|
292
273
|
has_rdoc:
|
data/lib/devdnsd/logger.rb
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
#
|
3
|
-
# This file is part of the devdnsd gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
-
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
-
#
|
6
|
-
|
7
|
-
module DevDNSd
|
8
|
-
# A custom logger for DevDNSd.
|
9
|
-
class Logger < ::Logger
|
10
|
-
# The start time of first line. This allows to show a `T+0.1234` information into the log.
|
11
|
-
mattr_accessor :start_time
|
12
|
-
|
13
|
-
# The file or device to log messages to.
|
14
|
-
attr_reader :device
|
15
|
-
|
16
|
-
# Creates a new logger
|
17
|
-
# @see http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html
|
18
|
-
#
|
19
|
-
# @param logdev [String|IO] The log device. This is a filename (String) or IO object (typically STDOUT, STDERR, or an open file).
|
20
|
-
# @param shift_age [Fixnum] Number of old log files to keep, or frequency of rotation (daily, weekly or monthly).
|
21
|
-
# @param shift_size [Fixnum] Maximum logfile size (only applies when shift_age is a number).
|
22
|
-
def initialize(logdev, shift_age = 0, shift_size = 1048576)
|
23
|
-
@device = logdev
|
24
|
-
super(logdev, shift_age, shift_size)
|
25
|
-
end
|
26
|
-
|
27
|
-
# Creates a new logger
|
28
|
-
#
|
29
|
-
# @param file [String|IO] The log device. This is a filename (String) or IO object (typically STDOUT, STDERR, or an open file).
|
30
|
-
# @param level [Fixnum] The minimum severity to log. See http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html for valid levels.
|
31
|
-
# @param formatter [Proc] The formatter to use for logging.
|
32
|
-
# @return [Logger] The new logger.
|
33
|
-
def self.create(file = nil, level = Logger::INFO, formatter = nil)
|
34
|
-
file ||= self.default_file
|
35
|
-
|
36
|
-
begin
|
37
|
-
rv = self.new(self.get_real_file(file))
|
38
|
-
rescue
|
39
|
-
raise DevDNSd::Errors::InvalidConfiguration
|
40
|
-
end
|
41
|
-
|
42
|
-
rv.level = level.to_i
|
43
|
-
rv.formatter = formatter || self.default_formatter
|
44
|
-
rv
|
45
|
-
end
|
46
|
-
|
47
|
-
# Translates a file to standard input or standard ouput in some special cases.
|
48
|
-
#
|
49
|
-
# @param file [String] The string to translate.
|
50
|
-
# @return [String|IO] The translated file name.
|
51
|
-
def self.get_real_file(file)
|
52
|
-
case file
|
53
|
-
when "STDOUT" then $stdout
|
54
|
-
when "STDERR" then $stderr
|
55
|
-
else file
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
# The default file for logging.
|
60
|
-
# @return [String|IO] The default file for logging.
|
61
|
-
def self.default_file
|
62
|
-
@default_file ||= $stdout
|
63
|
-
end
|
64
|
-
|
65
|
-
# The default formatter for logging.
|
66
|
-
# @return [Proc] The default formatter for logging.
|
67
|
-
def self.default_formatter
|
68
|
-
@default_formatter ||= ::Proc.new {|severity, datetime, progname, msg|
|
69
|
-
color = case severity
|
70
|
-
when "DEBUG" then :cyan
|
71
|
-
when "INFO" then :green
|
72
|
-
when "WARN" then :yellow
|
73
|
-
when "ERROR" then :red
|
74
|
-
when "FATAL" then :magenta
|
75
|
-
else :white
|
76
|
-
end
|
77
|
-
|
78
|
-
header = ("[%s T+%0.5f] %s:" %[datetime.strftime("%Y/%b/%d %H:%M:%S"), [datetime.to_f - self.start_time.to_f, 0].max, severity.rjust(5)]).bright
|
79
|
-
header = header.color(color) if color.present?
|
80
|
-
"%s %s\n" % [header, msg]
|
81
|
-
}
|
82
|
-
end
|
83
|
-
|
84
|
-
# The log time of the first logger. This allows to show a `T+0.1234` information into the log.
|
85
|
-
# @return [Time] The log time of the first logger.
|
86
|
-
def self.start_time
|
87
|
-
@start_time ||= ::Time.now
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
data/spec/devdnsd/logger_spec.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
#
|
3
|
-
# This file is part of the devdns gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
-
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
-
#
|
6
|
-
|
7
|
-
require "spec_helper"
|
8
|
-
|
9
|
-
describe DevDNSd::Logger do
|
10
|
-
before(:each) do
|
11
|
-
Sickill::Rainbow.enabled = false
|
12
|
-
end
|
13
|
-
|
14
|
-
describe ".create" do
|
15
|
-
it("should create a new default logger") do
|
16
|
-
logger = DevDNSd::Logger.create
|
17
|
-
expect(logger.device).to eq(DevDNSd::Logger.default_file)
|
18
|
-
expect(logger.level).to eq(::Logger::INFO)
|
19
|
-
expect(logger.formatter).to eq(DevDNSd::Logger.default_formatter)
|
20
|
-
end
|
21
|
-
|
22
|
-
it("should create a logger with a custom file and level") do
|
23
|
-
logger = DevDNSd::Logger.create("/dev/null", ::Logger::WARN)
|
24
|
-
expect(logger.device).to eq("/dev/null")
|
25
|
-
expect(logger.level).to eq(::Logger::WARN)
|
26
|
-
expect(logger.formatter).to eq(DevDNSd::Logger.default_formatter)
|
27
|
-
end
|
28
|
-
|
29
|
-
it("should create a logger with a custom formatter") do
|
30
|
-
formatter = Proc.new {|severity, datetime, progname, msg| msg }
|
31
|
-
logger = DevDNSd::Logger.create("/dev/null", ::Logger::WARN, formatter)
|
32
|
-
expect(logger.device).to eq("/dev/null")
|
33
|
-
expect(logger.level).to eq(::Logger::WARN)
|
34
|
-
expect(logger.formatter).to eq(formatter)
|
35
|
-
end
|
36
|
-
|
37
|
-
it("should raise exceptions for invalid files") do
|
38
|
-
expect { DevDNSd::Logger.create("/invalid/file") }.to raise_error(DevDNSd::Errors::InvalidConfiguration)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe ".default_formatter" do
|
43
|
-
let(:output) { ::StringIO.new }
|
44
|
-
let(:logger) { DevDNSd::Logger.create(output, Logger::DEBUG) }
|
45
|
-
|
46
|
-
def get_last_line(buffer)
|
47
|
-
buffer.string.split("\n").last.strip.gsub(/ T\+\d+\.\d+/, "")
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should correctly format a DEBUG message" do
|
51
|
-
logger.debug("Message.")
|
52
|
-
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] DEBUG: Message.")
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should correctly format a INFO message" do
|
56
|
-
logger.info("Message.")
|
57
|
-
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] INFO: Message.")
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should correctly format a WARN message" do
|
61
|
-
logger.warn("Message.")
|
62
|
-
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] WARN: Message.")
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should correctly format a ERROR message" do
|
66
|
-
logger.error("Message.")
|
67
|
-
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] ERROR: Message.")
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should correctly format a FATAL message" do
|
71
|
-
logger.fatal("Message.")
|
72
|
-
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] FATAL: Message.")
|
73
|
-
end
|
74
|
-
|
75
|
-
it "should correctly format a INVALID message" do
|
76
|
-
logger.log(::Logger::UNKNOWN, "Message.")
|
77
|
-
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] ANY: Message.")
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
describe ".get_real_file" do
|
82
|
-
it("should return the standard ouput") do
|
83
|
-
expect(DevDNSd::Logger.get_real_file("STDOUT")).to eq($stdout )
|
84
|
-
end
|
85
|
-
|
86
|
-
it("should return the standard error") do
|
87
|
-
expect(DevDNSd::Logger.get_real_file("STDERR")).to eq($stderr )
|
88
|
-
end
|
89
|
-
|
90
|
-
it("should return the file") do
|
91
|
-
expect(DevDNSd::Logger.get_real_file("/dev/null")).to eq("/dev/null" )
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
describe ".default_file" do
|
96
|
-
it("should return the standard output") do
|
97
|
-
expect(DevDNSd::Logger.default_file).to eq($stdout)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|