dlog 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/gem.yml +1 -1
  2. data/lib/dlog.rb +50 -15
  3. data/test/examples.rb +21 -0
  4. metadata +4 -2
data/gem.yml CHANGED
@@ -1,4 +1,4 @@
1
- version: "0.2"
1
+ version: "0.2.1"
2
2
  summary: "{d,r}log"
3
3
  description: |
4
4
  A logging library mainly for development purposes.
data/lib/dlog.rb CHANGED
@@ -190,6 +190,48 @@ module Dlog
190
190
  def info(*args); args.first; end
191
191
  def debug(*args); args.first; end
192
192
  end
193
+
194
+ module NoBenchmark
195
+ extend self
196
+
197
+ def error(*args, &block); yield; end
198
+ def warn(*args, &block); yield; end
199
+ def info(*args, &block); yield; end
200
+ def debug(*args, &block); yield; end
201
+ end
202
+
203
+ module Benchmark
204
+ extend self
205
+
206
+ def benchmark(severity, args, &block)
207
+ args.push "#{args.pop}:" if args.last.is_a?(String)
208
+
209
+ start = Time.now
210
+ r = yield
211
+
212
+ args.push "%d msecs" % (1000 * (Time.now - start))
213
+ Dlog.log severity, args, 2
214
+ r
215
+ rescue
216
+ args.push "exception raised after #{"%d msecs" % (1000 * (Time.now - start)) }"
217
+ Dlog.log severity, args, 2
218
+ raise
219
+ end
220
+
221
+ def error(*args, &block)
222
+ benchmark :error, args, &block
223
+ end
224
+
225
+ def warn(*args, &block)
226
+ benchmark :warn, args, &block
227
+ end
228
+ def info(*args, &block)
229
+ benchmark :info, args, &block
230
+ end
231
+ def debug(*args, &block)
232
+ benchmark :debug, args, &block
233
+ end
234
+ end
193
235
  end
194
236
 
195
237
  class Object
@@ -200,36 +242,29 @@ class Object
200
242
  quiet ? Dlog::Nolog : Dlog
201
243
  else
202
244
  Dlog.log :info, args unless quiet
203
- args.first
245
+ args.last
204
246
  end
205
247
  end
206
248
 
207
249
  def rlog(*args)
208
- quiet = Dlog.quiet? || Dlog.release?
250
+ quiet = Dlog.quiet?
209
251
 
210
252
  if args.empty?
211
253
  quiet ? Dlog::Nolog : Dlog
212
254
  else
213
255
  Dlog.log :warn, args unless quiet
214
- args.first
256
+ args.last
215
257
  end
216
258
  end
217
259
 
218
260
  def benchmark(*args, &block)
219
261
  if Dlog.quiet?
220
- return yield
262
+ Dlog::NoBenchmark
263
+ elsif args.empty? && !block_given?
264
+ Dlog::Benchmark
265
+ else
266
+ Dlog::Benchmark.benchmark :info, args, &block
221
267
  end
222
-
223
- start = Time.now
224
- r = yield
225
- args.push ": %3d msecs" % (1000 * (Time.now - start))
226
- Dlog.log :warn, args
227
- r
228
- rescue
229
- args.push ": exception raised after #{"%3d msecs" % (1000 * (Time.now - start)) }"
230
- args.push :source => caller[0]
231
- rlog *args
232
- raise
233
268
  end
234
269
 
235
270
  private
data/test/examples.rb ADDED
@@ -0,0 +1,21 @@
1
+ #
2
+ #
3
+ # load "dlog.rb"
4
+
5
+ #
6
+ #
7
+ dlog 1
8
+
9
+ #
10
+ #
11
+ rlog.warn "2"
12
+
13
+ #
14
+ #
15
+ benchmark do Thread.send(:sleep, 0.015); 1 end
16
+ benchmark "a message" do Thread.send(:sleep, 0.01); 1 end
17
+ benchmark.info "a message" do Thread.send(:sleep, 0.02); 1 end
18
+
19
+
20
+ benchmark "raise" do Thread.send(:sleep, 0.02); raise "1" end rescue nil
21
+ benchmark.warn "raise" do Thread.send(:sleep, 0.02); raise "1" end rescue nil
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dlog
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- version: "0.2"
9
+ - 1
10
+ version: 0.2.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - pboy
@@ -35,6 +36,7 @@ files:
35
36
  - init.rb
36
37
  - lib/dlog.rb
37
38
  - script/console
39
+ - test/examples.rb
38
40
  - test/test.rb
39
41
  - vex/gem.rake
40
42
  - vex/gem.rb