inversion 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
metadata.gz.sig CHANGED
Binary file
@@ -1,287 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # vim: set nosta noet ts=4 sw=4:
3
-
4
- require 'logger'
5
- require 'erb'
6
-
7
- require 'inversion' unless defined?( Inversion )
8
- require 'inversion/mixins'
9
-
10
-
11
- # A mixin that provides a top-level logging subsystem based on Logger.
12
- module Inversion::Logging
13
-
14
- ### Logging
15
- # Log levels
16
- LOG_LEVELS = {
17
- 'debug' => Logger::DEBUG,
18
- 'info' => Logger::INFO,
19
- 'warn' => Logger::WARN,
20
- 'error' => Logger::ERROR,
21
- 'fatal' => Logger::FATAL,
22
- }.freeze
23
- LOG_LEVEL_NAMES = LOG_LEVELS.invert.freeze
24
-
25
-
26
- ### Inclusion hook
27
- def self::extended( mod )
28
- super
29
-
30
- class << mod
31
- # the log formatter that will be used when the logging subsystem is reset
32
- attr_accessor :default_log_formatter
33
-
34
- # the logger that will be used when the logging subsystem is reset
35
- attr_accessor :default_logger
36
-
37
- # the logger that's currently in effect
38
- attr_accessor :logger
39
- alias_method :log, :logger
40
- alias_method :log=, :logger=
41
- end
42
-
43
- mod.default_logger = mod.logger = Logger.new( $stderr )
44
- mod.default_logger.level = case
45
- when $DEBUG then Logger::DEBUG
46
- when $VERBOSE then Logger::INFO
47
- else Logger::WARN end
48
- mod.default_log_formatter = Inversion::Logging::Formatter.new( mod.default_logger )
49
- end
50
-
51
-
52
- ### Reset the global logger object to the default
53
- def reset_logger
54
- self.logger = self.default_logger
55
- self.logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
56
- self.logger.formatter = self.default_log_formatter
57
- end
58
-
59
-
60
- ### Returns +true+ if the global logger has not been set to something other than
61
- ### the default one.
62
- def using_default_logger?
63
- return self.logger == self.default_logger
64
- end
65
-
66
-
67
- # A alternate formatter for Logger instances.
68
- class Formatter < Logger::Formatter
69
-
70
- # The format to output unless debugging is turned on
71
- DEFAULT_FORMAT = "[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"
72
-
73
- # The format to output if debugging is turned on
74
- DEFAULT_DEBUG_FORMAT = "[%1$s.%2$06d %3$d/%4$s] %5$5s {%6$s} -- %7$s\n"
75
-
76
-
77
- ### Initialize the formatter with a reference to the logger so it can check for log level.
78
- def initialize( logger, format=DEFAULT_FORMAT, debug=DEFAULT_DEBUG_FORMAT ) # :notnew:
79
- @logger = logger
80
- @format = format
81
- @debug_format = debug
82
-
83
- super()
84
- end
85
-
86
- ######
87
- public
88
- ######
89
-
90
- # The Logger object associated with the formatter
91
- attr_accessor :logger
92
-
93
- # The logging format string
94
- attr_accessor :format
95
-
96
- # The logging format string that's used when outputting in debug mode
97
- attr_accessor :debug_format
98
-
99
-
100
- ### Log using either the DEBUG_FORMAT if the associated logger is at ::DEBUG level or
101
- ### using FORMAT if it's anything less verbose.
102
- def call( severity, time, progname, msg )
103
- args = [
104
- time.strftime( '%Y-%m-%d %H:%M:%S' ), # %1$s
105
- time.usec, # %2$d
106
- Process.pid, # %3$d
107
- Thread.current == Thread.main ? 'main' : Thread.object_id, # %4$s
108
- severity, # %5$s
109
- progname, # %6$s
110
- msg # %7$s
111
- ]
112
-
113
- if @logger.level == Logger::DEBUG
114
- return self.debug_format % args
115
- else
116
- return self.format % args
117
- end
118
- end
119
- end # class Formatter
120
-
121
-
122
- # A ANSI-colorized formatter for Logger instances.
123
- class ColorFormatter < Logger::Formatter
124
-
125
- # Set some ANSI escape code constants (Shamelessly stolen from Perl's
126
- # Term::ANSIColor by Russ Allbery <rra@stanford.edu> and Zenin <zenin@best.com>
127
- ANSI_ATTRIBUTES = {
128
- 'clear' => 0,
129
- 'reset' => 0,
130
- 'bold' => 1,
131
- 'dark' => 2,
132
- 'underline' => 4,
133
- 'underscore' => 4,
134
- 'blink' => 5,
135
- 'reverse' => 7,
136
- 'concealed' => 8,
137
-
138
- 'black' => 30, 'on_black' => 40,
139
- 'red' => 31, 'on_red' => 41,
140
- 'green' => 32, 'on_green' => 42,
141
- 'yellow' => 33, 'on_yellow' => 43,
142
- 'blue' => 34, 'on_blue' => 44,
143
- 'magenta' => 35, 'on_magenta' => 45,
144
- 'cyan' => 36, 'on_cyan' => 46,
145
- 'white' => 37, 'on_white' => 47
146
- }
147
-
148
-
149
- ### Create a string that contains the ANSI codes specified and return it
150
- def self::ansi_code( *attributes )
151
- attributes.flatten!
152
- attributes.collect! {|at| at.to_s }
153
- return '' unless /(?:vt10[03]|xterm(?:-color)?|linux|screen)/i =~ ENV['TERM']
154
- attributes = ANSI_ATTRIBUTES.values_at( *attributes ).compact.join(';')
155
-
156
- if attributes.empty?
157
- return ''
158
- else
159
- return "\e[%sm" % attributes
160
- end
161
- end
162
-
163
-
164
- ### Colorize the given +string+ with the specified +attributes+ and
165
- ### return it, handling line-endings, color reset, etc.
166
- def self::colorize( *args )
167
- string = ''
168
-
169
- if block_given?
170
- string = yield
171
- else
172
- string = args.shift
173
- end
174
-
175
- ending = string[/(\s)$/] || ''
176
- string = string.rstrip
177
-
178
- return self.ansi_code( args.flatten ) + string + self.ansi_code( 'reset' ) + ending
179
- end
180
-
181
-
182
- # Color settings
183
- LEVEL_FORMATS = {
184
- :debug => colorize( :bold, :black ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s {%6$s} -- %7$s\n"},
185
- :info => colorize( :normal ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"},
186
- :warn => colorize( :bold, :yellow ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"},
187
- :error => colorize( :red ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"},
188
- :fatal => colorize( :bold, :red, :on_white ) {"[%1$s.%2$06d %3$d/%4$s] %5$5s -- %7$s\n"},
189
- }
190
-
191
-
192
- ### Initialize the formatter with a reference to the logger so it can check for log level.
193
- def initialize( logger, settings={} ) # :notnew:
194
- settings = LEVEL_FORMATS.merge( settings )
195
-
196
- @logger = logger
197
- @settings = settings
198
-
199
- super()
200
- end
201
-
202
- ######
203
- public
204
- ######
205
-
206
- # The Logger object associated with the formatter
207
- attr_accessor :logger
208
-
209
- # The formats, by level
210
- attr_accessor :settings
211
-
212
-
213
- ### Log using the format associated with the severity
214
- def call( severity, time, progname, msg )
215
- args = [
216
- time.strftime( '%Y-%m-%d %H:%M:%S' ), # %1$s
217
- time.usec, # %2$d
218
- Process.pid, # %3$d
219
- Thread.current == Thread.main ? 'main' : Thread.object_id, # %4$s
220
- severity, # %5$s
221
- progname, # %6$s
222
- msg # %7$s
223
- ]
224
-
225
- return self.settings[ severity.downcase.to_sym ] % args
226
- end
227
-
228
- end # class Formatter
229
-
230
-
231
- # An alternate formatter for Logger instances that outputs +div+ HTML
232
- # fragments.
233
- class HtmlFormatter < Logger::Formatter
234
- include ERB::Util # for html_escape()
235
-
236
- # The default HTML fragment that'll be used as the template for each log message.
237
- HTML_LOG_FORMAT = %q{
238
- <div class="log-message %5$s">
239
- <span class="log-time">%1$s.%2$06d</span>
240
- [
241
- <span class="log-pid">%3$d</span>
242
- /
243
- <span class="log-tid">%4$s</span>
244
- ]
245
- <span class="log-level">%5$s</span>
246
- :
247
- <span class="log-name">%6$s</span>
248
- <span class="log-message-text">%7$s</span>
249
- </div>
250
- }
251
-
252
- ### Override the logging formats with ones that generate HTML fragments
253
- def initialize( logger, format=HTML_LOG_FORMAT ) # :notnew:
254
- @logger = logger
255
- @format = format
256
- super()
257
- end
258
-
259
-
260
- ######
261
- public
262
- ######
263
-
264
- # The HTML fragment that will be used as a format() string for the log
265
- attr_accessor :format
266
-
267
-
268
- ### Return a log message composed out of the arguments formatted using the
269
- ### formatter's format string
270
- def call( severity, time, progname, msg )
271
- args = [
272
- time.strftime( '%Y-%m-%d %H:%M:%S' ), # %1$s
273
- time.usec, # %2$d
274
- Process.pid, # %3$d
275
- Thread.current == Thread.main ? 'main' : Thread.object_id, # %4$s
276
- severity.downcase, # %5$s
277
- progname, # %6$s
278
- html_escape( msg ).gsub(/\n/, '<br />') # %7$s
279
- ]
280
-
281
- return self.format % args
282
- end
283
-
284
- end # class HtmlFormatter
285
-
286
- end # module Inversion
287
-
@@ -1,74 +0,0 @@
1
- # -*- ruby -*-
2
- # vim: set nosta noet ts=4 sw=4:
3
- # encoding: utf-8
4
-
5
- BEGIN {
6
- require 'pathname'
7
- basedir = Pathname.new( __FILE__ ).dirname.parent.parent
8
- $LOAD_PATH.unshift( basedir ) unless $LOAD_PATH.include?( basedir )
9
- }
10
-
11
- require 'rspec'
12
- require 'stringio'
13
-
14
- require 'spec/lib/helpers'
15
-
16
- require 'inversion'
17
- require 'inversion/logging'
18
-
19
-
20
- #####################################################################
21
- ### C O N T E X T S
22
- #####################################################################
23
-
24
- describe Inversion::Logging, "-extended module" do
25
-
26
- before( :each ) do
27
- @extended_module = Module.new do
28
- extend Inversion::Logging
29
- end
30
- end
31
-
32
- it "should have a default Logger" do
33
- @extended_module.logger.should be_a( Logger )
34
- @extended_module.default_logger.should equal( @extended_module.logger )
35
- end
36
-
37
- it "should know if its default logger is replaced" do
38
- @extended_module.should be_using_default_logger
39
- @extended_module.logger = Logger.new( $stderr )
40
- @extended_module.should_not be_using_default_logger
41
- end
42
-
43
- it "has the default logger instance after being reset" do
44
- @extended_module.reset_logger
45
- @extended_module.logger.should equal( @extended_module.default_logger )
46
- end
47
-
48
- it "has the default log formatter instance after being reset" do
49
- @extended_module.reset_logger
50
- @extended_module.logger.formatter.should equal( @extended_module.default_log_formatter )
51
- end
52
-
53
-
54
- context "with new defaults" do
55
-
56
- before( :each ) do
57
- @sink = StringIO.new
58
- @logger = Logger.new( @sink )
59
- @formatter = Inversion::Logging::ColorFormatter.new( @logger )
60
-
61
- @extended_module.default_logger = @logger
62
- @extended_module.default_log_formatter = @formatter
63
- end
64
-
65
- it "uses the new defaults when the logging subsystem is reset" do
66
- @extended_module.reset_logger
67
- @extended_module.logger.should equal( @logger )
68
- end
69
-
70
- end
71
-
72
-
73
- end
74
-