rbs 0.2.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +7 -1
- data/.gitignore +1 -1
- data/CHANGELOG.md +35 -0
- data/COPYING +1 -1
- data/Gemfile +16 -2
- data/README.md +87 -48
- data/Rakefile +54 -22
- data/bin/rbs-prof +9 -0
- data/bin/run_in_md.rb +49 -0
- data/bin/test_runner.rb +0 -2
- data/docs/sigs.md +6 -6
- data/docs/stdlib.md +3 -5
- data/docs/syntax.md +6 -3
- data/goodcheck.yml +65 -0
- data/lib/rbs.rb +3 -0
- data/lib/rbs/ast/declarations.rb +115 -14
- data/lib/rbs/ast/members.rb +41 -17
- data/lib/rbs/cli.rb +301 -123
- data/lib/rbs/constant.rb +4 -4
- data/lib/rbs/constant_table.rb +64 -53
- data/lib/rbs/definition.rb +175 -59
- data/lib/rbs/definition_builder.rb +646 -603
- data/lib/rbs/environment.rb +352 -210
- data/lib/rbs/environment_walker.rb +14 -23
- data/lib/rbs/errors.rb +159 -3
- data/lib/rbs/factory.rb +14 -0
- data/lib/rbs/namespace.rb +18 -0
- data/lib/rbs/parser.y +75 -21
- data/lib/rbs/prototype/rb.rb +119 -117
- data/lib/rbs/prototype/rbi.rb +5 -3
- data/lib/rbs/prototype/runtime.rb +34 -7
- data/lib/rbs/substitution.rb +8 -1
- data/lib/rbs/test.rb +81 -3
- data/lib/rbs/test/errors.rb +1 -1
- data/lib/rbs/test/hook.rb +133 -259
- data/lib/rbs/test/observer.rb +17 -0
- data/lib/rbs/test/setup.rb +13 -14
- data/lib/rbs/test/spy.rb +0 -321
- data/lib/rbs/test/tester.rb +116 -0
- data/lib/rbs/test/type_check.rb +44 -7
- data/lib/rbs/type_name_resolver.rb +58 -0
- data/lib/rbs/types.rb +94 -2
- data/lib/rbs/validator.rb +51 -0
- data/lib/rbs/variance_calculator.rb +12 -2
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +127 -91
- data/rbs.gemspec +0 -9
- data/schema/annotation.json +14 -0
- data/schema/comment.json +26 -0
- data/schema/decls.json +353 -0
- data/schema/function.json +87 -0
- data/schema/location.json +56 -0
- data/schema/members.json +248 -0
- data/schema/methodType.json +44 -0
- data/schema/types.json +299 -0
- data/stdlib/benchmark/benchmark.rbs +151 -151
- data/stdlib/builtin/encoding.rbs +2 -0
- data/stdlib/builtin/enumerable.rbs +2 -2
- data/stdlib/builtin/enumerator.rbs +3 -1
- data/stdlib/builtin/fiber.rbs +5 -1
- data/stdlib/builtin/file.rbs +0 -3
- data/stdlib/builtin/io.rbs +4 -4
- data/stdlib/builtin/proc.rbs +1 -2
- data/stdlib/builtin/symbol.rbs +1 -1
- data/stdlib/builtin/thread.rbs +2 -2
- data/stdlib/csv/csv.rbs +4 -6
- data/stdlib/fiber/fiber.rbs +117 -0
- data/stdlib/json/json.rbs +1 -1
- data/stdlib/logger/formatter.rbs +23 -0
- data/stdlib/logger/log_device.rbs +39 -0
- data/stdlib/logger/logger.rbs +507 -0
- data/stdlib/logger/period.rbs +7 -0
- data/stdlib/logger/severity.rbs +8 -0
- data/stdlib/mutex_m/mutex_m.rbs +77 -0
- data/stdlib/pathname/pathname.rbs +6 -6
- data/stdlib/prime/integer-extension.rbs +1 -1
- data/stdlib/prime/prime.rbs +44 -44
- data/stdlib/tmpdir/tmpdir.rbs +1 -1
- metadata +26 -116
- data/lib/rbs/test/test_helper.rb +0 -183
data/stdlib/csv/csv.rbs
CHANGED
@@ -159,7 +159,7 @@
|
|
159
159
|
# with it.
|
160
160
|
#
|
161
161
|
class CSV < Object
|
162
|
-
include Enumerable
|
162
|
+
include Enumerable[untyped, untyped]
|
163
163
|
|
164
164
|
# This method is intended as the primary interface for reading CSV files. You
|
165
165
|
# pass a `path` and any `options` you wish to set for the read. Each row of file
|
@@ -407,7 +407,7 @@ CSV::VERSION: String
|
|
407
407
|
# processing is activated.
|
408
408
|
#
|
409
409
|
class CSV::Row < Object
|
410
|
-
include Enumerable
|
410
|
+
include Enumerable[untyped, untyped]
|
411
411
|
|
412
412
|
# If a two-element Array is provided, it is assumed to be a header and field and
|
413
413
|
# the pair is appended. A Hash works the same way with the key being the header
|
@@ -544,8 +544,6 @@ class CSV::Row < Object
|
|
544
544
|
|
545
545
|
def size: (*untyped args) { (*untyped) -> untyped } -> untyped
|
546
546
|
|
547
|
-
alias to_ary to_a
|
548
|
-
|
549
547
|
# Returns the row as a CSV String. Headers are not used. Equivalent to:
|
550
548
|
#
|
551
549
|
# csv_row.fields.to_csv( options )
|
@@ -564,7 +562,7 @@ class CSV::Row < Object
|
|
564
562
|
alias values_at fields
|
565
563
|
end
|
566
564
|
|
567
|
-
class CSV::FieldInfo < Struct
|
565
|
+
class CSV::FieldInfo < Struct[untyped]
|
568
566
|
end
|
569
567
|
|
570
568
|
# The error thrown when the parser encounters illegal CSV formatting.
|
@@ -580,7 +578,7 @@ end
|
|
580
578
|
# processing is activated.
|
581
579
|
#
|
582
580
|
class CSV::Table[out Elem] < Object
|
583
|
-
include Enumerable
|
581
|
+
include Enumerable[untyped, untyped]
|
584
582
|
|
585
583
|
# Constructs a new CSV::Table from `array_of_rows`, which are expected to be
|
586
584
|
# CSV::Row objects. All rows are assumed to have the same headers.
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# Fibers are primitives for implementing light weight cooperative concurrency in
|
2
|
+
# Ruby. Basically they are a means of creating code blocks that can be paused
|
3
|
+
# and resumed, much like threads. The main difference is that they are never
|
4
|
+
# preempted and that the scheduling must be done by the programmer and not the
|
5
|
+
# VM.
|
6
|
+
#
|
7
|
+
# As opposed to other stackless light weight concurrency models, each fiber
|
8
|
+
# comes with a stack. This enables the fiber to be paused from deeply nested
|
9
|
+
# function calls within the fiber block. See the ruby(1) manpage to configure
|
10
|
+
# the size of the fiber stack(s).
|
11
|
+
#
|
12
|
+
# When a fiber is created it will not run automatically. Rather it must be
|
13
|
+
# explicitly asked to run using the Fiber#resume method. The code running inside
|
14
|
+
# the fiber can give up control by calling Fiber.yield in which case it yields
|
15
|
+
# control back to caller (the caller of the Fiber#resume).
|
16
|
+
#
|
17
|
+
# Upon yielding or termination the Fiber returns the value of the last executed
|
18
|
+
# expression
|
19
|
+
#
|
20
|
+
# For instance:
|
21
|
+
#
|
22
|
+
# fiber = Fiber.new do
|
23
|
+
# Fiber.yield 1
|
24
|
+
# 2
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# puts fiber.resume
|
28
|
+
# puts fiber.resume
|
29
|
+
# puts fiber.resume
|
30
|
+
#
|
31
|
+
# *produces*
|
32
|
+
#
|
33
|
+
# 1
|
34
|
+
# 2
|
35
|
+
# FiberError: dead fiber called
|
36
|
+
#
|
37
|
+
# The Fiber#resume method accepts an arbitrary number of parameters, if it is
|
38
|
+
# the first call to #resume then they will be passed as block arguments.
|
39
|
+
# Otherwise they will be the return value of the call to Fiber.yield
|
40
|
+
#
|
41
|
+
# Example:
|
42
|
+
#
|
43
|
+
# fiber = Fiber.new do |first|
|
44
|
+
# second = Fiber.yield first + 2
|
45
|
+
# end
|
46
|
+
#
|
47
|
+
# puts fiber.resume 10
|
48
|
+
# puts fiber.resume 1_000_000
|
49
|
+
# puts fiber.resume "The fiber will be dead before I can cause trouble"
|
50
|
+
#
|
51
|
+
# *produces*
|
52
|
+
#
|
53
|
+
# 12
|
54
|
+
# 1000000
|
55
|
+
# FiberError: dead fiber called
|
56
|
+
#
|
57
|
+
class Fiber
|
58
|
+
# Returns the current fiber. You need to `require 'fiber'` before using this
|
59
|
+
# method. If you are not running in the context of a fiber this method will
|
60
|
+
# return the root fiber.
|
61
|
+
#
|
62
|
+
def self.current: () -> Fiber
|
63
|
+
|
64
|
+
public
|
65
|
+
|
66
|
+
# Returns true if the fiber can still be resumed (or transferred to). After
|
67
|
+
# finishing execution of the fiber block this method will always return false.
|
68
|
+
# You need to `require 'fiber'` before using this method.
|
69
|
+
#
|
70
|
+
def alive?: () -> bool
|
71
|
+
|
72
|
+
# Transfer control to another fiber, resuming it from where it last stopped or
|
73
|
+
# starting it if it was not resumed before. The calling fiber will be suspended
|
74
|
+
# much like in a call to Fiber.yield. You need to `require 'fiber'` before using
|
75
|
+
# this method.
|
76
|
+
#
|
77
|
+
# The fiber which receives the transfer call is treats it much like a resume
|
78
|
+
# call. Arguments passed to transfer are treated like those passed to resume.
|
79
|
+
#
|
80
|
+
# You cannot call `resume` on a fiber that has been transferred to. If you call
|
81
|
+
# `transfer` on a fiber, and later call `resume` on the the fiber, a
|
82
|
+
# `FiberError` will be raised. Once you call `transfer` on a fiber, the only way
|
83
|
+
# to resume processing the fiber is to call `transfer` on it again.
|
84
|
+
#
|
85
|
+
# Example:
|
86
|
+
#
|
87
|
+
# fiber1 = Fiber.new do
|
88
|
+
# puts "In Fiber 1"
|
89
|
+
# Fiber.yield
|
90
|
+
# puts "In Fiber 1 again"
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# fiber2 = Fiber.new do
|
94
|
+
# puts "In Fiber 2"
|
95
|
+
# fiber1.transfer
|
96
|
+
# puts "Never see this message"
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# fiber3 = Fiber.new do
|
100
|
+
# puts "In Fiber 3"
|
101
|
+
# end
|
102
|
+
#
|
103
|
+
# fiber2.resume
|
104
|
+
# fiber3.resume
|
105
|
+
# fiber1.resume rescue (p $!)
|
106
|
+
# fiber1.transfer
|
107
|
+
#
|
108
|
+
# *produces*
|
109
|
+
#
|
110
|
+
# In Fiber 2
|
111
|
+
# In Fiber 1
|
112
|
+
# In Fiber 3
|
113
|
+
# #<FiberError: cannot resume transferred Fiber>
|
114
|
+
# In Fiber 1 again
|
115
|
+
#
|
116
|
+
def transfer: (*untyped) -> untyped
|
117
|
+
end
|
data/stdlib/json/json.rbs
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
class Logger
|
2
|
+
class Formatter
|
3
|
+
public
|
4
|
+
|
5
|
+
attr_accessor datetime_format: String?
|
6
|
+
|
7
|
+
def call: (String severity, Time time, untyped progname, untyped msg) -> String
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def format_datetime: (Time time) -> untyped
|
12
|
+
|
13
|
+
def initialize: () -> void
|
14
|
+
|
15
|
+
def msg2str: (String | Exception | untyped msg) -> String
|
16
|
+
end
|
17
|
+
|
18
|
+
interface _Formatter
|
19
|
+
def call: (String severity, Time time, untyped progname, untyped msg) -> _ToS
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Logger::Formatter::Format: String
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class Logger
|
2
|
+
class LogDevice
|
3
|
+
# TODO: Write type signature for MonitorMixin
|
4
|
+
# include MonitorMixin
|
5
|
+
|
6
|
+
include Period
|
7
|
+
|
8
|
+
attr_reader dev: _WriteCloser
|
9
|
+
attr_reader filename: String?
|
10
|
+
|
11
|
+
public
|
12
|
+
|
13
|
+
def close: () -> nil
|
14
|
+
|
15
|
+
def reopen: (?logdev log) -> self
|
16
|
+
|
17
|
+
def write: (untyped message) -> untyped
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def add_log_header: (IO file) -> untyped
|
22
|
+
|
23
|
+
def check_shift_log: () -> untyped
|
24
|
+
|
25
|
+
def create_logfile: (String filename) -> File
|
26
|
+
|
27
|
+
def initialize: (?untyped logdev, ?binmode: bool, ?shift_period_suffix: String, ?shift_size: Integer, ?shift_age: Numeric | String) -> void
|
28
|
+
|
29
|
+
def lock_shift_log: () { () -> untyped } -> untyped
|
30
|
+
|
31
|
+
def open_logfile: (String filename) -> File
|
32
|
+
|
33
|
+
def set_dev: (logdev log) -> untyped
|
34
|
+
|
35
|
+
def shift_log_age: () -> true
|
36
|
+
|
37
|
+
def shift_log_period: (Time period_end) -> true
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,507 @@
|
|
1
|
+
# ## Description
|
2
|
+
#
|
3
|
+
# The Logger class provides a simple but sophisticated logging utility that you
|
4
|
+
# can use to output messages.
|
5
|
+
#
|
6
|
+
# The messages have associated levels, such as `INFO` or `ERROR` that indicate
|
7
|
+
# their importance. You can then give the Logger a level, and only messages at
|
8
|
+
# that level or higher will be printed.
|
9
|
+
#
|
10
|
+
# The levels are:
|
11
|
+
#
|
12
|
+
# `UNKNOWN`
|
13
|
+
# : An unknown message that should always be logged.
|
14
|
+
# `FATAL`
|
15
|
+
# : An unhandleable error that results in a program crash.
|
16
|
+
# `ERROR`
|
17
|
+
# : A handleable error condition.
|
18
|
+
# `WARN`
|
19
|
+
# : A warning.
|
20
|
+
# `INFO`
|
21
|
+
# : Generic (useful) information about system operation.
|
22
|
+
# `DEBUG`
|
23
|
+
# : Low-level information for developers.
|
24
|
+
#
|
25
|
+
#
|
26
|
+
# For instance, in a production system, you may have your Logger set to `INFO`
|
27
|
+
# or even `WARN`. When you are developing the system, however, you probably want
|
28
|
+
# to know about the program's internal state, and would set the Logger to
|
29
|
+
# `DEBUG`.
|
30
|
+
#
|
31
|
+
# **Note**: Logger does not escape or sanitize any messages passed to it.
|
32
|
+
# Developers should be aware of when potentially malicious data (user-input) is
|
33
|
+
# passed to Logger, and manually escape the untrusted data:
|
34
|
+
#
|
35
|
+
# logger.info("User-input: #{input.dump}")
|
36
|
+
# logger.info("User-input: %p" % input)
|
37
|
+
#
|
38
|
+
# You can use #formatter= for escaping all data.
|
39
|
+
#
|
40
|
+
# original_formatter = Logger::Formatter.new
|
41
|
+
# logger.formatter = proc { |severity, datetime, progname, msg|
|
42
|
+
# original_formatter.call(severity, datetime, progname, msg.dump)
|
43
|
+
# }
|
44
|
+
# logger.info(input)
|
45
|
+
#
|
46
|
+
# ### Example
|
47
|
+
#
|
48
|
+
# This creates a Logger that outputs to the standard output stream, with a level
|
49
|
+
# of `WARN`:
|
50
|
+
#
|
51
|
+
# require 'logger'
|
52
|
+
#
|
53
|
+
# logger = Logger.new(STDOUT)
|
54
|
+
# logger.level = Logger::WARN
|
55
|
+
#
|
56
|
+
# logger.debug("Created logger")
|
57
|
+
# logger.info("Program started")
|
58
|
+
# logger.warn("Nothing to do!")
|
59
|
+
#
|
60
|
+
# path = "a_non_existent_file"
|
61
|
+
#
|
62
|
+
# begin
|
63
|
+
# File.foreach(path) do |line|
|
64
|
+
# unless line =~ /^(\w+) = (.*)$/
|
65
|
+
# logger.error("Line in wrong format: #{line.chomp}")
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
# rescue => err
|
69
|
+
# logger.fatal("Caught exception; exiting")
|
70
|
+
# logger.fatal(err)
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
# Because the Logger's level is set to `WARN`, only the warning, error, and
|
74
|
+
# fatal messages are recorded. The debug and info messages are silently
|
75
|
+
# discarded.
|
76
|
+
#
|
77
|
+
# ### Features
|
78
|
+
#
|
79
|
+
# There are several interesting features that Logger provides, like auto-rolling
|
80
|
+
# of log files, setting the format of log messages, and specifying a program
|
81
|
+
# name in conjunction with the message. The next section shows you how to
|
82
|
+
# achieve these things.
|
83
|
+
#
|
84
|
+
# ## HOWTOs
|
85
|
+
#
|
86
|
+
# ### How to create a logger
|
87
|
+
#
|
88
|
+
# The options below give you various choices, in more or less increasing
|
89
|
+
# complexity.
|
90
|
+
#
|
91
|
+
# 1. Create a logger which logs messages to STDERR/STDOUT.
|
92
|
+
#
|
93
|
+
# logger = Logger.new(STDERR)
|
94
|
+
# logger = Logger.new(STDOUT)
|
95
|
+
#
|
96
|
+
# 2. Create a logger for the file which has the specified name.
|
97
|
+
#
|
98
|
+
# logger = Logger.new('logfile.log')
|
99
|
+
#
|
100
|
+
# 3. Create a logger for the specified file.
|
101
|
+
#
|
102
|
+
# file = File.open('foo.log', File::WRONLY | File::APPEND)
|
103
|
+
# # To create new logfile, add File::CREAT like:
|
104
|
+
# # file = File.open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
|
105
|
+
# logger = Logger.new(file)
|
106
|
+
#
|
107
|
+
# 4. Create a logger which ages the logfile once it reaches a certain size.
|
108
|
+
# Leave 10 "old" log files where each file is about 1,024,000 bytes.
|
109
|
+
#
|
110
|
+
# logger = Logger.new('foo.log', 10, 1024000)
|
111
|
+
#
|
112
|
+
# 5. Create a logger which ages the logfile daily/weekly/monthly.
|
113
|
+
#
|
114
|
+
# logger = Logger.new('foo.log', 'daily')
|
115
|
+
# logger = Logger.new('foo.log', 'weekly')
|
116
|
+
# logger = Logger.new('foo.log', 'monthly')
|
117
|
+
#
|
118
|
+
#
|
119
|
+
# ### How to log a message
|
120
|
+
#
|
121
|
+
# Notice the different methods (`fatal`, `error`, `info`) being used to log
|
122
|
+
# messages of various levels? Other methods in this family are `warn` and
|
123
|
+
# `debug`. `add` is used below to log a message of an arbitrary (perhaps
|
124
|
+
# dynamic) level.
|
125
|
+
#
|
126
|
+
# 1. Message in a block.
|
127
|
+
#
|
128
|
+
# logger.fatal { "Argument 'foo' not given." }
|
129
|
+
#
|
130
|
+
# 2. Message as a string.
|
131
|
+
#
|
132
|
+
# logger.error "Argument #{@foo} mismatch."
|
133
|
+
#
|
134
|
+
# 3. With progname.
|
135
|
+
#
|
136
|
+
# logger.info('initialize') { "Initializing..." }
|
137
|
+
#
|
138
|
+
# 4. With severity.
|
139
|
+
#
|
140
|
+
# logger.add(Logger::FATAL) { 'Fatal error!' }
|
141
|
+
#
|
142
|
+
#
|
143
|
+
# The block form allows you to create potentially complex log messages, but to
|
144
|
+
# delay their evaluation until and unless the message is logged. For example,
|
145
|
+
# if we have the following:
|
146
|
+
#
|
147
|
+
# logger.debug { "This is a " + potentially + " expensive operation" }
|
148
|
+
#
|
149
|
+
# If the logger's level is `INFO` or higher, no debug messages will be logged,
|
150
|
+
# and the entire block will not even be evaluated. Compare to this:
|
151
|
+
#
|
152
|
+
# logger.debug("This is a " + potentially + " expensive operation")
|
153
|
+
#
|
154
|
+
# Here, the string concatenation is done every time, even if the log level is
|
155
|
+
# not set to show the debug message.
|
156
|
+
#
|
157
|
+
# ### How to close a logger
|
158
|
+
#
|
159
|
+
# logger.close
|
160
|
+
#
|
161
|
+
# ### Setting severity threshold
|
162
|
+
#
|
163
|
+
# 1. Original interface.
|
164
|
+
#
|
165
|
+
# logger.sev_threshold = Logger::WARN
|
166
|
+
#
|
167
|
+
# 2. Log4r (somewhat) compatible interface.
|
168
|
+
#
|
169
|
+
# logger.level = Logger::INFO
|
170
|
+
#
|
171
|
+
# # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
|
172
|
+
#
|
173
|
+
# 3. Symbol or String (case insensitive)
|
174
|
+
#
|
175
|
+
# logger.level = :info
|
176
|
+
# logger.level = 'INFO'
|
177
|
+
#
|
178
|
+
# # :debug < :info < :warn < :error < :fatal < :unknown
|
179
|
+
#
|
180
|
+
# 4. Constructor
|
181
|
+
#
|
182
|
+
# Logger.new(logdev, level: Logger::INFO)
|
183
|
+
# Logger.new(logdev, level: :info)
|
184
|
+
# Logger.new(logdev, level: 'INFO')
|
185
|
+
#
|
186
|
+
#
|
187
|
+
# ## Format
|
188
|
+
#
|
189
|
+
# Log messages are rendered in the output stream in a certain format by default.
|
190
|
+
# The default format and a sample are shown below:
|
191
|
+
#
|
192
|
+
# Log format:
|
193
|
+
# SeverityID, [DateTime #pid] SeverityLabel -- ProgName: message
|
194
|
+
#
|
195
|
+
# Log sample:
|
196
|
+
# I, [1999-03-03T02:34:24.895701 #19074] INFO -- Main: info.
|
197
|
+
#
|
198
|
+
# You may change the date and time format via #datetime_format=.
|
199
|
+
#
|
200
|
+
# logger.datetime_format = '%Y-%m-%d %H:%M:%S'
|
201
|
+
# # e.g. "2004-01-03 00:54:26"
|
202
|
+
#
|
203
|
+
# or via the constructor.
|
204
|
+
#
|
205
|
+
# Logger.new(logdev, datetime_format: '%Y-%m-%d %H:%M:%S')
|
206
|
+
#
|
207
|
+
# Or, you may change the overall format via the #formatter= method.
|
208
|
+
#
|
209
|
+
# logger.formatter = proc do |severity, datetime, progname, msg|
|
210
|
+
# "#{datetime}: #{msg}\n"
|
211
|
+
# end
|
212
|
+
# # e.g. "2005-09-22 08:51:08 +0900: hello world"
|
213
|
+
#
|
214
|
+
# or via the constructor.
|
215
|
+
#
|
216
|
+
# Logger.new(logdev, formatter: proc {|severity, datetime, progname, msg|
|
217
|
+
# "#{datetime}: #{msg}\n"
|
218
|
+
# })
|
219
|
+
#
|
220
|
+
class Logger
|
221
|
+
interface _WriteCloser
|
222
|
+
def write: (_ToS) -> untyped
|
223
|
+
|
224
|
+
def close: () -> untyped
|
225
|
+
end
|
226
|
+
type logdev = _WriteCloser | String
|
227
|
+
|
228
|
+
include Logger::Severity
|
229
|
+
|
230
|
+
public
|
231
|
+
|
232
|
+
# Dump given message to the log device without any formatting. If no log device
|
233
|
+
# exists, return `nil`.
|
234
|
+
#
|
235
|
+
def <<: (untyped msg) -> (untyped | nil)
|
236
|
+
|
237
|
+
# ### Args
|
238
|
+
#
|
239
|
+
# `severity`
|
240
|
+
# : Severity. Constants are defined in Logger namespace: `DEBUG`, `INFO`,
|
241
|
+
# `WARN`, `ERROR`, `FATAL`, or `UNKNOWN`.
|
242
|
+
# `message`
|
243
|
+
# : The log message. A String or Exception.
|
244
|
+
# `progname`
|
245
|
+
# : Program name string. Can be omitted. Treated as a message if no
|
246
|
+
# `message` and `block` are given.
|
247
|
+
# `block`
|
248
|
+
# : Can be omitted. Called to get a message string if `message` is nil.
|
249
|
+
#
|
250
|
+
#
|
251
|
+
# ### Return
|
252
|
+
#
|
253
|
+
# When the given severity is not high enough (for this particular logger), log
|
254
|
+
# no message, and return `true`.
|
255
|
+
#
|
256
|
+
# ### Description
|
257
|
+
#
|
258
|
+
# Log a message if the given severity is high enough. This is the generic
|
259
|
+
# logging method. Users will be more inclined to use #debug, #info, #warn,
|
260
|
+
# #error, and #fatal.
|
261
|
+
#
|
262
|
+
# **Message format**: `message` can be any object, but it has to be converted to
|
263
|
+
# a String in order to log it. Generally, `inspect` is used if the given object
|
264
|
+
# is not a String. A special case is an `Exception` object, which will be
|
265
|
+
# printed in detail, including message, class, and backtrace. See #msg2str for
|
266
|
+
# the implementation if required.
|
267
|
+
#
|
268
|
+
# ### Bugs
|
269
|
+
#
|
270
|
+
# * Logfile is not locked.
|
271
|
+
# * Append open does not need to lock file.
|
272
|
+
# * If the OS supports multi I/O, records possibly may be mixed.
|
273
|
+
#
|
274
|
+
def add: (Integer severity, ?untyped message, ?untyped progname) ?{ () -> untyped } -> true
|
275
|
+
|
276
|
+
# Close the logging device.
|
277
|
+
#
|
278
|
+
def close: () -> untyped
|
279
|
+
|
280
|
+
# Returns the date format being used. See #datetime_format=
|
281
|
+
#
|
282
|
+
def datetime_format: () -> String?
|
283
|
+
|
284
|
+
# Set date-time format.
|
285
|
+
#
|
286
|
+
# `datetime_format`
|
287
|
+
# : A string suitable for passing to `strftime`.
|
288
|
+
#
|
289
|
+
def datetime_format=: (String datetime_format) -> String
|
290
|
+
| (nil datetime_format) -> nil
|
291
|
+
|
292
|
+
# Log a `DEBUG` message.
|
293
|
+
#
|
294
|
+
# See #info for more information.
|
295
|
+
#
|
296
|
+
def debug: (?untyped progname) ?{ () -> untyped } -> true
|
297
|
+
|
298
|
+
# Sets the severity to DEBUG.
|
299
|
+
#
|
300
|
+
def debug!: () -> Integer
|
301
|
+
|
302
|
+
# Returns `true` iff the current severity level allows for the printing of
|
303
|
+
# `DEBUG` messages.
|
304
|
+
#
|
305
|
+
def debug?: () -> bool
|
306
|
+
|
307
|
+
# Log an `ERROR` message.
|
308
|
+
#
|
309
|
+
# See #info for more information.
|
310
|
+
#
|
311
|
+
def error: (?untyped progname) ?{ () -> untyped } -> true
|
312
|
+
|
313
|
+
# Sets the severity to ERROR.
|
314
|
+
#
|
315
|
+
def error!: () -> Integer
|
316
|
+
|
317
|
+
# Returns `true` iff the current severity level allows for the printing of
|
318
|
+
# `ERROR` messages.
|
319
|
+
#
|
320
|
+
def error?: () -> bool
|
321
|
+
|
322
|
+
# Log a `FATAL` message.
|
323
|
+
#
|
324
|
+
# See #info for more information.
|
325
|
+
#
|
326
|
+
def fatal: (?untyped progname) ?{ () -> untyped } -> true
|
327
|
+
|
328
|
+
# Sets the severity to FATAL.
|
329
|
+
#
|
330
|
+
def fatal!: () -> Integer
|
331
|
+
|
332
|
+
# Returns `true` iff the current severity level allows for the printing of
|
333
|
+
# `FATAL` messages.
|
334
|
+
#
|
335
|
+
def fatal?: () -> bool
|
336
|
+
|
337
|
+
# Logging formatter, as a `Proc` that will take four arguments and return the
|
338
|
+
# formatted message. The arguments are:
|
339
|
+
#
|
340
|
+
# `severity`
|
341
|
+
# : The Severity of the log message.
|
342
|
+
# `time`
|
343
|
+
# : A Time instance representing when the message was logged.
|
344
|
+
# `progname`
|
345
|
+
# : The #progname configured, or passed to the logger method.
|
346
|
+
# `msg`
|
347
|
+
# : The *Object* the user passed to the log message; not necessarily a String.
|
348
|
+
#
|
349
|
+
#
|
350
|
+
# The block should return an Object that can be written to the logging device
|
351
|
+
# via `write`. The default formatter is used when no formatter is set.
|
352
|
+
#
|
353
|
+
def formatter: () -> (_Formatter | nil)
|
354
|
+
|
355
|
+
def formatter=: (_Formatter) -> _Formatter
|
356
|
+
| (nil) -> nil
|
357
|
+
|
358
|
+
# Log an `INFO` message.
|
359
|
+
#
|
360
|
+
# `message`
|
361
|
+
# : The message to log; does not need to be a String.
|
362
|
+
# `progname`
|
363
|
+
# : In the block form, this is the #progname to use in the log message. The
|
364
|
+
# default can be set with #progname=.
|
365
|
+
# `block`
|
366
|
+
# : Evaluates to the message to log. This is not evaluated unless the
|
367
|
+
# logger's level is sufficient to log the message. This allows you to
|
368
|
+
# create potentially expensive logging messages that are only called when
|
369
|
+
# the logger is configured to show them.
|
370
|
+
#
|
371
|
+
#
|
372
|
+
# ### Examples
|
373
|
+
#
|
374
|
+
# logger.info("MainApp") { "Received connection from #{ip}" }
|
375
|
+
# # ...
|
376
|
+
# logger.info "Waiting for input from user"
|
377
|
+
# # ...
|
378
|
+
# logger.info { "User typed #{input}" }
|
379
|
+
#
|
380
|
+
# You'll probably stick to the second form above, unless you want to provide a
|
381
|
+
# program name (which you can do with #progname= as well).
|
382
|
+
#
|
383
|
+
# ### Return
|
384
|
+
#
|
385
|
+
# See #add.
|
386
|
+
#
|
387
|
+
def info: (?untyped progname) ?{ () -> untyped } -> true
|
388
|
+
|
389
|
+
# Sets the severity to INFO.
|
390
|
+
#
|
391
|
+
def info!: () -> Integer
|
392
|
+
|
393
|
+
# Returns `true` iff the current severity level allows for the printing of
|
394
|
+
# `INFO` messages.
|
395
|
+
#
|
396
|
+
def info?: () -> bool
|
397
|
+
|
398
|
+
# Logging severity threshold (e.g. `Logger::INFO`).
|
399
|
+
#
|
400
|
+
def level: () -> Integer
|
401
|
+
|
402
|
+
# Set logging severity threshold.
|
403
|
+
#
|
404
|
+
# `severity`
|
405
|
+
# : The Severity of the log message.
|
406
|
+
#
|
407
|
+
#
|
408
|
+
def level=: (Integer | String severity) -> Integer
|
409
|
+
|
410
|
+
alias log add
|
411
|
+
|
412
|
+
# Program name to include in log messages.
|
413
|
+
#
|
414
|
+
def progname: () -> untyped
|
415
|
+
|
416
|
+
def progname=: (untyped) -> untyped
|
417
|
+
|
418
|
+
# ### Args
|
419
|
+
#
|
420
|
+
# `logdev`
|
421
|
+
# : The log device. This is a filename (String) or IO object (typically
|
422
|
+
# `STDOUT`, `STDERR`, or an open file). reopen the same filename if it is
|
423
|
+
# `nil`, do nothing for IO. Default is `nil`.
|
424
|
+
#
|
425
|
+
#
|
426
|
+
# ### Description
|
427
|
+
#
|
428
|
+
# Reopen a log device.
|
429
|
+
#
|
430
|
+
def reopen: () -> self
|
431
|
+
| (logdev?) -> self
|
432
|
+
|
433
|
+
# Logging severity threshold (e.g. `Logger::INFO`).
|
434
|
+
#
|
435
|
+
alias sev_threshold level
|
436
|
+
|
437
|
+
alias sev_threshold= level=
|
438
|
+
|
439
|
+
# Log an `UNKNOWN` message. This will be printed no matter what the logger's
|
440
|
+
# level is.
|
441
|
+
#
|
442
|
+
# See #info for more information.
|
443
|
+
#
|
444
|
+
def unknown: (?untyped progname) ?{ () -> untyped } -> true
|
445
|
+
|
446
|
+
# Log a `WARN` message.
|
447
|
+
#
|
448
|
+
# See #info for more information.
|
449
|
+
#
|
450
|
+
def warn: (?untyped progname) ?{ () -> untyped } -> true
|
451
|
+
|
452
|
+
# Sets the severity to WARN.
|
453
|
+
#
|
454
|
+
def warn!: () -> Integer
|
455
|
+
|
456
|
+
# Returns `true` iff the current severity level allows for the printing of
|
457
|
+
# `WARN` messages.
|
458
|
+
#
|
459
|
+
def warn?: () -> bool
|
460
|
+
|
461
|
+
private
|
462
|
+
|
463
|
+
def format_message: (String severity, Time datetime, untyped progname, untyped msg) -> _ToS
|
464
|
+
|
465
|
+
def format_severity: (Integer severity) -> String
|
466
|
+
|
467
|
+
# ### Args
|
468
|
+
#
|
469
|
+
# `logdev`
|
470
|
+
# : The log device. This is a filename (String) or IO object (typically
|
471
|
+
# `STDOUT`, `STDERR`, or an open file).
|
472
|
+
# `shift_age`
|
473
|
+
# : Number of old log files to keep, **or** frequency of rotation (`daily`,
|
474
|
+
# `weekly` or `monthly`). Default value is 0, which disables log file
|
475
|
+
# rotation.
|
476
|
+
# `shift_size`
|
477
|
+
# : Maximum logfile size in bytes (only applies when `shift_age` is a positive
|
478
|
+
# Integer). Defaults to `1048576` (1MB).
|
479
|
+
# `level`
|
480
|
+
# : Logging severity threshold. Default values is Logger::DEBUG.
|
481
|
+
# `progname`
|
482
|
+
# : Program name to include in log messages. Default value is nil.
|
483
|
+
# `formatter`
|
484
|
+
# : Logging formatter. Default values is an instance of Logger::Formatter.
|
485
|
+
# `datetime_format`
|
486
|
+
# : Date and time format. Default value is '%Y-%m-%d %H:%M:%S'.
|
487
|
+
# `binmode`
|
488
|
+
# : Use binary mode on the log device. Default value is false.
|
489
|
+
# `shift_period_suffix`
|
490
|
+
# : The log file suffix format for `daily`, `weekly` or `monthly` rotation.
|
491
|
+
# Default is '%Y%m%d'.
|
492
|
+
#
|
493
|
+
#
|
494
|
+
# ### Description
|
495
|
+
#
|
496
|
+
# Create an instance.
|
497
|
+
#
|
498
|
+
def initialize: (logdev logdev, ?Numeric | String shift_age, ?Integer shift_size, ?shift_period_suffix: String, ?binmode: bool, ?datetime_format: String, ?formatter: _Formatter, ?progname: String, ?level: Integer) -> void
|
499
|
+
end
|
500
|
+
|
501
|
+
Logger::ProgName: String
|
502
|
+
|
503
|
+
# Severity label for logging (max 5 chars).
|
504
|
+
#
|
505
|
+
Logger::SEV_LABEL: Array[String]
|
506
|
+
|
507
|
+
Logger::VERSION: String
|