logger 1.6.2 → 1.6.4

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: 513b3bfa57e4f617976d220ea3ab04b6a72f8ce0f3734b80a2de276b623d6b49
4
- data.tar.gz: 26bf01af278a060e23d1d80c83e46d474933d2af9425360223656f62c04c8df6
3
+ metadata.gz: 7342324d3e0712ca848158153398c4a83117ba503860ecf9e6bdfb6af659c671
4
+ data.tar.gz: ec5f49b614221c3e2278b3d21781b3270035225db3d69ee9823c59055caf8b9f
5
5
  SHA512:
6
- metadata.gz: be737e542a0c9763d280be61f8fe045c72db441f4c25032c3ac332c926abf21c0e957dcdabe17f8cc403348636c71c1e0ee5d9220ea9108572b8d3457e1e00f9
7
- data.tar.gz: bcc5494d61bda582003ba814ceadce03476ca58cbecad391053b5d7b98538d3ee047bb2cce72efb80af54df0947ec82c9c993124dd91dbb1c421401b356b613f
6
+ metadata.gz: 93c4e8130c1c1e80453aaa72b2c9af936755bb07af4d5f6ee3f2101f26de7219c786db26849c0b17f1c741471182ebd96738457c2c2db7649ac03666e54fd475
7
+ data.tar.gz: 238eb90c2b827d02a1e81c07451a65d8e6e2f9c626446cdbace821c4617cacc0b2ad7c69d4b7d92c35405a9f73ef6a5d6ba4215e3252792adb201e9c0518c947
@@ -30,29 +30,13 @@ class Logger
30
30
  end
31
31
 
32
32
  def write(message)
33
- begin
33
+ handle_write_errors("writing") do
34
34
  synchronize do
35
35
  if @shift_age and @dev.respond_to?(:stat)
36
- begin
37
- check_shift_log
38
- rescue *@reraise_write_errors
39
- raise
40
- rescue
41
- warn("log shifting failed. #{$!}")
42
- end
43
- end
44
- begin
45
- @dev.write(message)
46
- rescue *@reraise_write_errors
47
- raise
48
- rescue
49
- warn("log writing failed. #{$!}")
36
+ handle_write_errors("shifting") {check_shift_log}
50
37
  end
38
+ handle_write_errors("writing") {@dev.write(message)}
51
39
  end
52
- rescue *@reraise_write_errors
53
- raise
54
- rescue Exception => ignored
55
- warn("log writing failed. #{ignored}")
56
40
  end
57
41
  end
58
42
 
@@ -83,6 +67,12 @@ class Logger
83
67
 
84
68
  private
85
69
 
70
+ # :stopdoc:
71
+
72
+ MODE = File::WRONLY | File::APPEND
73
+ MODE_TO_OPEN = MODE | File::SHARE_DELETE | File::BINARY
74
+ MODE_TO_CREATE = MODE_TO_OPEN | File::CREAT | File::EXCL
75
+
86
76
  def set_dev(log)
87
77
  if log.respond_to?(:write) and log.respond_to?(:close)
88
78
  @dev = log
@@ -93,34 +83,61 @@ class Logger
93
83
  end
94
84
  else
95
85
  @dev = open_logfile(log)
96
- @dev.sync = true
97
- @dev.binmode if @binmode
98
86
  @filename = log
99
87
  end
100
88
  end
101
89
 
90
+ if MODE_TO_OPEN == MODE
91
+ def fixup_mode(dev, filename)
92
+ dev
93
+ end
94
+ else
95
+ def fixup_mode(dev, filename)
96
+ return dev if @binmode
97
+ dev.autoclose = false
98
+ old_dev = dev
99
+ dev = File.new(dev.fileno, mode: MODE, path: filename)
100
+ old_dev.close
101
+ PathAttr.set_path(dev, filename) if defined?(PathAttr)
102
+ dev
103
+ end
104
+ end
105
+
102
106
  def open_logfile(filename)
103
107
  begin
104
- File.open(filename, (File::WRONLY | File::APPEND))
108
+ dev = File.open(filename, MODE_TO_OPEN)
105
109
  rescue Errno::ENOENT
106
110
  create_logfile(filename)
111
+ else
112
+ dev = fixup_mode(dev, filename)
113
+ dev.sync = true
114
+ dev.binmode if @binmode
115
+ dev
107
116
  end
108
117
  end
109
118
 
110
119
  def create_logfile(filename)
111
120
  begin
112
- logdev = File.open(filename, (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
121
+ logdev = File.open(filename, MODE_TO_CREATE)
113
122
  logdev.flock(File::LOCK_EX)
123
+ logdev = fixup_mode(logdev, filename)
114
124
  logdev.sync = true
115
125
  logdev.binmode if @binmode
116
126
  add_log_header(logdev)
117
127
  logdev.flock(File::LOCK_UN)
128
+ logdev
118
129
  rescue Errno::EEXIST
119
130
  # file is created by another process
120
- logdev = open_logfile(filename)
121
- logdev.sync = true
131
+ open_logfile(filename)
122
132
  end
123
- logdev
133
+ end
134
+
135
+ def handle_write_errors(mesg)
136
+ yield
137
+ rescue *@reraise_write_errors
138
+ raise
139
+ rescue
140
+ warn("log #{mesg} failed. #{$!}")
124
141
  end
125
142
 
126
143
  def add_log_header(file)
@@ -144,40 +161,33 @@ class Logger
144
161
  end
145
162
  end
146
163
 
147
- if /mswin|mingw|cygwin/ =~ RbConfig::CONFIG['host_os']
148
- def lock_shift_log
149
- yield
150
- end
151
- else
152
- def lock_shift_log
153
- retry_limit = 8
154
- retry_sleep = 0.1
155
- begin
156
- File.open(@filename, File::WRONLY | File::APPEND) do |lock|
157
- lock.flock(File::LOCK_EX) # inter-process locking. will be unlocked at closing file
158
- if File.identical?(@filename, lock) and File.identical?(lock, @dev)
159
- yield # log shifting
160
- else
161
- # log shifted by another process (i-node before locking and i-node after locking are different)
162
- @dev.close rescue nil
163
- @dev = open_logfile(@filename)
164
- @dev.sync = true
165
- end
166
- end
167
- rescue Errno::ENOENT
168
- # @filename file would not exist right after #rename and before #create_logfile
169
- if retry_limit <= 0
170
- warn("log rotation inter-process lock failed. #{$!}")
164
+ def lock_shift_log
165
+ retry_limit = 8
166
+ retry_sleep = 0.1
167
+ begin
168
+ File.open(@filename, MODE_TO_OPEN) do |lock|
169
+ lock.flock(File::LOCK_EX) # inter-process locking. will be unlocked at closing file
170
+ if File.identical?(@filename, lock) and File.identical?(lock, @dev)
171
+ yield # log shifting
171
172
  else
172
- sleep retry_sleep
173
- retry_limit -= 1
174
- retry_sleep *= 2
175
- retry
173
+ # log shifted by another process (i-node before locking and i-node after locking are different)
174
+ @dev.close rescue nil
175
+ @dev = open_logfile(@filename)
176
176
  end
177
177
  end
178
- rescue
179
- warn("log rotation inter-process lock failed. #{$!}")
178
+ rescue Errno::ENOENT
179
+ # @filename file would not exist right after #rename and before #create_logfile
180
+ if retry_limit <= 0
181
+ warn("log rotation inter-process lock failed. #{$!}")
182
+ else
183
+ sleep retry_sleep
184
+ retry_limit -= 1
185
+ retry_sleep *= 2
186
+ retry
187
+ end
180
188
  end
189
+ rescue
190
+ warn("log rotation inter-process lock failed. #{$!}")
181
191
  end
182
192
 
183
193
  def shift_log_age
@@ -212,3 +222,15 @@ class Logger
212
222
  end
213
223
  end
214
224
  end
225
+
226
+ File.open(__FILE__) do |f|
227
+ File.new(f.fileno, autoclose: false, path: "").path
228
+ rescue IOError
229
+ module PathAttr # :nodoc:
230
+ attr_reader :path
231
+
232
+ def self.set_path(file, path)
233
+ file.extend(self).instance_variable_set(:@path, path)
234
+ end
235
+ end
236
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Logger
4
- VERSION = "1.6.2"
4
+ VERSION = "1.6.4"
5
5
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 1.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  - SHIBATA Hiroshi
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-12-02 00:00:00.000000000 Z
12
+ date: 2024-12-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Provides a simple logging utility for outputting messages.
15
15
  email:
@@ -34,7 +34,7 @@ licenses:
34
34
  - Ruby
35
35
  - BSD-2-Clause
36
36
  metadata: {}
37
- post_install_message:
37
+ post_install_message:
38
38
  rdoc_options: []
39
39
  require_paths:
40
40
  - lib
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  version: '0'
51
51
  requirements: []
52
52
  rubygems_version: 3.5.11
53
- signing_key:
53
+ signing_key:
54
54
  specification_version: 4
55
55
  summary: Provides a simple logging utility for outputting messages.
56
56
  test_files: []