libo11y 0.2.0 → 0.3.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/CHANGELOG.md +170 -21
- data/README.md +77 -8
- data/lib/libo11y/{proc.rb → child_process.rb} +12 -2
- data/lib/libo11y/db.rb +22 -10
- data/lib/libo11y/detached.rb +241 -29
- data/lib/libo11y/http_client.rb +48 -15
- data/lib/libo11y/internal/bridges.rb +3553 -255
- data/lib/libo11y/internal/endpoint.rb +7 -1
- data/lib/libo11y/internal/env.rb +177 -59
- data/lib/libo11y/internal/error_type.rb +17 -0
- data/lib/libo11y/internal/histogram_buckets.rb +42 -0
- data/lib/libo11y/internal/http_common.rb +2 -0
- data/lib/libo11y/internal/identity.rb +10 -1
- data/lib/libo11y/internal/injected.rb +178 -0
- data/lib/libo11y/internal/logging_device.rb +141 -32
- data/lib/libo11y/internal/path_match.rb +148 -0
- data/lib/libo11y/internal/pii.rb +185 -65
- data/lib/libo11y/internal/pipeline.rb +141 -34
- data/lib/libo11y/internal/policy.rb +556 -52
- data/lib/libo11y/internal/propagators.rb +29 -6
- data/lib/libo11y/internal/quiet_inspect.rb +20 -0
- data/lib/libo11y/internal/resource.rb +187 -52
- data/lib/libo11y/internal/sdk_load.rb +64 -0
- data/lib/libo11y/internal/sql_text.rb +365 -67
- data/lib/libo11y/internal/summary.rb +2 -1
- data/lib/libo11y/options.rb +159 -1
- data/lib/libo11y/rack.rb +257 -44
- data/lib/libo11y/record.rb +70 -7
- data/lib/libo11y/redact.rb +87 -21
- data/lib/libo11y/signals.rb +70 -29
- data/lib/libo11y/spec/app_registry.rb +97 -61
- data/lib/libo11y/spec/yaml_v3.rb +179 -111
- data/lib/libo11y/telemetry.rb +315 -53
- data/lib/libo11y/testing.rb +31 -19
- data/lib/libo11y/version.rb +1 -1
- data/lib/libo11y.rb +1 -1
- metadata +94 -39
|
@@ -6,10 +6,12 @@ require "opentelemetry"
|
|
|
6
6
|
require_relative "../spec"
|
|
7
7
|
require_relative "bridges"
|
|
8
8
|
require_relative "pii"
|
|
9
|
+
require_relative "quiet_inspect"
|
|
9
10
|
|
|
10
11
|
module Libo11y
|
|
11
12
|
FORCE_EMIT_FIBER_KEY = :libo11y_force_emit
|
|
12
13
|
REENTRY_FIBER_KEY = :libo11y_logging_reentry
|
|
14
|
+
private_constant :FORCE_EMIT_FIBER_KEY, :REENTRY_FIBER_KEY
|
|
13
15
|
|
|
14
16
|
# Runs the block with every log record in it passing the logger's level,
|
|
15
17
|
# for records that must always be written, such as audit records. Returns
|
|
@@ -37,13 +39,16 @@ module Libo11y
|
|
|
37
39
|
# Correlates every record with the active OpenTelemetry span context, applies attr_replacer
|
|
38
40
|
# followed by SensitivityPolicy, and fans out to JSON console, OTLP logs, and extra sinks.
|
|
39
41
|
class StructuredLogger < ::Logger
|
|
42
|
+
include QuietInspect
|
|
43
|
+
|
|
40
44
|
SEVERITY_TO_OTEL = {
|
|
41
45
|
::Logger::DEBUG => [5, "DEBUG"],
|
|
42
46
|
::Logger::INFO => [9, "INFO"],
|
|
43
47
|
::Logger::WARN => [13, "WARN"],
|
|
44
48
|
::Logger::ERROR => [17, "ERROR"],
|
|
45
49
|
::Logger::FATAL => [21, "FATAL"],
|
|
46
|
-
|
|
50
|
+
# Ruby ranks UNKNOWN above FATAL and prints it as ANY.
|
|
51
|
+
::Logger::UNKNOWN => [21, "ANY"]
|
|
47
52
|
}.freeze
|
|
48
53
|
|
|
49
54
|
def initialize(
|
|
@@ -73,21 +78,36 @@ module Libo11y
|
|
|
73
78
|
force = Libo11y.force_emit?
|
|
74
79
|
return true if !force && severity < level
|
|
75
80
|
|
|
76
|
-
message
|
|
81
|
+
# As Logger#add: with no message and no block, the program name is the message.
|
|
82
|
+
message = block_given? ? yield : (progname || self.progname) if message.nil?
|
|
77
83
|
|
|
78
|
-
body, attrs = extract_message_and_attrs(message)
|
|
84
|
+
body, attrs, origin = extract_message_and_attrs(message)
|
|
79
85
|
emit_record(
|
|
80
86
|
severity: severity,
|
|
81
87
|
body: body,
|
|
82
88
|
attrs: attrs,
|
|
83
89
|
event_name: nil,
|
|
84
90
|
force: force,
|
|
85
|
-
raise_on_sink_error: false
|
|
91
|
+
raise_on_sink_error: false,
|
|
92
|
+
seen: origin
|
|
86
93
|
)
|
|
87
94
|
true
|
|
88
95
|
end
|
|
89
96
|
|
|
90
|
-
|
|
97
|
+
# Logger#log is an alias of Logger#add, bound to Logger's own method,
|
|
98
|
+
# so it is aliased again here.
|
|
99
|
+
alias log add
|
|
100
|
+
|
|
101
|
+
# Returns true whether or not the record passed the level, as
|
|
102
|
+
# Logger#add does.
|
|
103
|
+
#
|
|
104
|
+
# @param deadline [Float, nil] a point on the monotonic clock after
|
|
105
|
+
# which no further sink is started, for a record written during a
|
|
106
|
+
# shutdown
|
|
107
|
+
# @param otel [Boolean] false keeps the record out of the OpenTelemetry
|
|
108
|
+
# log pipeline, for a record about that pipeline shutting down
|
|
109
|
+
def log_with_attrs(severity, body, attrs = {}, event_name: nil, force: nil, raise_on_sink_error: false, deadline: nil, otel: true,
|
|
110
|
+
seen: nil, key_origins: nil)
|
|
91
111
|
forced = force.nil? ? Libo11y.force_emit? : force
|
|
92
112
|
emit_record(
|
|
93
113
|
severity: severity,
|
|
@@ -95,8 +115,13 @@ module Libo11y
|
|
|
95
115
|
attrs: attrs || {},
|
|
96
116
|
event_name: event_name,
|
|
97
117
|
force: forced,
|
|
98
|
-
raise_on_sink_error: raise_on_sink_error
|
|
118
|
+
raise_on_sink_error: raise_on_sink_error,
|
|
119
|
+
deadline: deadline,
|
|
120
|
+
otel: otel,
|
|
121
|
+
seen: seen || attrs,
|
|
122
|
+
key_origins: key_origins
|
|
99
123
|
)
|
|
124
|
+
true
|
|
100
125
|
end
|
|
101
126
|
|
|
102
127
|
def debug(progname = nil, attrs = nil, &)
|
|
@@ -129,48 +154,79 @@ module Libo11y
|
|
|
129
154
|
super(progname, &)
|
|
130
155
|
end
|
|
131
156
|
|
|
157
|
+
# Logger#<< writes a message as it is, whatever the level. Here it is
|
|
158
|
+
# a record like any other, at INFO, without the line break a caller
|
|
159
|
+
# such as Rack::CommonLogger ends it with. Returns the logger.
|
|
160
|
+
def <<(message)
|
|
161
|
+
log_with_attrs(::Logger::INFO, message.to_s.chomp, {}, force: true)
|
|
162
|
+
self
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def unknown(progname = nil, attrs = nil, &)
|
|
166
|
+
return log_with_attrs(::Logger::UNKNOWN, progname, attrs) if attrs.is_a?(Hash)
|
|
167
|
+
|
|
168
|
+
super(progname, &)
|
|
169
|
+
end
|
|
170
|
+
|
|
132
171
|
private
|
|
133
172
|
|
|
134
173
|
def extract_message_and_attrs(message)
|
|
135
174
|
if message.is_a?(Hash)
|
|
136
175
|
dup = message.dup
|
|
137
176
|
body = dup.delete(:msg) || dup.delete("msg") || dup.delete(:message) || dup.delete("message") || ""
|
|
138
|
-
[body.to_s, dup]
|
|
177
|
+
[body.to_s, dup, message]
|
|
139
178
|
else
|
|
140
|
-
[message.to_s, {}]
|
|
179
|
+
[message.to_s, {}, nil]
|
|
141
180
|
end
|
|
142
181
|
end
|
|
143
182
|
|
|
144
|
-
def emit_record(severity:, body:, attrs:, event_name:, force:, raise_on_sink_error:
|
|
183
|
+
def emit_record(severity:, body:, attrs:, event_name:, force:, raise_on_sink_error:, deadline: nil, otel: true,
|
|
184
|
+
seen: nil, key_origins: nil)
|
|
145
185
|
return if Thread.current[REENTRY_FIBER_KEY]
|
|
146
186
|
# One level gates every sink. force_emit and events pass it.
|
|
147
187
|
return unless force || severity >= level
|
|
148
188
|
|
|
149
189
|
Thread.current[REENTRY_FIBER_KEY] = true
|
|
150
190
|
begin
|
|
151
|
-
record = build_record(severity, body, attrs, event_name)
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
191
|
+
record = build_record(severity, body, attrs, event_name, seen: seen, key_origins: key_origins)
|
|
192
|
+
sinks(record, raise_on_sink_error, deadline, otel).each do |write|
|
|
193
|
+
break if deadline && Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
|
|
194
|
+
|
|
195
|
+
write.call
|
|
196
|
+
end
|
|
155
197
|
ensure
|
|
156
198
|
Thread.current[REENTRY_FIBER_KEY] = false
|
|
157
199
|
end
|
|
158
200
|
end
|
|
159
201
|
|
|
202
|
+
# One step per sink that is on: the console, OTLP, then the extra sinks.
|
|
203
|
+
def sinks(record, raise_on_sink_error, deadline, otel)
|
|
204
|
+
steps = []
|
|
205
|
+
if @console_enabled && @console_writer
|
|
206
|
+
steps << -> { write_console(raise_on_sink_error: raise_on_sink_error, deadline: deadline) { console_line(record) } }
|
|
207
|
+
end
|
|
208
|
+
steps << -> { emit_otel(record, raise_on_sink_error: raise_on_sink_error) } if otel && @otel_logger
|
|
209
|
+
steps + @extra_sinks.map { |sink| -> { emit_extra(sink, record, raise_on_sink_error: raise_on_sink_error) } }
|
|
210
|
+
end
|
|
211
|
+
|
|
160
212
|
# The record every sink gets: the trace identity of the active span,
|
|
161
213
|
# and the attributes after the replacer and the policy, classified once.
|
|
162
|
-
def build_record(severity, body, attrs, event_name)
|
|
214
|
+
def build_record(severity, body, attrs, event_name, seen: nil, key_origins: nil)
|
|
163
215
|
now = Time.now.utc
|
|
164
216
|
severity_number, severity_text = SEVERITY_TO_OTEL.fetch(severity, [9, "INFO"])
|
|
165
217
|
span_context = OpenTelemetry::Trace.current_span.context
|
|
166
218
|
|
|
167
|
-
attributes = attrs.
|
|
168
|
-
|
|
169
|
-
|
|
219
|
+
attributes = attrs.each_with_object({}) { |(key, value), out| out[SensitivityPolicy.normalize_key(key)] = value }
|
|
220
|
+
named = event_name && !event_name.empty?
|
|
221
|
+
attributes.delete(Spec::EVENT_NAME_KEY) if named
|
|
222
|
+
classified = @policy.classify_log_attributes(attributes, replacer: @attr_replacer, seen: seen || attrs, key_origins: key_origins)
|
|
170
223
|
# trace_id and span_id are reserved: they come from the active span
|
|
171
224
|
# only. Removed after the replacer, which could bring them back.
|
|
172
225
|
classified.delete("trace_id")
|
|
173
226
|
classified.delete("span_id")
|
|
227
|
+
# An event's name is reserved too. It is written after the replacer
|
|
228
|
+
# has run, so the replacer can neither remove it nor add another.
|
|
229
|
+
classified = { Spec::EVENT_NAME_KEY => event_name }.merge(classified.except(Spec::EVENT_NAME_KEY)) if named
|
|
174
230
|
|
|
175
231
|
{
|
|
176
232
|
timestamp: now,
|
|
@@ -196,31 +252,41 @@ module Libo11y
|
|
|
196
252
|
end
|
|
197
253
|
|
|
198
254
|
def console_line(record)
|
|
199
|
-
payload = { "time" => record[:timestamp].iso8601(6), "level" => record[:severity_text], "msg" => record[:body] }
|
|
255
|
+
payload = { "time" => record[:timestamp].iso8601(6), "level" => record[:severity_text], "msg" => utf8_safe(record[:body]) }
|
|
200
256
|
payload["trace_id"] = record[:trace_id] if record[:trace_id]
|
|
201
257
|
payload["span_id"] = record[:span_id] if record[:span_id]
|
|
202
258
|
record[:attributes].each do |key, value|
|
|
203
|
-
|
|
259
|
+
safe_key = utf8_safe(key)
|
|
260
|
+
payload[safe_key] = json_safe(value) unless %w[time level msg trace_id span_id].include?(safe_key)
|
|
204
261
|
end
|
|
205
|
-
"#{JSON.generate(payload)}\n"
|
|
262
|
+
"#{JSON.generate(payload, max_nesting: false)}\n"
|
|
206
263
|
end
|
|
207
264
|
|
|
208
265
|
# JSON has no NaN and no infinity, so those floats are written as the
|
|
209
|
-
# strings Go's slog writes: "NaN", "+Inf" and "-Inf".
|
|
266
|
+
# strings Go's slog writes: "NaN", "+Inf" and "-Inf". Strings are also
|
|
267
|
+
# normalized to valid UTF-8 so one malformed byte never drops the line.
|
|
210
268
|
def json_safe(value)
|
|
211
269
|
case value
|
|
270
|
+
when String then utf8_safe(value)
|
|
212
271
|
when Float then value.finite? ? value : PII.canonical_float(value)
|
|
213
|
-
when Hash then value.
|
|
272
|
+
when Hash then value.each_with_object({}) { |(k, v), out| out[utf8_safe(k.to_s)] = json_safe(v) }
|
|
214
273
|
when Array then value.map { |v| json_safe(v) }
|
|
215
274
|
else value
|
|
216
275
|
end
|
|
217
276
|
end
|
|
218
277
|
|
|
278
|
+
def utf8_safe(str)
|
|
279
|
+
SensitivityPolicy.utf8_safe(str)
|
|
280
|
+
end
|
|
281
|
+
|
|
219
282
|
# Encodes and writes one line. A failure to encode or to write stays
|
|
220
283
|
# in this sink, so the record still reaches the others.
|
|
221
|
-
def write_console(raise_on_sink_error:)
|
|
284
|
+
def write_console(raise_on_sink_error:, deadline: nil)
|
|
222
285
|
line = yield
|
|
223
286
|
@mutex.synchronize do
|
|
287
|
+
# Checked with the lock held: the wait for it may have outlasted the deadline.
|
|
288
|
+
next if deadline && Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
|
|
289
|
+
|
|
224
290
|
@console_writer.write(line)
|
|
225
291
|
end
|
|
226
292
|
rescue StandardError
|
|
@@ -228,26 +294,69 @@ module Libo11y
|
|
|
228
294
|
end
|
|
229
295
|
|
|
230
296
|
def emit_otel(record, raise_on_sink_error:)
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
297
|
+
attributes = flatten_for_otel(record[:attributes])
|
|
298
|
+
name = utf8_safe(record[:event_name])
|
|
299
|
+
if name && !name.empty?
|
|
300
|
+
# Written after flattening, so a group that flattens to event.name
|
|
301
|
+
# cannot replace it, and last, because over the attribute count
|
|
302
|
+
# limit the SDK drops the oldest entries.
|
|
303
|
+
attributes.delete(Spec::EVENT_NAME_KEY)
|
|
304
|
+
attributes[Spec::EVENT_NAME_KEY] = name
|
|
305
|
+
end
|
|
306
|
+
Bridges::PolicyLogRecordProcessor.with_classified do
|
|
307
|
+
@otel_logger.on_emit(
|
|
308
|
+
timestamp: record[:timestamp],
|
|
309
|
+
observed_timestamp: record[:observed_timestamp],
|
|
310
|
+
severity_number: record[:severity_number],
|
|
311
|
+
severity_text: record[:severity_text],
|
|
312
|
+
body: utf8_safe(record[:body]),
|
|
313
|
+
event_name: name,
|
|
314
|
+
attributes: attributes,
|
|
315
|
+
context: OpenTelemetry::Context.current
|
|
316
|
+
)
|
|
317
|
+
end
|
|
236
318
|
rescue StandardError
|
|
237
319
|
raise if raise_on_sink_error
|
|
238
320
|
end
|
|
239
321
|
|
|
322
|
+
# A list as an OpenTelemetry attribute value: strings, numbers or
|
|
323
|
+
# booleans, all of one kind. A member that is a Hash or a list becomes
|
|
324
|
+
# JSON, and a list of several kinds, or with a nil, becomes strings.
|
|
325
|
+
# The SDK refuses any other list and then stops checking the record's
|
|
326
|
+
# remaining attributes.
|
|
327
|
+
def otel_list(list)
|
|
328
|
+
members = list.map do |el|
|
|
329
|
+
if el.is_a?(Hash) || el.is_a?(Array)
|
|
330
|
+
JSON.generate(json_safe(el), max_nesting: false)
|
|
331
|
+
elsif el.is_a?(String)
|
|
332
|
+
utf8_safe(el)
|
|
333
|
+
elsif el.is_a?(Integer) && !el.between?(SensitivityPolicy::INT64_MIN, SensitivityPolicy::INT64_MAX)
|
|
334
|
+
el.to_s
|
|
335
|
+
else
|
|
336
|
+
el
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
one_kind = [[String], [Integer, Float], [TrueClass, FalseClass]].any? do |kinds|
|
|
340
|
+
members.all? { |member| kinds.any? { |kind| member.is_a?(kind) } }
|
|
341
|
+
end
|
|
342
|
+
one_kind ? members : members.map { |member| utf8_safe(member.to_s) }
|
|
343
|
+
end
|
|
344
|
+
|
|
240
345
|
def flatten_for_otel(hash, prefix = nil, out = {})
|
|
241
346
|
hash.each do |k, v|
|
|
242
|
-
|
|
347
|
+
key = utf8_safe(k.to_s)
|
|
348
|
+
full = prefix ? "#{prefix}.#{key}" : key
|
|
243
349
|
if v.is_a?(Hash)
|
|
244
350
|
flatten_for_otel(v, full, out)
|
|
245
351
|
elsif v.is_a?(Array)
|
|
246
|
-
out[full] = v
|
|
247
|
-
elsif v.is_a?(String)
|
|
352
|
+
out[full] = otel_list(v)
|
|
353
|
+
elsif v.is_a?(String)
|
|
354
|
+
out[full] = utf8_safe(v)
|
|
355
|
+
elsif (v.is_a?(Integer) && v.between?(SensitivityPolicy::INT64_MIN, SensitivityPolicy::INT64_MAX)) ||
|
|
356
|
+
v.is_a?(Float) || v.is_a?(TrueClass) || v.is_a?(FalseClass)
|
|
248
357
|
out[full] = v
|
|
249
358
|
elsif !v.nil?
|
|
250
|
-
out[full] = v.to_s
|
|
359
|
+
out[full] = utf8_safe(v.to_s)
|
|
251
360
|
end
|
|
252
361
|
end
|
|
253
362
|
out
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Libo11y
|
|
4
|
+
module Internal
|
|
5
|
+
# Go's path.Match, so an ignore pattern means in Ruby what it means in
|
|
6
|
+
# every other libo11y implementation. `*` matches any run of characters
|
|
7
|
+
# other than `/`, `?` one such character, `[...]` a character class
|
|
8
|
+
# (negated with `^`, ranges with `-`), and a backslash escapes the next
|
|
9
|
+
# character. The pattern must match the whole name. Ruby's File.fnmatch
|
|
10
|
+
# differs: it gives `**` a recursive meaning.
|
|
11
|
+
module PathMatch
|
|
12
|
+
# A malformed pattern, such as an unclosed character class.
|
|
13
|
+
class BadPattern < ArgumentError; end
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
# @raise [BadPattern] for a malformed pattern, also when the match has
|
|
18
|
+
# already failed
|
|
19
|
+
def match?(pattern, name)
|
|
20
|
+
until pattern.empty?
|
|
21
|
+
star, chunk, pattern = scan_chunk(pattern)
|
|
22
|
+
# A trailing star matches the rest of name unless it has a slash.
|
|
23
|
+
return !name.include?("/") if star && chunk.empty?
|
|
24
|
+
|
|
25
|
+
rest = match_chunk(chunk, name)
|
|
26
|
+
# The last chunk must exhaust name, or the star may still find a later match.
|
|
27
|
+
if rest && (rest.empty? || !pattern.empty?)
|
|
28
|
+
name = rest
|
|
29
|
+
next
|
|
30
|
+
end
|
|
31
|
+
skipped = star ? match_after_star(chunk, name, last: pattern.empty?) : nil
|
|
32
|
+
if skipped
|
|
33
|
+
name = skipped
|
|
34
|
+
next
|
|
35
|
+
end
|
|
36
|
+
# Before reporting no match, check that the rest of the pattern is well formed.
|
|
37
|
+
until pattern.empty?
|
|
38
|
+
_, chunk, pattern = scan_chunk(pattern)
|
|
39
|
+
match_chunk(chunk, "")
|
|
40
|
+
end
|
|
41
|
+
return false
|
|
42
|
+
end
|
|
43
|
+
name.empty?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Whether the pattern is well formed. Matching the empty name reads the
|
|
47
|
+
# whole pattern.
|
|
48
|
+
def valid?(pattern)
|
|
49
|
+
match?(pattern, "")
|
|
50
|
+
true
|
|
51
|
+
rescue BadPattern
|
|
52
|
+
false
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Matches chunk after skipping one or more characters of name, never a slash.
|
|
56
|
+
def match_after_star(chunk, name, last:)
|
|
57
|
+
name.each_char.with_index do |char, index|
|
|
58
|
+
break if char == "/"
|
|
59
|
+
|
|
60
|
+
rest = match_chunk(chunk, name[(index + 1)..])
|
|
61
|
+
return rest if rest && !(last && !rest.empty?)
|
|
62
|
+
end
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Splits off the next run of pattern characters without a star, and
|
|
67
|
+
# says whether stars came before it.
|
|
68
|
+
def scan_chunk(pattern)
|
|
69
|
+
stripped = pattern.sub(/\A\*+/, "")
|
|
70
|
+
star = stripped.length != pattern.length
|
|
71
|
+
in_range = false
|
|
72
|
+
index = 0
|
|
73
|
+
while index < stripped.length
|
|
74
|
+
case stripped[index]
|
|
75
|
+
when "\\" then index += 1 if index + 1 < stripped.length # a trailing backslash is reported by match_chunk
|
|
76
|
+
when "[" then in_range = true
|
|
77
|
+
when "]" then in_range = false
|
|
78
|
+
when "*" then return [star, stripped[0, index], stripped[index..]] unless in_range
|
|
79
|
+
end
|
|
80
|
+
index += 1
|
|
81
|
+
end
|
|
82
|
+
[star, stripped, ""]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Matches a chunk without stars against the start of name and returns
|
|
86
|
+
# what is left of name, or nil. After a mismatch it keeps reading the
|
|
87
|
+
# chunk, so a malformed chunk raises whatever name is.
|
|
88
|
+
def match_chunk(chunk, name)
|
|
89
|
+
failed = false
|
|
90
|
+
until chunk.empty?
|
|
91
|
+
failed ||= name.empty?
|
|
92
|
+
case chunk[0]
|
|
93
|
+
when "["
|
|
94
|
+
char = failed ? "" : name[0]
|
|
95
|
+
name = name[1..] unless failed
|
|
96
|
+
matched, chunk = match_class(chunk[1..], char)
|
|
97
|
+
failed ||= !matched
|
|
98
|
+
next
|
|
99
|
+
when "?"
|
|
100
|
+
unless failed
|
|
101
|
+
failed = name[0] == "/"
|
|
102
|
+
name = name[1..]
|
|
103
|
+
end
|
|
104
|
+
chunk = chunk[1..]
|
|
105
|
+
next
|
|
106
|
+
when "\\"
|
|
107
|
+
chunk = chunk[1..]
|
|
108
|
+
raise BadPattern, "libo11y: a pattern ends in a backslash" if chunk.empty?
|
|
109
|
+
end
|
|
110
|
+
unless failed
|
|
111
|
+
failed = chunk[0] != name[0]
|
|
112
|
+
name = name[1..]
|
|
113
|
+
end
|
|
114
|
+
chunk = chunk[1..]
|
|
115
|
+
end
|
|
116
|
+
failed ? nil : name
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Reads a character class after its opening bracket. Returns whether
|
|
120
|
+
# char is in it and the pattern after the closing bracket.
|
|
121
|
+
def match_class(chunk, char)
|
|
122
|
+
negated = chunk.start_with?("^")
|
|
123
|
+
chunk = chunk[1..] if negated
|
|
124
|
+
matched = false
|
|
125
|
+
ranges = 0
|
|
126
|
+
loop do
|
|
127
|
+
return [matched != negated, chunk[1..]] if chunk.start_with?("]") && ranges.positive?
|
|
128
|
+
|
|
129
|
+
low, chunk = class_char(chunk)
|
|
130
|
+
high = low
|
|
131
|
+
high, chunk = class_char(chunk[1..]) if chunk[0] == "-"
|
|
132
|
+
matched ||= !char.empty? && low <= char && char <= high
|
|
133
|
+
ranges += 1
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Reads one character of a class, which a backslash may escape.
|
|
138
|
+
def class_char(chunk)
|
|
139
|
+
raise BadPattern, "libo11y: malformed character class in a pattern" if chunk.empty? || ["-", "]"].include?(chunk[0])
|
|
140
|
+
|
|
141
|
+
chunk = chunk[1..] if chunk[0] == "\\"
|
|
142
|
+
raise BadPattern, "libo11y: malformed character class in a pattern" if chunk.empty? || chunk.length == 1
|
|
143
|
+
|
|
144
|
+
[chunk[0], chunk[1..]]
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|