console 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8f2a979a83f16c98de42a9ce7c76db88096c5a6baeb0a6739842e627d9c3ba4
4
- data.tar.gz: 2e326f28c7a744ff783d009db13438d4333a02e63347fe03aedaab2edb1aee48
3
+ metadata.gz: 1caef24154ecf1f0cbe36ce8dfbbaceec31a1be279ce244105ff5e3868e04e28
4
+ data.tar.gz: 5c90b8b2f64ef76dccc1b7d37989ece807c2f636ef85e2d70344030884dab13e
5
5
  SHA512:
6
- metadata.gz: 5e1df308579cd0c3da40dd8fdb970bfd41f0fd006acb9ca62e73dfdd47b62d28ce719f71eb853bea3400f0c1a1b2eec18be82a601652388ebcc8a8a32ac9f135
7
- data.tar.gz: 5fd7718437f58ce2a8b41f91f9661b227a5b9b307046f057b837f63fbaf93642fa01b06c35cf720907d2b3d666b828bed3c01d6514f167f92142db18c56a3b9c
6
+ metadata.gz: 881fbf9a24669cf5287045c4a750c84f596fdb433047d4a1246503b060bd80441332d319eba0479b5a6ffc89e6eb6341e09c82fe8df4afc3a87537f63fb55733
7
+ data.tar.gz: e2cf4fcca144be6ebd9f5842a6fa44b077e7a603c0c29e23b80fd745ce83fedc9132e9e0dc3bdf72e52c80eb75dd8147b1bdf60d44b5fb0edd3c2c8c6fe85f72
data/.gitignore CHANGED
@@ -7,6 +7,6 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
 
10
- # rspec failure tracking
10
+ Gemfile.lock
11
11
  .rspec_status
12
12
  .covered.db
@@ -19,6 +19,7 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require_relative 'console/logger'
22
+ require_relative 'console/resolver'
22
23
  require_relative 'console/terminal/logger'
23
24
 
24
25
  module Console
@@ -32,6 +33,7 @@ module Console
32
33
 
33
34
  # Set the default log level based on `$DEBUG` and `$VERBOSE`.
34
35
  # You can also specify CONSOLE_LOG_LEVEL=debug or CONSOLE_LOG_LEVEL=info in environment.
36
+ # https://mislav.net/2011/06/ruby-verbose-mode/ has more details about how it all fits together.
35
37
  def default_log_level(env = ENV)
36
38
  if level = env['CONSOLE_LOG_LEVEL']
37
39
  LEVELS[level] || Logger.warn
@@ -52,6 +54,14 @@ module Console
52
54
  verbose: !$VERBOSE.nil?,
53
55
  )
54
56
 
57
+ if names = ENV['CONSOLE_DEBUG']&.split(',')
58
+ @resolver ||= Resolver.new
59
+
60
+ @resolver.bind(names) do |klass|
61
+ @logger.enable(klass, Logger::DEBUG)
62
+ end
63
+ end
64
+
55
65
  def logger= logger
56
66
  @logger = logger
57
67
  end
@@ -29,15 +29,14 @@ module Console
29
29
 
30
30
  klass.instance_exec do
31
31
  const_set(:LEVELS, levels)
32
+ const_set(:MINIMUM_LEVEL, levels.values.min)
32
33
  const_set(:MAXIMUM_LEVEL, levels.values.max)
33
-
34
+
34
35
  levels.each do |name, level|
35
36
  const_set(name.to_s.upcase, level)
36
37
 
37
38
  define_method(name) do |subject = nil, *arguments, **options, &block|
38
- enabled = @subjects[subject.class]
39
-
40
- if enabled == true or (enabled != false and level >= @level)
39
+ if self.enabled?(subject, level)
41
40
  self.call(subject, *arguments, severity: name, **options, **@options, &block)
42
41
  end
43
42
  end
@@ -55,7 +54,7 @@ module Console
55
54
  return klass
56
55
  end
57
56
 
58
- def initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, **options)
57
+ def initialize(output, verbose: true, level: self.class::DEFAULT_LEVEL, enabled: nil, **options)
59
58
  @output = output
60
59
  @verbose = verbose
61
60
  @level = level
@@ -64,6 +63,10 @@ module Console
64
63
 
65
64
  @options = options
66
65
 
66
+ if enabled
67
+ enabled.each{|name| enable(name)}
68
+ end
69
+
67
70
  verbose!(verbose)
68
71
  end
69
72
 
@@ -104,16 +107,36 @@ module Console
104
107
  @level = self.class::MAXIMUM_LEVEL
105
108
  end
106
109
 
107
- def enabled?(subject)
108
- @subjects[subject.class] == true
110
+ # You can enable and disable logging for classes. This function checks if logging for a given subject is enabled.
111
+ # @param subject [Object] the subject to check.
112
+ def enabled?(subject, level = self.class::MINIMUM_LEVEL)
113
+ if specific_level = @subjects[subject.class]
114
+ return level >= specific_level
115
+ end
116
+
117
+ if level >= @level
118
+ return true
119
+ end
109
120
  end
110
121
 
111
- def enable(subject)
112
- @subjects[subject.class] = true
122
+ # Enable specific log level for the given class.
123
+ # @param name [String, Class] The class to enable.
124
+ def enable(subject, level = self.class::MINIMUM_LEVEL)
125
+ unless subject.is_a?(Class)
126
+ subject = subject.class
127
+ end
128
+
129
+ @subjects[subject] = level
113
130
  end
114
131
 
132
+ # Disable specific logging for the specific class.
133
+ # @param name [String, Class] The class to disable.
115
134
  def disable(subject)
116
- @subjects[subject.class] = false
135
+ unless subject.is_a? Class
136
+ subject = subject.class
137
+ end
138
+
139
+ @subjects.delete(subject)
117
140
  end
118
141
 
119
142
  def call(*arguments, &block)
@@ -0,0 +1,61 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all 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,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'filter'
22
+
23
+ module Console
24
+ class Resolver
25
+ def initialize
26
+ @names = {}
27
+
28
+ @trace_point = TracePoint.new(:class, &self.method(:resolve))
29
+ end
30
+
31
+ def bind(names, &block)
32
+ names.each do |name|
33
+ if klass = Object.const_get(name) rescue nil
34
+ yield klass
35
+ else
36
+ @names[name] = block
37
+ end
38
+ end
39
+
40
+ if @names.any?
41
+ @trace_point.enable
42
+ else
43
+ @trace_point.disable
44
+ end
45
+ end
46
+
47
+ def waiting?
48
+ @trace_point.enabled?
49
+ end
50
+
51
+ def resolve(trace_point)
52
+ if block = @names.delete(trace_point.self.to_s)
53
+ block.call(trace_point.self)
54
+ end
55
+
56
+ if @names.empty?
57
+ @trace_point.disable
58
+ end
59
+ end
60
+ end
61
+ end
@@ -118,7 +118,7 @@ module Console
118
118
  prefix_style = @terminal[severity]
119
119
 
120
120
  if @verbose
121
- suffix = " #{@terminal[:logger_suffix]}[pid=#{Process.pid}]#{@terminal.reset}"
121
+ suffix = " #{@terminal[:logger_suffix]}[pid=#{Process.pid}] [#{Time.now}]#{@terminal.reset}"
122
122
  end
123
123
 
124
124
  output.puts "#{@terminal[:logger_prefix]}#{subject}#{@terminal.reset}#{suffix}", prefix: "#{prefix_style}#{prefix}:#{@terminal.reset} "
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Console
22
- VERSION = "1.3.2"
22
+ VERSION = "1.4.0"
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-10 00:00:00.000000000 Z
11
+ date: 2019-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: covered
@@ -89,6 +89,7 @@ files:
89
89
  - lib/console/event/spawn.rb
90
90
  - lib/console/filter.rb
91
91
  - lib/console/logger.rb
92
+ - lib/console/resolver.rb
92
93
  - lib/console/serialized/logger.rb
93
94
  - lib/console/split.rb
94
95
  - lib/console/terminal.rb
@@ -115,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  - !ruby/object:Gem::Version
116
117
  version: '0'
117
118
  requirements: []
118
- rubygems_version: 3.0.2
119
+ rubygems_version: 3.0.3
119
120
  signing_key:
120
121
  specification_version: 4
121
122
  summary: Beautiful logging for Ruby.