configurability 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,137 @@
1
+ #!/usr/bin/ruby
2
+ # coding: utf-8
3
+
4
+ BEGIN {
5
+ require 'pathname'
6
+ basedir = Pathname.new( __FILE__ ).dirname.parent
7
+
8
+ libdir = basedir + "lib"
9
+
10
+ $LOAD_PATH.unshift( libdir.to_s ) unless $LOAD_PATH.include?( libdir.to_s )
11
+ }
12
+
13
+ require 'logger'
14
+ require 'erb'
15
+ require 'yaml'
16
+
17
+ require 'configurability'
18
+
19
+
20
+ # An alternate formatter for Logger instances that outputs +div+ HTML
21
+ # fragments.
22
+ class HtmlLogFormatter < Logger::Formatter
23
+ include ERB::Util # for html_escape()
24
+
25
+ # The default HTML fragment that'll be used as the template for each log message.
26
+ HTML_LOG_FORMAT = %q{
27
+ <div class="log-message %5$s">
28
+ <span class="log-time">%1$s.%2$06d</span>
29
+ [
30
+ <span class="log-pid">%3$d</span>
31
+ /
32
+ <span class="log-tid">%4$s</span>
33
+ ]
34
+ <span class="log-level">%5$s</span>
35
+ :
36
+ <span class="log-name">%6$s</span>
37
+ <span class="log-message-text">%7$s</span>
38
+ </div>
39
+ }
40
+
41
+ ### Override the logging formats with ones that generate HTML fragments
42
+ def initialize( logger, format=HTML_LOG_FORMAT ) # :notnew:
43
+ @logger = logger
44
+ @format = format
45
+ super()
46
+ end
47
+
48
+
49
+ ######
50
+ public
51
+ ######
52
+
53
+ # The HTML fragment that will be used as a format() string for the log
54
+ attr_accessor :format
55
+
56
+
57
+ ### Return a log message composed out of the arguments formatted using the
58
+ ### formatter's format string
59
+ def call( severity, time, progname, msg )
60
+ args = [
61
+ time.strftime( '%Y-%m-%d %H:%M:%S' ), # %1$s
62
+ time.usec, # %2$d
63
+ Process.pid, # %3$d
64
+ Thread.current == Thread.main ? 'main' : Thread.object_id, # %4$s
65
+ severity.downcase, # %5$s
66
+ progname, # %6$s
67
+ html_escape( msg ).gsub(/\n/, '<br />') # %7$s
68
+ ]
69
+
70
+ return self.format % args
71
+ end
72
+
73
+ end # class HtmlLogFormatter
74
+
75
+ ### RSpec helper functions.
76
+ module Configurability::SpecHelpers
77
+
78
+ LEVEL = {
79
+ :debug => Logger::DEBUG,
80
+ :info => Logger::INFO,
81
+ :warn => Logger::WARN,
82
+ :error => Logger::ERROR,
83
+ :fatal => Logger::FATAL,
84
+ }
85
+
86
+ class ArrayLogger
87
+ ### Create a new ArrayLogger that will append content to +array+.
88
+ def initialize( array )
89
+ @array = array
90
+ end
91
+
92
+ ### Write the specified +message+ to the array.
93
+ def write( message )
94
+ @array << message
95
+ end
96
+
97
+ ### No-op -- this is here just so Logger doesn't complain
98
+ def close; end
99
+
100
+ end # class ArrayLogger
101
+
102
+
103
+ ###############
104
+ module_function
105
+ ###############
106
+
107
+ ### Reset the logging subsystem to its default state.
108
+ def reset_logging
109
+ Configurability.reset_logger
110
+ end
111
+
112
+
113
+ ### Alter the output of the default log formatter to be pretty in SpecMate output
114
+ def setup_logging( level=Logger::FATAL )
115
+
116
+ # Turn symbol-style level config into Logger's expected Fixnum level
117
+ level = LEVEL[ level ] if LEVEL.key?( level )
118
+
119
+ # Only do this when executing from a spec in TextMate
120
+ if ENV['HTML_LOGGING'] || (ENV['TM_FILENAME'] && ENV['TM_FILENAME'] =~ /_spec\.rb/)
121
+ Thread.current['logger-output'] = []
122
+ logdevice = ArrayLogger.new( Thread.current['logger-output'] )
123
+ Configurability.logger = Logger.new( logdevice )
124
+ Configurability.logger.level = level
125
+ Configurability.logger.formatter = HtmlLogFormatter.new( Configurability.logger )
126
+ else
127
+ logger = Logger.new( $stderr )
128
+ Configurability.logger = logger
129
+ Configurability.logger.level = level
130
+ end
131
+ end
132
+
133
+ end
134
+
135
+
136
+ # vim: set nosta noet ts=4 sw=4:
137
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configurability
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Michael Granger
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-07-12 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Configurability is mixin that allows you to add configurability to one or more classes, assign them each a section of the configuration, and then when the configuration is loaded, the class is given the section it requested.
22
+ email:
23
+ - ged@FaerieMUD.org
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - ChangeLog
30
+ - README.md
31
+ - LICENSE
32
+ files:
33
+ - Rakefile
34
+ - ChangeLog
35
+ - README.md
36
+ - LICENSE
37
+ - spec/configurability_spec.rb
38
+ - spec/lib/helpers.rb
39
+ - lib/configurability/logformatter.rb
40
+ - lib/configurability.rb
41
+ - rake/191_compat.rb
42
+ - rake/dependencies.rb
43
+ - rake/documentation.rb
44
+ - rake/helpers.rb
45
+ - rake/hg.rb
46
+ - rake/manual.rb
47
+ - rake/packaging.rb
48
+ - rake/publishing.rb
49
+ - rake/style.rb
50
+ - rake/svn.rb
51
+ - rake/testing.rb
52
+ - rake/verifytask.rb
53
+ has_rdoc: true
54
+ homepage: http://bitbucket.org/ged/configurability
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --tab-width=4
60
+ - --show-hash
61
+ - --include
62
+ - .
63
+ - --main=README.md
64
+ - --title=configurability
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A configurability mixin for Ruby
88
+ test_files:
89
+ - spec/configurability_spec.rb
90
+ - spec/lib/helpers.rb