console 1.20.0 → 1.22.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: d974c867b670b89c2b04c83f640f46217e9836e3eec7f92903bc062e6c59458c
4
- data.tar.gz: 4bc1d803689cad17d85a36d7da8c172a63ea869e6fa060f9c85702c1da365b90
3
+ metadata.gz: 9f868615689827a90fa20cb73c9408598bd6cfc9602b21ee939a318213a3aee1
4
+ data.tar.gz: 44927a1200deac00fab718aa995f0549eec0301b688a440da83ae13fd87edc33
5
5
  SHA512:
6
- metadata.gz: 1db5693110b3a7f48b17abdf58eb74ac5269f4748d0746df057c7dbe01ddee228db5bcc74bd581c1e9bddf94602943682feb4cf50c7b14312d23db47e5209206
7
- data.tar.gz: 3209fa49d39e7de2040610187599b7b22e41af0da3c2749b7a7c939ce40d07c2101472fec9a47a20de754d216f780ab59f508a3b4ddc17dd5c251d4a94e215f3
6
+ metadata.gz: eabb7c42bcb625726e75a7ebc1e7f330b7dcb0b8936b012c96c79b8a5456f0ac2b3fea5d02cdb1511fde9bd696c7ba300b6394952138810824cb18bdbe8e8e6e
7
+ data.tar.gz: 1afa8d02eb8e56c81ac39f925e6a5df7edad41198c8aa255eb418847076913b6965b7d5794ba04ed6d5d35b08a1785e8825e12d32a8398f4203f7197584670c1
checksums.yaml.gz.sig CHANGED
Binary file
@@ -29,7 +29,7 @@ module Console
29
29
  end
30
30
  end
31
31
 
32
- def initialize(subject, output)
32
+ def initialize(subject, output = Console)
33
33
  super(nil)
34
34
 
35
35
  @progname = subject
@@ -5,6 +5,7 @@
5
5
 
6
6
  module Console
7
7
  module Output
8
+ # @deprecated With no replacement.
8
9
  class Encoder
9
10
  def initialize(output, encoding = ::Encoding::UTF_8)
10
11
  @output = output
@@ -4,16 +4,98 @@
4
4
  # Copyright, 2021-2022, by Samuel Williams.
5
5
 
6
6
  require_relative '../serialized/logger'
7
- require_relative 'encoder'
8
7
 
9
8
  module Console
10
9
  module Output
11
10
  module JSON
11
+ # This is a safe JSON serializer that will not raise exceptions.
12
+ class Safe
13
+ def initialize(limit: 8, encoding: ::Encoding::UTF_8)
14
+ @limit = limit
15
+ @encoding = encoding
16
+ end
17
+
18
+ def dump(object)
19
+ ::JSON.dump(object, @limit)
20
+ rescue => error
21
+ ::JSON.dump(safe_dump(object, error))
22
+ end
23
+
24
+ private
25
+
26
+ def default_objects
27
+ Hash.new.compare_by_identity
28
+ end
29
+
30
+ def safe_dump(object, error)
31
+ object = safe_dump_recurse(object)
32
+
33
+ object[:truncated] = true
34
+ object[:error] = {
35
+ class: safe_dump_recurse(error.class.name),
36
+ message: safe_dump_recurse(error.message),
37
+ }
38
+
39
+ return object
40
+ end
41
+
42
+ def replacement_for(object)
43
+ case object
44
+ when Array
45
+ "[...]"
46
+ when Hash
47
+ "{...}"
48
+ else
49
+ "..."
50
+ end
51
+ end
52
+
53
+ # This will recursively generate a safe version of the object.
54
+ # Nested hashes and arrays will be transformed recursively.
55
+ # Strings will be encoded with the given encoding.
56
+ # Primitive values will be returned as-is.
57
+ # Other values will be converted using `as_json` if available, otherwise `to_s`.
58
+ def safe_dump_recurse(object, limit = @limit, objects = default_objects)
59
+ if limit <= 0 || objects[object]
60
+ return replacement_for(object)
61
+ end
62
+
63
+ case object
64
+ when Hash
65
+ objects[object] = true
66
+
67
+ object.to_h do |key, value|
68
+ [
69
+ String(key).encode(@encoding, invalid: :replace, undef: :replace),
70
+ safe_dump_recurse(value, limit - 1, objects)
71
+ ]
72
+ end
73
+ when Array
74
+ objects[object] = true
75
+
76
+ object.map do |value|
77
+ safe_dump_recurse(value, limit - 1, objects)
78
+ end
79
+ when String
80
+ object.encode(@encoding, invalid: :replace, undef: :replace)
81
+ when Numeric, TrueClass, FalseClass, NilClass
82
+ object
83
+ else
84
+ objects[object] = true
85
+
86
+ # We could do something like this but the chance `as_json` will blow up.
87
+ # We'd need to be extremely careful about it.
88
+ # if object.respond_to?(:as_json)
89
+ # safe_dump_recurse(object.as_json, limit - 1, objects)
90
+ # else
91
+
92
+ safe_dump_recurse(object.to_s, limit - 1, objects)
93
+ end
94
+ end
95
+ end
96
+
12
97
  def self.new(output, **options)
13
- # The output encoder can prevent encoding issues (e.g. invalid UTF-8):
14
- Output::Encoder.new(
15
- Serialized::Logger.new(output, format: ::JSON, **options)
16
- )
98
+ Serialized::Logger.new(output, format: Safe.new, **options)
17
99
  end
18
100
  end
19
101
  end
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2019-2023, by Samuel Williams.
5
5
 
6
6
  module Console
7
- VERSION = "1.20.0"
7
+ VERSION = "1.22.0"
8
8
  end
data/lib/console.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Released under the MIT License.
4
- # Copyright, 2019-2022, by Samuel Williams.
4
+ # Copyright, 2019-2023, by Samuel Williams.
5
5
  # Copyright, 2019, by Bryan Powell.
6
6
  # Copyright, 2020, by Michael Adams.
7
7
  # Copyright, 2021, by Cédric Boutillier.
@@ -10,23 +10,45 @@ require_relative 'console/version'
10
10
  require_relative 'console/logger'
11
11
 
12
12
  module Console
13
- def self.logger
14
- Logger.instance
15
- end
16
-
17
- def self.logger= instance
18
- Logger.instance= instance
13
+ class << self
14
+ def logger
15
+ Logger.instance
16
+ end
17
+
18
+ def logger= instance
19
+ Logger.instance= instance
20
+ end
21
+
22
+ def debug(...)
23
+ Logger.instance.debug(...)
24
+ end
25
+
26
+ def info(...)
27
+ Logger.instance.info(...)
28
+ end
29
+
30
+ def warn(...)
31
+ Logger.instance.warn(...)
32
+ end
33
+
34
+ def error(...)
35
+ Logger.instance.error(...)
36
+ end
37
+
38
+ def fatal(...)
39
+ Logger.instance.fatal(...)
40
+ end
41
+
42
+ def call(...)
43
+ Logger.instance.call(...)
44
+ end
19
45
  end
20
46
 
21
47
  def logger= logger
22
- @logger = logger
48
+ warn "Setting logger on #{self} is deprecated. Use Console.logger= instead.", uplevel: 1
23
49
  end
24
50
 
25
51
  def logger
26
- @logger || Logger.instance
27
- end
28
-
29
- def self.extended(klass)
30
- klass.instance_variable_set(:@logger, nil)
52
+ Logger.instance
31
53
  end
32
54
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.20.0
4
+ version: 1.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -46,7 +46,7 @@ cert_chain:
46
46
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
47
47
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
48
48
  -----END CERTIFICATE-----
49
- date: 2023-08-03 00:00:00.000000000 Z
49
+ date: 2023-08-11 00:00:00.000000000 Z
50
50
  dependencies:
51
51
  - !ruby/object:Gem::Dependency
52
52
  name: fiber-annotation
metadata.gz.sig CHANGED
Binary file