ip_in_range 0.9 → 1.02

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.
data/lib/logging.rb DELETED
@@ -1,198 +0,0 @@
1
- #encoding: UTF-8
2
- =begin
3
- /***************************************************************************
4
- * ©2011-2019 Michael Uplawski <michael.uplawski@uplawski.eu> *
5
- * *
6
- * This program is free software; you can redistribute it and/or modify *
7
- * it under the terms of the GNU General Public License as published by *
8
- * the Free Software Foundation; either version 3 of the License, or *
9
- * (at your option) any later version. *
10
- * *
11
- * This program is distributed in the hope that it will be useful, *
12
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
- * GNU General Public License for more details. *
15
- * *
16
- * You should have received a copy of the GNU General Public License *
17
- * along with this program; if not, write to the *
18
- * Free Software Foundation, Inc., *
19
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20
- ***************************************************************************/
21
- =end
22
- require 'logger'
23
-
24
- =begin Creates a member @log and precede its output with the name of the class
25
- of the object.
26
- Example for a class-level logger:
27
- # --------------------
28
- class TClass
29
- self.extend(Logging)
30
- @@log = init_logger(STDOUT)
31
- def test_log
32
- @@log.info('class-level logger called from instance: ' << @@log.to_s)
33
- @log = @@log
34
- @log.info('AGAIN: class-level logger called from instance: ' << @log.to_s)
35
- end
36
- def self::test_log
37
- @log.info('class-level logger called from class: ' << @log.to_s)
38
- @@log.info('AGAIN: class-level logger called from class: ' << @@log.to_s)
39
- end
40
- end
41
- #---------------------
42
- Example for a object-level logger:
43
- ATTN! This means 1 logger per object.
44
- # --------------------
45
- class TClass
46
- include Logging
47
- def initialize
48
- init_logger(STDOUT, Logger::DEBUG)
49
- end
50
- def test_log
51
- @log.debug('called test_log() ')
52
- end
53
- end
54
- =end
55
- module Logging
56
-
57
- @@have_log = false
58
- @@LOG_CONF = File.dirname(File.absolute_path(__FILE__)) << File::Separator << 'log.conf'
59
-
60
- # Call this method in an instance-method (e.g. initialize() ) to define the
61
- # object-level logger; i.e. an object-specific member @log.
62
- # Call this method within the class-definition for a class-level logger; i.e.
63
- # a member @log for class-level acces.
64
- # The method returns the logger, so you can actually do what you want with it.
65
- def init_logger(target = STDOUT, level = Logger::INFO)
66
- # Prepare for a class-level logger. This is actually quite cool.
67
-
68
- # ---> Ingeniuous code starts here
69
- cn = (self.class == Class ? name : self.class.name)
70
- # <--- Ingeniuous code ends here
71
-
72
- # 11/2019
73
- if (cn == 'Object')
74
- cn = 'Top Level'
75
- end
76
-
77
- # allow to override the set log-levels with an
78
- # external configuration (log.conf).
79
- log_conf(cn)
80
- # Or use the defaults as set here or elsewhere...
81
-
82
- @level ||= level
83
- @target ||= target
84
-
85
- @log = Logger.new(@target)
86
- @log.level = @level
87
-
88
- @log.formatter = proc do |severity, datetime, progname, msg|
89
- t = Time.now
90
- "#{cn}: #{severity} #{t.hour}-#{t.min}-#{t.sec}: #{msg}\n"
91
- end
92
- if ! @@have_log
93
- @log.debug cn.dup << ' reading logging-configuration from ' << @@LOG_CONF
94
- @@have_log = true
95
- @log.debug('level is ' << level.to_s)
96
- end
97
- return @log
98
- end
99
-
100
- # Set the log-target to an IO object.
101
- def log_target=(target)
102
- @target = target
103
- @log = Logger.new(@@target)
104
- @log.level = @level
105
- end
106
-
107
- # set the log-level
108
- def log_level=(level)
109
- @level = level
110
- @log.level = @level
111
- end
112
-
113
- private
114
-
115
- # Override or set the log-level and target-device, as set in a file 'log.conf'.
116
- # I do not like the look of this, but it works just the way I want it to.
117
- # "HEAVANS! Isn't there a standard way to do this in Ruby, for Christ's sake?", you say.
118
- # Heck, I don't care. <= Read that again, I say.
119
- def log_conf(cn = nil)
120
- config = level = target = nil
121
- # puts 'log-config is in ' << @@LOG_CONF
122
- if(File::exist?(@@LOG_CONF) )
123
- begin
124
- conf = File.read(@@LOG_CONF)
125
- config = instance_eval(conf)
126
- rescue Exception => ex
127
- STDERR.puts "WARNING! Cannot evaluate the logger-configuration!" << ' ' << ex.message
128
- STDERR.puts "Default log-levels apply."
129
- end
130
- else
131
- puts "Default log-levels apply."
132
- end
133
-
134
- if(config && config.respond_to?(:to_hash) )
135
- config.default = nil
136
- if cn
137
- config = config[cn.to_sym]
138
- else
139
- config = config[self.class.name.to_sym]
140
- end
141
-
142
- if(config )
143
- if(config.respond_to?(:to_ary) && config.size == 2)
144
- @level, @target = config
145
- @target.downcase!
146
- logdir = File.dirname(@target)
147
- [:exist?, :directory?, :writable?].each do |m|
148
- msg = File.send(m, logdir)
149
- if(msg)
150
- STDERR.puts "WARNING! A logfile for '%s' cannot be written to %s (%s)!" %[self.class.name, logdir, msg]
151
- @target = nil
152
- end
153
- end
154
- else
155
- @level = config
156
- end
157
- end
158
- end
159
- end
160
- end
161
-
162
- ######### test
163
- if __FILE__ == $0
164
- class TClass
165
- # class level ---->
166
- self.extend(Logging)
167
- @@log = init_logger(STDOUT, Logger::INFO)
168
- # <------
169
- # object-level ---->
170
- include Logging
171
- # <---------
172
-
173
- def test_log
174
- @@log.info('class-level logger called from instance: ' << @@log.to_s)
175
- #@log = @@log # works too
176
- @log = TClass.class_eval{@log}
177
- @log.info('AGAIN: class-level logger called from instance: ' << @log.to_s)
178
- @log.debug("you won't see this on log-level INFO")
179
-
180
- # object-level ---->
181
- init_logger
182
- # <-----------
183
- @log.info("That's a different thing: " << @log.to_s << " - object-level logger!")
184
-
185
- end
186
- def self::test_log
187
- @log.info('class-level logger called from class: ' << @log.to_s)
188
- @@log.info('AGAIN: class-level logger called from class: ' << @log.to_s)
189
- end
190
- end
191
-
192
- TClass.new.test_log # class-logger + 1st object-logger
193
- TClass.new.test_log # same class-logger + 2nd object-logger
194
-
195
- TClass::test_log # same class-logger
196
- puts 'And just say it once clearly: THIS IS COOOL!!'
197
- end
198
- #EOF