tp2 0.8.1 → 0.8.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 75f66b5020339be69ee5ee0122eea1673a4f96330cedfec95cde805ef43b9221
4
- data.tar.gz: efc072f110cacc703c6ae75f4bade2a0753c73ecffae0f3055f6346eb165c79d
3
+ metadata.gz: 809c5e549581a17a4ba9190599b4804a594de55e4fa0645f214b9fd5826a7409
4
+ data.tar.gz: a23b48ceeee575bdbe2f3cffb20f5f27ef7f1a0f706a7d076996705aaebad77d
5
5
  SHA512:
6
- metadata.gz: 5d522b6958b99135fe730b514d30ec8be9e18e01cabce1892e188ca2c255c5f963c4fc6ecf6c6b6f82b0c7ef9fc10c56dd2020a57bfbf9ea7abf27b3a3c88d2b
7
- data.tar.gz: 64a166087f1bae5a36f648ae2068ffa731fc4ecba6e77c400a311fc8be82eea795cf25b640f94f91731d7d3d7484645837b462a0058159a3eb68f2787e54e8d5
6
+ metadata.gz: 2ebd43993a905da8d06ee9278983ce336a3a7b7480a6194b3e3b55df3985de196f352f3b8d3f9d9b4c6e6f7ab8329d04c78060df214f0b339ae6ad06b8febbb9
7
+ data.tar.gz: 19b2a6b4cdf703910dd84c62613d880d300b9057c603b503b244c2760b6b2034ddbcbb84099408251ed31b8f1a03d1603f3c9b0d6f32bb0633e707cc459e7f5d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # Version 0.8.2 2025-06-07
2
+
3
+ - Improve logging
4
+
1
5
  # Version 0.8 2025-06-07
2
6
 
3
7
  - Add silent mode
@@ -16,6 +16,7 @@ module TP2
16
16
 
17
17
  @done = nil
18
18
  @response_headers = nil
19
+ @status = nil
19
20
  end
20
21
 
21
22
  def run
@@ -161,16 +162,17 @@ module TP2
161
162
  private
162
163
 
163
164
  def log(request, response_headers)
165
+ return if !@opts[:log]
166
+
164
167
  request_headers = request.headers
165
168
  str = format(
166
- "%<stamp>s %<method>s %<path>s %<status>s %<tx>d\n",
167
- stamp: Time.now.strftime('%Y-%m-%d %H:%M:%S.%3N'),
169
+ "%<method>s %<path>s %<status>s %<tx>d",
168
170
  method: request_headers[':method'].upcase,
169
171
  path: request_headers[':path'],
170
172
  status: @status,
171
173
  tx: request_headers[':tx']
172
174
  )
173
- @machine.write_async(1, str)
175
+ @opts[:log].log(str)
174
176
  end
175
177
 
176
178
  RE_REQUEST_LINE = /^([a-z]+)\s+([^\s]+)\s+(http\/[0-9\.]{1,3})/i.freeze
data/lib/tp2/logger.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TP2
4
+ class Logger
5
+ def initialize(machine, fd = STDOUT.fileno, **opts)
6
+ @machine = machine
7
+ @fd = fd
8
+ @opts = opts
9
+ end
10
+
11
+ def log(str)
12
+ str = format("%s %s\n", Time.now.strftime('%Y-%m-%d %H:%M:%S.%3N'), str)
13
+ @machine.write_async(@fd, str)
14
+ end
15
+ end
16
+ end
data/lib/tp2/server.rb CHANGED
@@ -15,9 +15,9 @@ module TP2
15
15
  TP2::RackAdapter.load(opts[:app_location])
16
16
  end
17
17
 
18
- def self.tp2_app(opts)
18
+ def self.tp2_app(machine, opts)
19
19
  if opts[:app_location]
20
- puts "Loading app at #{opts[:app_location]}" if opts[:log]
20
+ opts[:log]&.log("Loading app at #{opts[:app_location]}")
21
21
  require opts[:app_location]
22
22
 
23
23
  opts.merge!(TP2.config)
@@ -40,7 +40,7 @@ module TP2
40
40
  def app_from_opts
41
41
  case @opts[:app_type]
42
42
  when nil, :tp2
43
- Server.tp2_app(@opts)
43
+ Server.tp2_app(@machine, @opts)
44
44
  when :rack
45
45
  Server.rack_app(@opts)
46
46
  when :static
@@ -67,7 +67,7 @@ module TP2
67
67
  @accept_fibers << @machine.spin { accept_incoming(fd) }
68
68
  end
69
69
  bind_string = bind_info.map { it.join(':') }.join(", ")
70
- @machine.puts("Listening on #{bind_string}") if @opts[:log]
70
+ @opts[:log]&.log("Listening on #{bind_string}")
71
71
 
72
72
  # map fibers
73
73
  @connection_fiber_map = {}
@@ -123,6 +123,8 @@ module TP2
123
123
  end
124
124
 
125
125
  def graceful_shutdown
126
+ @opts[:log]&.log("Shutting down gracefully...")
127
+
126
128
  # stop listening
127
129
  close_all_server_fds
128
130
  @machine.snooze
data/lib/tp2/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module TP2
2
- VERSION = '0.8.1'
2
+ VERSION = '0.8.2'
3
3
  end
data/lib/tp2.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'uringmachine'
4
4
  require 'tp2/version'
5
5
  require 'tp2/server'
6
+ require 'tp2/logger'
6
7
 
7
8
  class UringMachine
8
9
  def puts(str)
@@ -24,16 +25,23 @@ module TP2
24
25
 
25
26
  class << self
26
27
  def run(opts = nil, &app)
27
- return if @in_run
28
+ if @in_run
29
+ @config = opts if opts
30
+ @config[:app] = app if app
31
+ return
32
+ end
28
33
 
29
34
  opts ||= @config || {}
30
35
  begin
31
36
  @in_run = true
32
37
  machine = UM.new
38
+
39
+ machine.puts(TP2::BANNER) if opts[:banner]
40
+ opts[:log] = opts[:log] && TP2::Logger.new(machine, **opts)
41
+
33
42
  server = Server.new(machine, opts, &app)
34
43
 
35
44
  setup_signal_handling(machine, Fiber.current)
36
- machine.puts(TP2::BANNER) if opts[:banner]
37
45
  server.run
38
46
  ensure
39
47
  @in_run = false
@@ -59,7 +67,6 @@ module TP2
59
67
  # to be done
60
68
  def watch_for_int_signal(machine, queue, fiber)
61
69
  sig = machine.shift(queue)
62
- machine.puts("\nGot #{sig} signal, shutting down gracefully...")
63
70
  machine.schedule(fiber, UM::Terminate.new)
64
71
  end
65
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tp2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -71,6 +71,7 @@ files:
71
71
  - examples/simple.rb
72
72
  - lib/tp2.rb
73
73
  - lib/tp2/http1_adapter.rb
74
+ - lib/tp2/logger.rb
74
75
  - lib/tp2/rack_adapter.rb
75
76
  - lib/tp2/request_extensions.rb
76
77
  - lib/tp2/server.rb