kelredd-useful 0.3.5 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -0
- data/lib/useful/ruby_helpers/logger.rb +127 -0
- data/lib/useful/version.rb +2 -2
- metadata +24 -7
data/Rakefile
CHANGED
@@ -0,0 +1,127 @@
|
|
1
|
+
module Useful; end
|
2
|
+
module Useful::RubyHelpers; end
|
3
|
+
|
4
|
+
require 'useful/ruby_extensions/object'
|
5
|
+
|
6
|
+
module Useful::RubyHelpers::Logger
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def benchmark(*args, &block)
|
10
|
+
require 'benchmark'
|
11
|
+
name, msg, opts = handle_log_args(args)
|
12
|
+
result = nil
|
13
|
+
ms = Benchmark.measure { result = block.call }.real
|
14
|
+
name += ' ' unless name.empty?
|
15
|
+
name += '(%.1fms)' % [ms*1000]
|
16
|
+
log(name, msg, opts)
|
17
|
+
result
|
18
|
+
end
|
19
|
+
|
20
|
+
def log(*args)
|
21
|
+
name, msg, opts = handle_log_args(args)
|
22
|
+
opts[:level] ||= 'debug'
|
23
|
+
unless logger.respond_to?(opts[:level].to_s)
|
24
|
+
raise Exception, "no '#{opts[:level].to_s.upcase}' logger provided"
|
25
|
+
end
|
26
|
+
if name.empty? && msg.empty?
|
27
|
+
nil
|
28
|
+
else
|
29
|
+
returning formatted_log_msg(name, msg, opts) do |log_msg|
|
30
|
+
logger.send(opts[:level].to_s, log_msg)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
[:debug, :info, :warn, :error].each do |level|
|
36
|
+
define_method(level) do |*args|
|
37
|
+
if args.last.kind_of?(::Hash)
|
38
|
+
args.last[:level] = level
|
39
|
+
else
|
40
|
+
args << {:level => level}
|
41
|
+
end
|
42
|
+
log(*args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def logger
|
49
|
+
raise Exception, "to use these logging features, override the self.logger method and provide a log4r-style logger"
|
50
|
+
end
|
51
|
+
|
52
|
+
def formatted_log_msg(name, msg, opts)
|
53
|
+
opts[:newlines] ||= 0
|
54
|
+
opts[:color] = true if opts[:color].nil?
|
55
|
+
opts[:prefix] ||= case opts[:level].to_s
|
56
|
+
when 'info'
|
57
|
+
"*" # green
|
58
|
+
when 'warn'
|
59
|
+
"~" # cyan
|
60
|
+
when 'error'
|
61
|
+
"!" # red
|
62
|
+
else # debug
|
63
|
+
"-"
|
64
|
+
end
|
65
|
+
color_level = opts[:color] ? opts[:level].to_s : 'none'
|
66
|
+
color = case color_level
|
67
|
+
when 'debug'
|
68
|
+
"33;1" # yellow
|
69
|
+
when 'info'
|
70
|
+
"32;1" # green
|
71
|
+
when 'warn'
|
72
|
+
"36;1" # cyan
|
73
|
+
when 'error'
|
74
|
+
"31;1" # red
|
75
|
+
else
|
76
|
+
# white
|
77
|
+
"0"
|
78
|
+
end
|
79
|
+
color_reg = "0;#{color}"
|
80
|
+
color_underlined = "4;#{color}"
|
81
|
+
"\e[0m#{"\n"*opts[:newlines].to_i.abs} \e[#{color_reg}m#{opts[:prefix]}\e[0m#{opts[:prefix].empty? ? '' : ' '}\e[#{color_underlined}m#{name}\e[0m#{name.empty? ? '' : ' '}#{msg}"
|
82
|
+
end
|
83
|
+
|
84
|
+
def handle_log_args(args)
|
85
|
+
case (the_args = args.flatten).length
|
86
|
+
when 3
|
87
|
+
the_args
|
88
|
+
when 2
|
89
|
+
if the_args.last.kind_of?(::Hash)
|
90
|
+
["", the_args.first, the_args.last]
|
91
|
+
else
|
92
|
+
[the_args.first, the_args.last, {}]
|
93
|
+
end
|
94
|
+
when 0
|
95
|
+
["", "", {}]
|
96
|
+
else
|
97
|
+
["", the_args.first, {}]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
module InstanceMethods
|
105
|
+
def benchmark(*args, &block)
|
106
|
+
self.class.send(:benchmark, *args, &block)
|
107
|
+
end
|
108
|
+
|
109
|
+
def log(*args)
|
110
|
+
self.class.send(:log, *args)
|
111
|
+
end
|
112
|
+
|
113
|
+
[:debug, :info, :warn, :error].each do |level|
|
114
|
+
define_method(level) do |*args|
|
115
|
+
self.class.send(level, *args)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
def self.included(receiver)
|
123
|
+
receiver.extend ClassMethods
|
124
|
+
receiver.send :include, InstanceMethods
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
data/lib/useful/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kelredd-useful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-08-
|
18
|
+
date: 2010-08-30 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -51,9 +51,25 @@ dependencies:
|
|
51
51
|
type: :development
|
52
52
|
version_requirements: *id002
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
|
-
name:
|
54
|
+
name: log4r
|
55
55
|
prerelease: false
|
56
56
|
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 19
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 1
|
65
|
+
- 0
|
66
|
+
version: 1.1.0
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: json
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
57
73
|
none: false
|
58
74
|
requirements:
|
59
75
|
- - ">="
|
@@ -63,7 +79,7 @@ dependencies:
|
|
63
79
|
- 0
|
64
80
|
version: "0"
|
65
81
|
type: :runtime
|
66
|
-
version_requirements: *
|
82
|
+
version_requirements: *id004
|
67
83
|
description:
|
68
84
|
email: kelly@kelredd.com
|
69
85
|
executables: []
|
@@ -107,6 +123,7 @@ files:
|
|
107
123
|
- lib/useful/ruby_extensions/true_class.rb
|
108
124
|
- lib/useful/ruby_extensions.rb
|
109
125
|
- lib/useful/ruby_extensions_with_activesupport.rb
|
126
|
+
- lib/useful/ruby_helpers/logger.rb
|
110
127
|
- lib/useful/shoulda_macros/test_unit/classes.rb
|
111
128
|
- lib/useful/shoulda_macros/test_unit/context.rb
|
112
129
|
- lib/useful/shoulda_macros/test_unit/files.rb
|