analogger 0.5.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,100 @@
1
+ begin
2
+ load_attempted ||= false
3
+ require 'eventmachine'
4
+ rescue LoadError => e
5
+ unless load_attempted
6
+ load_attempted = true
7
+ require 'rubygems'
8
+ retry
9
+ end
10
+ raise e
11
+ end
12
+
13
+ module Swiftcore
14
+ module Analogger
15
+ class ClientProtocol < EventMachine::Connection
16
+ Cauthentication = 'authentication'.freeze
17
+ Ci = 'i'.freeze
18
+ attr_accessor :key, :host, :port, :msg_queue, :connected, :sender
19
+
20
+ def self.connect(service = 'default', host = '127.0.0.1', port = 6766, key = nil)
21
+ connection = ::EventMachine.connect(host, port.to_i, self) do |conn|
22
+ conn.connected = false
23
+ conn.msg_queue ||= ''
24
+ conn.service = service
25
+ conn.host = host
26
+ conn.port = port
27
+ conn.key = key
28
+ end
29
+ end
30
+
31
+ def connection_completed
32
+ @connected = true
33
+ pos = 0
34
+ log(Cauthentication,"#{@key}",true)
35
+ send_data @msg_queue
36
+ # @sender = EM::Timer.new(1) {send_data @msg_queue if @connected; @msg_queue = ''}
37
+ # while @msg_queue.length > pos
38
+ # msg = @msg_queue[pos]
39
+ # pos += 1
40
+ # break unless log(*msg)
41
+ # end
42
+ # if pos > 0
43
+ # @msg_queue.slice!(0..(pos - 1))
44
+ # end
45
+ end
46
+
47
+ def service
48
+ @service
49
+ end
50
+
51
+ def service=(val)
52
+ @service = val
53
+ @service_length = val.length
54
+ end
55
+
56
+ def close
57
+ close_connection_after_writing
58
+ end
59
+
60
+ def closed?
61
+ @connected
62
+ end
63
+
64
+ def unbind
65
+ @connected = false
66
+ #@sender.cancel
67
+ ::EventMachine.add_timer(rand(2)) {self.class.connect(@service, @host, @port, @key)}
68
+ end
69
+
70
+ def log(severity,msg,immediate=false)
71
+ len = [@service_length + severity.length + msg.length + 3].pack(Ci)
72
+ fullmsg = "#{len}#{len}:#{@service}:#{severity}:#{msg}"
73
+ if immediate
74
+ send_data fullmsg
75
+ elsif @connected
76
+ send_data fullmsg
77
+ else
78
+ @msg_queue << fullmsg
79
+ end
80
+ #if @connected
81
+ #send_data "#{len}#{len}:#{@service}:#{severity}:#{msg}"
82
+ #else
83
+ # @msg_queue << fullmsg
84
+ # false
85
+ #end
86
+ rescue Exception => e
87
+ puts e
88
+ @msg_queue << fullmsg if msg and severity
89
+ false
90
+ end
91
+
92
+ end
93
+
94
+ class Client
95
+ def self.new(*args)
96
+ ClientProtocol.connect(*args)
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,5 @@
1
+ module Swiftcore
2
+ class Analogger
3
+ VERSION = "0.9.1"
4
+ end
5
+ end
@@ -0,0 +1,316 @@
1
+ # Created by James Tucker <jftucker@gmail.com> on 2008-01-07.
2
+ # Code for the Logger interface taken from Logger itself now, instead of being generated.
3
+ require 'logger'
4
+
5
+ # = LoggerInterface.rb
6
+ #
7
+ # Simple logging utility wrapper.
8
+ #
9
+ # Author:: James Tucker <jftucker@gmail.com>, NAKAMURA, Hiroshi <nakahiro@sarion.co.jp> (logger)
10
+ # Documentation:: James Tucker, NAKAMURA, Hiroshi and Gavin Sinclair
11
+ # License::
12
+ # You can redistribute it and/or modify it under the same terms of Ruby's
13
+ # license; either the dual license version in 2003, or any later version.
14
+ #
15
+ # See LoggerInterface for documentation.
16
+ #
17
+
18
+ module Swiftcore
19
+ module Analogger
20
+ class Client
21
+
22
+
23
+ #
24
+ # == Description
25
+ #
26
+ # LoggerInterface provides a module which may be used to extend an Analogger
27
+ # Client interface, and provide a dual-mode interface, supporting both the
28
+ # analogger client api, and the logger api.
29
+ #
30
+ # === Description From logger.rb:
31
+ # The Logger class provides a simple but sophisticated logging utility that
32
+ # anyone can use because it's included in the Ruby 1.8.x standard library.
33
+ #
34
+ # The HOWTOs below give a code-based overview of Logger's usage, but the basic
35
+ # concept is as follows. You create a Logger object (output to a file or
36
+ # elsewhere), and use it to log messages. The messages will have varying
37
+ # levels (+info+, +error+, etc), reflecting their varying importance. The
38
+ # levels, and their meanings, are:
39
+ #
40
+ # +FATAL+:: an unhandleable error that results in a program crash
41
+ # +ERROR+:: a handleable error condition
42
+ # +WARN+:: a warning
43
+ # +INFO+:: generic (useful) information about system operation
44
+ # +DEBUG+:: low-level information for developers
45
+ #
46
+ # So each message has a level, and the Logger itself has a level, which acts
47
+ # as a filter, so you can control the amount of information emitted from the
48
+ # logger without having to remove actual messages.
49
+ #
50
+ # For instance, in a production system, you may have your logger(s) set to
51
+ # +INFO+ (or +WARN+ if you don't want the log files growing large with
52
+ # repetitive information). When you are developing it, though, you probably
53
+ # want to know about the program's internal state, and would set them to
54
+ # +DEBUG+.
55
+ #
56
+ # === Example
57
+ #
58
+ # A simple example demonstrates the above explanation:
59
+ #
60
+ # log = Swiftcore::Analogger::Client.new('logger_interface','127.0.0.1','47990')
61
+ # log.extend(Swiftcore::Analogger::Client::LoggerInterface)
62
+ # log.level = Logger::WARN
63
+ #
64
+ # log.debug("Created logger")
65
+ # log.info("Program started")
66
+ # log.warn("Nothing to do!")
67
+ #
68
+ # begin
69
+ # File.each_line(path) do |line|
70
+ # unless line =~ /^(\w+) = (.*)$/
71
+ # log.error("Line in wrong format: #{line}")
72
+ # end
73
+ # end
74
+ # rescue => err
75
+ # log.fatal("Caught exception; exiting")
76
+ # log.fatal(err)
77
+ # end
78
+ #
79
+ # Because the Logger's level is set to +WARN+, only the warning, error, and
80
+ # fatal messages are recorded. The debug and info messages are silently
81
+ # discarded.
82
+ #
83
+ # === How to log a message
84
+ #
85
+ # Notice the different methods (+fatal+, +error+, +info+) being used to log
86
+ # messages of various levels. Other methods in this family are +warn+ and
87
+ # +debug+. +add+ is used below to log a message of an arbitrary (perhaps
88
+ # dynamic) level.
89
+ #
90
+ # 1. Message in block.
91
+ #
92
+ # logger.fatal { "Argument 'foo' not given." }
93
+ #
94
+ # 2. Message as a string.
95
+ #
96
+ # logger.error "Argument #{ @foo } mismatch."
97
+ #
98
+ # 3. With progname.
99
+ #
100
+ # logger.info('initialize') { "Initializing..." }
101
+ #
102
+ # 4. With severity.
103
+ #
104
+ # logger.add(Logger::FATAL) { 'Fatal error!' }
105
+ #
106
+ # === Setting severity threshold
107
+ #
108
+ # 1. Original interface.
109
+ #
110
+ # logger.sev_threshold = Logger::WARN
111
+ #
112
+ # 2. Log4r (somewhat) compatible interface.
113
+ #
114
+ # logger.level = Logger::INFO
115
+ #
116
+ # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
117
+ #
118
+ #
119
+ module LoggerInterface
120
+ include Logger::Severity
121
+ MapUnknownTo = 'info'
122
+
123
+ # A severity is the worded name for a log level
124
+ # A level is the numerical value given to a severity
125
+ SeverityToLevel = Hash.new
126
+ LevelToSeverity = Hash.new
127
+
128
+ Logger::Severity.constants.each do |const|
129
+ # N.B. All severities mapped to lower case!
130
+ severity = const.downcase
131
+ level = Logger::Severity.const_get(const)
132
+ SeverityToLevel[severity] = level
133
+ LevelToSeverity[level] = severity
134
+ end
135
+
136
+ SeverityToLevel.default = SeverityToLevel[MapUnknownTo]
137
+ LevelToSeverity.default = MapUnknownTo
138
+
139
+
140
+ def self.extend_object(log_client)
141
+
142
+ class <<log_client
143
+ include ::Swiftcore::Analogger::Client::LoggerInterface
144
+ alias analog log
145
+
146
+ # The interface supports string names, symbol names and levels as the first
147
+ # argument. It therefrore covers both the standard analogger api, and the
148
+ # logger api, and some other string based log level api.
149
+ # N.B. This adds one main limitation - all levels are commonly downcased
150
+ # by this interface.
151
+ def add(severity, message = nil, progname = nil, &block)
152
+ level = severity
153
+
154
+ case severity
155
+ when Numeric
156
+ severity = LevelToSeverity[level]
157
+ when Symbol
158
+ severity = severity.to_s.downcase
159
+ level = SeverityToLevel[severity]
160
+ when String
161
+ severity = severity.to_s.downcase
162
+ level = SeverityToLevel[severity]
163
+ else
164
+ raise ArgumentError.new('#add accepts either Numeric, Symbol or String')
165
+ end
166
+ return true unless @level <= level
167
+
168
+ # We map severity unknown to info by default. MapUnknownTo.replace('mylevel')
169
+ # to change that.
170
+ severity = MapUnknownTo if severity == 'unknown'
171
+
172
+ progname ||= @service
173
+ if message.nil?
174
+ if block_given?
175
+ message = yield
176
+ else
177
+ message = progname
178
+ progname = @service
179
+ end
180
+ end
181
+
182
+ analog( severity, message )
183
+ true
184
+ end
185
+ alias log add
186
+
187
+ end
188
+
189
+ # Default log level for logger is 0, maybe a good idea to fetch from logger itself.
190
+ log_client.level ||= 0
191
+ log_client
192
+ end
193
+
194
+ # As there is no notion of a raw message for an analogger client, this sends messages
195
+ # at the default log level (unknown, which is mapped to MapUnknownTo).
196
+ def <<(raw)
197
+ add(nil, raw)
198
+ end
199
+
200
+ def progname=(name)
201
+ @service = name
202
+ end
203
+
204
+ def progname
205
+ @service
206
+ end
207
+
208
+ #
209
+ # The following code has been taken from logger.rb in the standard ruby distribution
210
+ # Author:: NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
211
+ # Documentation:: NAKAMURA, Hiroshi and Gavin Sinclair
212
+ # License::
213
+ # You can redistribute it and/or modify it under the same terms of Ruby's
214
+ # license; either the dual license version in 2003, or any later version.
215
+ #
216
+
217
+ # Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
218
+ attr_accessor :level
219
+ alias sev_threshold level
220
+ alias sev_threshold= level=
221
+
222
+ # Returns +true+ iff the current severity level allows for the printing of
223
+ # +DEBUG+ messages.
224
+ def debug?; @level <= DEBUG; end
225
+
226
+ # Returns +true+ iff the current severity level allows for the printing of
227
+ # +INFO+ messages.
228
+ def info?; @level <= INFO; end
229
+
230
+ # Returns +true+ iff the current severity level allows for the printing of
231
+ # +WARN+ messages.
232
+ def warn?; @level <= WARN; end
233
+
234
+ # Returns +true+ iff the current severity level allows for the printing of
235
+ # +ERROR+ messages.
236
+ def error?; @level <= ERROR; end
237
+
238
+ # Returns +true+ iff the current severity level allows for the printing of
239
+ # +FATAL+ messages.
240
+ def fatal?; @level <= FATAL; end
241
+
242
+ #
243
+ # Log a +DEBUG+ message.
244
+ #
245
+ # See #info for more information.
246
+ #
247
+ def debug(message = nil, &block)
248
+ add(DEBUG, message, nil, &block)
249
+ end
250
+
251
+ #
252
+ # Log an +INFO+ message.
253
+ #
254
+ # The message can come either from the +progname+ argument or the +block+. If
255
+ # both are provided, then the +block+ is used as the message, and +progname+
256
+ # is used as the program name.
257
+ #
258
+ # === Examples
259
+ #
260
+ # logger.info("MainApp") { "Received connection from #{ip}" }
261
+ # # ...
262
+ # logger.info "Waiting for input from user"
263
+ # # ...
264
+ # logger.info { "User typed #{input}" }
265
+ #
266
+ # You'll probably stick to the second form above, unless you want to provide a
267
+ # program name (which you can do with <tt>Logger#progname=</tt> as well).
268
+ #
269
+ # === Return
270
+ #
271
+ # See #add.
272
+ #
273
+ def info(message = nil, &block)
274
+ add(INFO, message, nil, &block)
275
+ end
276
+
277
+ #
278
+ # Log a +WARN+ message.
279
+ #
280
+ # See #info for more information.
281
+ #
282
+ def warn(message = nil, &block)
283
+ add(WARN, message, nil, &block)
284
+ end
285
+
286
+ #
287
+ # Log an +ERROR+ message.
288
+ #
289
+ # See #info for more information.
290
+ #
291
+ def error(message = nil, &block)
292
+ add(ERROR, message, nil, &block)
293
+ end
294
+
295
+ #
296
+ # Log a +FATAL+ message.
297
+ #
298
+ # See #info for more information.
299
+ #
300
+ def fatal(message = nil, &block)
301
+ add(FATAL, message, nil, &block)
302
+ end
303
+
304
+ #
305
+ # Log an +UNKNOWN+ message. This will be printed no matter what the logger
306
+ # level.
307
+ #
308
+ # See #info for more information.
309
+ #
310
+ def unknown(message = nil, &block)
311
+ add(UNKNOWN, message, nil, &block)
312
+ end
313
+ end
314
+ end
315
+ end
316
+ end
data/setup.rb CHANGED
@@ -6,21 +6,22 @@ $:.push(basedir)
6
6
  require 'external/package'
7
7
  require 'rbconfig'
8
8
  begin
9
- require 'rubygems'
9
+ require 'rubygems'
10
10
  rescue LoadError
11
11
  end
12
12
 
13
13
  Dir.chdir(basedir)
14
14
  Package.setup("1.0") {
15
- name "Swiftcore Analogger"
15
+ name "Swiftcore Analogger"
16
16
 
17
- translate(:lib, 'src/' => '')
18
- translate(:bin, 'bin/' => '')
19
- lib(*Dir["src/swiftcore/**/*.rb"])
20
- ri(*Dir["src/swiftcore/**/*.rb"])
21
- bin "bin/analogger"
17
+ translate(:lib, 'src/' => '')
18
+ translate(:bin, 'bin/' => '')
19
+ lib(*Dir["src/swiftcore/**/*.rb"])
20
+ ri(*Dir["src/swiftcore/**/*.rb"])
21
+ bin "bin/analogger"
22
22
 
23
- unit_test "test/TC_Analogger.rb"
24
-
25
- true
23
+ unit_test "test/TC_Analogger.rb"
24
+ #unit_test "test/TC_Analogger2.rb"
25
+
26
+ true
26
27
  }