yore 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1,2 +1,2 @@
1
- 0.3.0
1
+ 0.3.1
2
2
 
@@ -0,0 +1,210 @@
1
+ # Hacked together from Rails 2.3.8
2
+
3
+ # Extends the class object with class and instance accessors for class attributes,
4
+ # just like the native attr* accessors for instance attributes.
5
+ #
6
+ # class Person
7
+ # cattr_accessor :hair_colors
8
+ # end
9
+ #
10
+ # Person.hair_colors = [:brown, :black, :blonde, :red]
11
+ class Class
12
+ def cattr_reader(*syms)
13
+ options = syms.extract_options!
14
+ syms.each do |sym|
15
+ next if sym.is_a?(Hash)
16
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
17
+ unless defined? @@#{sym}
18
+ @@#{sym} = nil
19
+ end
20
+
21
+ def self.#{sym}
22
+ @@#{sym}
23
+ end
24
+ EOS
25
+
26
+ unless options[:instance_reader] == false
27
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
28
+ def #{sym}
29
+ @@#{sym}
30
+ end
31
+ EOS
32
+ end
33
+ end
34
+ end
35
+
36
+ def cattr_writer(*syms)
37
+ options = syms.extract_options!
38
+ syms.each do |sym|
39
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
40
+ unless defined? @@#{sym}
41
+ @@#{sym} = nil
42
+ end
43
+
44
+ def self.#{sym}=(obj)
45
+ @@#{sym} = obj
46
+ end
47
+ EOS
48
+
49
+ unless options[:instance_writer] == false
50
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
51
+ def #{sym}=(obj)
52
+ @@#{sym} = obj
53
+ end
54
+ EOS
55
+ end
56
+ end
57
+ end
58
+
59
+ def cattr_accessor(*syms)
60
+ cattr_reader(*syms)
61
+ cattr_writer(*syms)
62
+ end
63
+ end
64
+
65
+ Array.class_eval do
66
+ #module Array #:nodoc:
67
+ # module ExtractOptions
68
+ # Extracts options from a set of arguments. Removes and returns the last
69
+ # element in the array if it's a hash, otherwise returns a blank hash.
70
+ #
71
+ # def options(*args)
72
+ # args.extract_options!
73
+ # end
74
+ #
75
+ # options(1, 2) # => {}
76
+ # options(1, 2, :a => :b) # => {:a=>:b}
77
+ def extract_options!
78
+ last.is_a?(::Hash) ? pop : {}
79
+ end
80
+ end
81
+ #end
82
+
83
+ #module ActiveSupport
84
+ # Inspired by the buffered logger idea by Ezra
85
+ class BufferedLogger
86
+ module Severity
87
+ DEBUG = 0
88
+ INFO = 1
89
+ WARN = 2
90
+ ERROR = 3
91
+ FATAL = 4
92
+ UNKNOWN = 5
93
+ end
94
+ include Severity
95
+
96
+ MAX_BUFFER_SIZE = 1000
97
+
98
+ ##
99
+ # :singleton-method:
100
+ # Set to false to disable the silencer
101
+ cattr_accessor :silencer
102
+ self.silencer = true
103
+
104
+ # Silences the logger for the duration of the block.
105
+ def silence(temporary_level = ERROR)
106
+ if silencer
107
+ begin
108
+ old_logger_level, self.level = level, temporary_level
109
+ yield self
110
+ ensure
111
+ self.level = old_logger_level
112
+ end
113
+ else
114
+ yield self
115
+ end
116
+ end
117
+
118
+ attr_accessor :level
119
+ attr_reader :auto_flushing
120
+
121
+ def initialize(log, level = DEBUG)
122
+ @level = level
123
+ @buffer = {}
124
+ @auto_flushing = 1
125
+ @guard = Mutex.new
126
+
127
+ if log.respond_to?(:write)
128
+ @log = log
129
+ elsif File.exist?(log)
130
+ @log = open(log, (File::WRONLY | File::APPEND))
131
+ @log.sync = true
132
+ else
133
+ FileUtils.mkdir_p(File.dirname(log))
134
+ @log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
135
+ @log.sync = true
136
+ @log.write("# Logfile created on %s" % [Time.now.to_s])
137
+ end
138
+ end
139
+
140
+ def add(severity, message = nil, progname = nil, &block)
141
+ return if @level > severity
142
+ message = (message || (block && block.call) || progname).to_s
143
+ # If a newline is necessary then create a new message ending with a newline.
144
+ # Ensures that the original message is not mutated.
145
+ message = "#{message}\n" unless message[-1] == ?\n
146
+ buffer << message
147
+ auto_flush
148
+ message
149
+ end
150
+
151
+ for severity in Severity.constants
152
+ class_eval <<-EOT, __FILE__, __LINE__ + 1
153
+ def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
154
+ add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
155
+ end # end
156
+ #
157
+ def #{severity.downcase}? # def debug?
158
+ #{severity} >= @level # DEBUG >= @level
159
+ end # end
160
+ EOT
161
+ end
162
+
163
+ # Set the auto-flush period. Set to true to flush after every log message,
164
+ # to an integer to flush every N messages, or to false, nil, or zero to
165
+ # never auto-flush. If you turn auto-flushing off, be sure to regularly
166
+ # flush the log yourself -- it will eat up memory until you do.
167
+ def auto_flushing=(period)
168
+ @auto_flushing =
169
+ case period
170
+ when true; 1
171
+ when false, nil, 0; MAX_BUFFER_SIZE
172
+ when Integer; period
173
+ else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
174
+ end
175
+ end
176
+
177
+ def flush
178
+ @guard.synchronize do
179
+ unless buffer.empty?
180
+ old_buffer = buffer
181
+ @log.write(old_buffer.join)
182
+ end
183
+
184
+ # Important to do this even if buffer was empty or else @buffer will
185
+ # accumulate empty arrays for each request where nothing was logged.
186
+ clear_buffer
187
+ end
188
+ end
189
+
190
+ def close
191
+ flush
192
+ @log.close if @log.respond_to?(:close)
193
+ @log = nil
194
+ end
195
+
196
+ protected
197
+ def auto_flush
198
+ flush if buffer.size >= @auto_flushing
199
+ end
200
+
201
+ def buffer
202
+ @buffer[Thread.current] ||= []
203
+ end
204
+
205
+ def clear_buffer
206
+ @buffer.delete(Thread.current)
207
+ end
208
+ end
209
+ #end
210
+
@@ -6,6 +6,7 @@ require 'fileutils'
6
6
  require 'net/smtp'
7
7
 
8
8
  require 'yore/AWSS3Client'
9
+ require 'yore/BufferedLogger'
9
10
  require 'yaml'
10
11
 
11
12
 
@@ -55,9 +56,10 @@ module YoreCore
55
56
  cons.level = Logger::Severity.const_get(config[:log_level]) rescue Logger::Severity::INFO
56
57
 
57
58
  report_file = MiscUtils::temp_file
58
- @reporter = Logger.new(report_file)
59
- @reporter.formatter = ConsoleLogger::ReportFormatter.new
59
+ @reporter = BufferedLogger.new(report_file)
60
+ #@reporter.formatter = ConsoleLogger::ReportFormatter.new
60
61
  @reporter.level = cons.level
62
+ @reporter.auto_flushing = 1
61
63
 
62
64
  @logger = MultiLogger.new([cons,@reporter])
63
65
  @logger.info "Yore file and database backup tool for Amazon S3 "
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{yore}
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["buzzware"]
12
- s.date = %q{2011-11-07}
12
+ s.date = %q{2011-11-30}
13
13
  s.default_executable = %q{yore}
14
14
  s.description = %q{yore (as in "days of yore") is a user data management utility for web applications.}
15
15
  s.email = %q{contact@buzzware.com.au}
@@ -27,6 +27,7 @@ Gem::Specification.new do |s|
27
27
  "VERSION",
28
28
  "bin/yore",
29
29
  "lib/yore/AWSS3Client.rb",
30
+ "lib/yore/BufferedLogger.rb",
30
31
  "lib/yore/yore_core.rb",
31
32
  "notes.txt",
32
33
  "test.crontab",
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yore
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - buzzware
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-07 00:00:00 +08:00
18
+ date: 2011-11-30 00:00:00 +08:00
19
19
  default_executable: yore
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -114,6 +114,7 @@ files:
114
114
  - VERSION
115
115
  - bin/yore
116
116
  - lib/yore/AWSS3Client.rb
117
+ - lib/yore/BufferedLogger.rb
117
118
  - lib/yore/yore_core.rb
118
119
  - notes.txt
119
120
  - test.crontab