dumb-logger 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/features/output.feature +27 -3
- data/lib/dumb-logger.rb +123 -17
- data/lib/dumb-logger/version.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97dcaa8c2a86896da031d59fce6d9e76629ae0ad
|
4
|
+
data.tar.gz: cc24b7b01e23defa63650a640f71d264dfa0d122
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1502f4709b0138d8618fdae48cde9e614c9371795734f6a8f8491f0ffbc923f626ff41d68301d6a52bc8d4312b5beeb32124cc0a2e71c10da37e5ed5d5145667
|
7
|
+
data.tar.gz: a01939fc773088661a1865147f6be643017227cd620a7d854b00784852c7174665c79e4fd54fef36b886a7561e7f29eae610b4ba970bd9c3c45f9d6d38452ace
|
data/features/output.feature
CHANGED
@@ -41,8 +41,20 @@ Feature: Test the output generated
|
|
41
41
|
And stderr should contain exactly "a message"
|
42
42
|
And stdout should contain exactly ""
|
43
43
|
|
44
|
-
Scenario: Single-line message with no newline (alternate)
|
45
|
-
When I invoke the logger with ("a message", :newline=>false)
|
44
|
+
Scenario: Single-line message with no newline (alternate 1)
|
45
|
+
When I invoke the logger with ("a message", {:newline=>false})
|
46
|
+
Then the return value should be 0
|
47
|
+
And stderr should contain exactly "a message"
|
48
|
+
And stdout should contain exactly ""
|
49
|
+
|
50
|
+
Scenario: Single-line message with no newline (alternate 2)
|
51
|
+
When I invoke the logger with ("a message", {:return=>false})
|
52
|
+
Then the return value should be 0
|
53
|
+
And stderr should contain exactly "a message"
|
54
|
+
And stdout should contain exactly ""
|
55
|
+
|
56
|
+
Scenario: Single-line message with :newline overriding :return
|
57
|
+
When I invoke the logger with ("a message", {:newline=>false,:return=>true})
|
46
58
|
Then the return value should be 0
|
47
59
|
And stderr should contain exactly "a message"
|
48
60
|
And stdout should contain exactly ""
|
@@ -53,12 +65,24 @@ Feature: Test the output generated
|
|
53
65
|
And stderr should contain exactly "line 1\nline 2"
|
54
66
|
And stdout should contain exactly ""
|
55
67
|
|
56
|
-
Scenario: Multi-line message with no newline (alternate)
|
68
|
+
Scenario: Multi-line message with no newline (alternate 1)
|
57
69
|
When I invoke the logger with ("line 1",{:newline=>false},"line 2")
|
58
70
|
Then the return value should be 0
|
59
71
|
And stderr should contain exactly "line 1\nline 2"
|
60
72
|
And stdout should contain exactly ""
|
61
73
|
|
74
|
+
Scenario: Multi-line message with no newline (alternate 2)
|
75
|
+
When I invoke the logger with ("line 1",{:return=>false},"line 2")
|
76
|
+
Then the return value should be 0
|
77
|
+
And stderr should contain exactly "line 1\nline 2"
|
78
|
+
And stdout should contain exactly ""
|
79
|
+
|
80
|
+
Scenario: Multi-line message with :newline overriding :return
|
81
|
+
When I invoke the logger with ("line 1",{:return=>true,:newline=>false},"line 2")
|
82
|
+
Then the return value should be 0
|
83
|
+
And stderr should contain exactly "line 1\nline 2"
|
84
|
+
And stdout should contain exactly ""
|
85
|
+
|
62
86
|
Scenario: Single-line message with instance prefix
|
63
87
|
When I set the prefix to '[instance-prefix] '
|
64
88
|
And I invoke the logger with ("a message")
|
data/lib/dumb-logger.rb
CHANGED
@@ -368,6 +368,13 @@ class DumbLogger
|
|
368
368
|
@sink_io = File.open(@options[:sink], (self.append? ? 'a' : 'w'))
|
369
369
|
@options[:needs_close] = true
|
370
370
|
end
|
371
|
+
#
|
372
|
+
# Note that you cannot seek-position the $stdout or $stderr
|
373
|
+
# streams. However, there doesn't seem to be a clear way to
|
374
|
+
# determine that, so we'll wrap the actual seek (in {#message}) in
|
375
|
+
# a rescue block.
|
376
|
+
#
|
377
|
+
@options[:needs_seek] = true
|
371
378
|
@sink_io.sync = true if (@sink_io.respond_to?(:sync=))
|
372
379
|
return self.sink
|
373
380
|
end # def sink=
|
@@ -504,21 +511,42 @@ class DumbLogger
|
|
504
511
|
# @param [Array<Array,String,Symbol,Integer,Hash>] args
|
505
512
|
# * The last integer in the array will be treated as the report's
|
506
513
|
# loglevel; default is `0`.
|
514
|
+
#
|
515
|
+
# **Overridden by `:level` or `:mask` in an options hash passed
|
516
|
+
# to the method.**
|
507
517
|
# * Any `Array` elements in the arguments will be merged and the
|
508
518
|
# values interpreted as level labels (see {#label_levels}). If
|
509
|
-
# loglevels are bitmasks, the labeled levels
|
510
|
-
# otherwise the lowest labeled level will be
|
519
|
+
# loglevels are bitmasks (see {#level_style}), the labeled levels
|
520
|
+
# are `OR`ed together; otherwise the lowest labeled level will be
|
521
|
+
# used for the message.
|
522
|
+
#
|
523
|
+
# **Overridden by `:level` or `:mask` in an options hash passed
|
524
|
+
# to the method.**
|
511
525
|
# * Any `Hash` elements in the array will be merged and will
|
512
526
|
# temporarily override instance-wide options -- *e.g.*,
|
513
|
-
# `{ :prefix
|
514
|
-
#
|
515
|
-
#
|
516
|
-
#
|
517
|
-
# `
|
527
|
+
# `{ :prefix => 'alt' }` . Valid *per*-call options are:
|
528
|
+
# * `:prefix => String`
|
529
|
+
# * `:level => Integer`
|
530
|
+
# (takes precedence over `:mask` if {#level_style} is {USE_LEVELS}.)
|
531
|
+
# * `:mask => Integer`
|
532
|
+
# (takes precedence over `:level` if {#level_style} is {USE_BITMASK}.)
|
533
|
+
# * `:newline => Boolean`
|
534
|
+
# (takes precedence over {DumbLogger::NO_NL} in the argument list)
|
535
|
+
# * `:return => Boolean`
|
536
|
+
# (alias for `:newline`; **deprecated after 1.0.0**)
|
537
|
+
# * If the {DumbLogger::NO_NL} value (a `Symbol`) appears in the
|
538
|
+
# array, or a hash element of `:newline => false` (or `:return =>
|
539
|
+
# false`), the report will not include a terminating newline
|
540
|
+
# (useful for `"progress:..done"` reports).
|
518
541
|
# * Any strings in the array are treated as text to be reported,
|
519
|
-
# one
|
520
|
-
#
|
521
|
-
#
|
542
|
+
# one *per* line. Each line will begin with the value of
|
543
|
+
# logger's value of {#prefix} (or any overriding value set with
|
544
|
+
# `:prefix` in a hash of options), and only the final line is
|
545
|
+
# subject to the {DumbLogger::NO_NL} special-casing.
|
546
|
+
#
|
547
|
+
# @note
|
548
|
+
# Use of the `:return` hash option is deprecated in versions after
|
549
|
+
# 1.0.0. Use `:newline` instead.
|
522
550
|
#
|
523
551
|
# @return [nil,Integer]
|
524
552
|
# Returns either `nil` if the message's loglevel is higher than the
|
@@ -527,7 +555,7 @@ class DumbLogger
|
|
527
555
|
# If integer levels are being used, a non-`nil` return value is
|
528
556
|
# that of the message. If bitmask levels are being used, the
|
529
557
|
# return value is a mask of the active level bits that applied to
|
530
|
-
# the message -- *i.e.*, `
|
558
|
+
# the message -- *i.e.*, `message_mask & logging_mask` .
|
531
559
|
#
|
532
560
|
def message(*args)
|
533
561
|
#
|
@@ -535,32 +563,110 @@ class DumbLogger
|
|
535
563
|
# list. This makes the calling format very flexible.
|
536
564
|
#
|
537
565
|
(symopts, args) = args.partition { |elt| elt.kind_of?(Symbol) }
|
566
|
+
#
|
567
|
+
# Pull out any symbols that are actually names for levels (or
|
568
|
+
# masks). The args variable now contains no Symbol elements.
|
569
|
+
#
|
538
570
|
symlevels = (symopts & self.labeled_levels.keys).map { |o|
|
539
571
|
self.labeled_levels[o]
|
540
572
|
}.compact
|
573
|
+
#
|
574
|
+
# Now any option hashes.
|
575
|
+
#
|
541
576
|
(hashopts, args) = args.partition { |elt| elt.kind_of?(Hash) }
|
542
577
|
hashopts = hashopts.reduce(:merge) || {}
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
578
|
+
#
|
579
|
+
# All hashes have been removed from the args array, and merged
|
580
|
+
# together into a single *per*-message options hash.
|
581
|
+
#
|
582
|
+
|
583
|
+
#
|
584
|
+
# Now some fun stuff. The appropriate loglevel/mask for this
|
585
|
+
# message can come from
|
586
|
+
#
|
587
|
+
# * Integers in the argument array (last one takes precedence); or
|
588
|
+
# * Values of symbolic level/mask labels (again, last one takes
|
589
|
+
# precedence, and overrides any explicit integers); or
|
590
|
+
# * Any `:level` or `:mask` value in the options hash (which one
|
591
|
+
# of those takes precedence depends on the current logging
|
592
|
+
# style).
|
593
|
+
#
|
594
|
+
(lvls, args) = args.partition { |elt| elt.kind_of?(Integer) }
|
595
|
+
if (self.log_levels?)
|
596
|
+
level = hashopts[:level] || hashopts[:mask]
|
597
|
+
else
|
598
|
+
level = hashopts[:mask] || hashopts[:level]
|
599
|
+
end
|
600
|
+
if (level.nil?)
|
601
|
+
if (self.log_levels?)
|
602
|
+
level = symlevels.empty? ? lvls.last : symlevels.min
|
603
|
+
else
|
604
|
+
level = symlevels.empty? ? lvls.last : symlevels.reduce(:|)
|
605
|
+
end
|
606
|
+
end
|
607
|
+
level ||= 0
|
608
|
+
#
|
609
|
+
# We should now have a minimum logging level, or an ORed bitmask,
|
610
|
+
# in variable 'level'. Time to see if it meets our criteria.
|
611
|
+
#
|
547
612
|
unless (level.zero?)
|
548
613
|
if (self.log_levels?)
|
549
|
-
return nil if (
|
614
|
+
return nil if (self.loglevel < level)
|
550
615
|
elsif (self.log_masks?)
|
551
|
-
level
|
616
|
+
level &= self.logmask
|
552
617
|
return nil if (level.zero?)
|
553
618
|
end
|
554
619
|
end
|
620
|
+
#
|
621
|
+
# Looks like the request loglevel/mask is within the logger's
|
622
|
+
# requirements, so let's build the output string.
|
623
|
+
#
|
555
624
|
prefix_text = hashopts[:prefix] || self.prefix
|
556
625
|
text = prefix_text + args.join("\n#{prefix_text}")
|
626
|
+
#
|
627
|
+
# The :return option is overridden by :newline, and renamed to it
|
628
|
+
# if :newline isn't already in the options hash.
|
629
|
+
#
|
630
|
+
if (hashopts.key?(:return) && (! hashopts.key?(:newline)))
|
631
|
+
hashopts[:newline] = hashopts[:return]
|
632
|
+
hashopts.delete(:return)
|
633
|
+
end
|
557
634
|
unless (hashopts.key?(:newline))
|
558
635
|
hashopts[:newline]= (! symopts.include?(NO_NL))
|
559
636
|
end
|
560
637
|
text << "\n" if (hashopts[:newline])
|
638
|
+
#
|
639
|
+
# Okey. If the output stream is marked 'volatile', it's one of
|
640
|
+
# our special sinks and we need to evaluate it on every write.
|
641
|
+
#
|
561
642
|
stream = @options[:volatile] ? eval(self.sink.to_s) : @sink_io
|
643
|
+
#
|
644
|
+
# If this is our first write to this sink, make sure we position
|
645
|
+
# properly before writing!
|
646
|
+
#
|
647
|
+
if (@options[:needs_seek] && stream.respond_to?(:seek))
|
648
|
+
poz = (self.append? \
|
649
|
+
? IO::SEEK_END \
|
650
|
+
: IO::SEEK_SET)
|
651
|
+
begin
|
652
|
+
#
|
653
|
+
# Can't seek on some things, so just catch the exception and
|
654
|
+
# ignore it.
|
655
|
+
#
|
656
|
+
stream.seek(0, poz)
|
657
|
+
rescue Errno::ESPIPE => exc
|
658
|
+
#
|
659
|
+
# Do nothing..
|
660
|
+
#
|
661
|
+
end
|
662
|
+
@options[:needs_seek] = false
|
663
|
+
end
|
562
664
|
stream.write(text)
|
563
665
|
stream.flush if (@options[:volatile])
|
666
|
+
#
|
667
|
+
# All done! Return the level, or mask bits, that resulted in the
|
668
|
+
# text being transmitted.
|
669
|
+
#
|
564
670
|
return level
|
565
671
|
end # def message
|
566
672
|
|
data/lib/dumb-logger/version.rb
CHANGED
@@ -31,6 +31,15 @@ class DumbLogger
|
|
31
31
|
@version = @version.change(:major => 1,
|
32
32
|
:tiny => 0)
|
33
33
|
|
34
|
+
#
|
35
|
+
# * Corrected & deprecated `:return` {#message} option key
|
36
|
+
# * Added seek-to on first write to a sink
|
37
|
+
# * Corrected (and documented) applicable level/mask determination
|
38
|
+
# for {#message}
|
39
|
+
# * Added some tests for `:return` *versus* `:newline`
|
40
|
+
#
|
41
|
+
@version = @version.bump(:tiny)
|
42
|
+
|
34
43
|
#
|
35
44
|
# How to advance the version number.
|
36
45
|
#
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dumb-logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ken Coar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|