ip_in_range 0.9 → 1.0
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.
- checksums.yaml +4 -4
- data/README.md +80 -0
- data/bin/ip_in_range +30 -33
- data/doc/license.txt +9 -670
- data/ip_in_range.gemspec +5 -7
- data/lib/basic_logging.rb +170 -0
- data/lib/email.rb +11 -19
- data/lib/file_checking.rb +4 -10
- data/lib/ip_range.rb +9 -16
- data/lib/version.rb +19 -2
- metadata +9 -9
- data/lib/log.conf +0 -56
- data/lib/logging.rb +0 -198
data/ip_in_range.gemspec
CHANGED
@@ -7,13 +7,11 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.date = Date.today.strftime('%F')
|
8
8
|
s.summary = SUMMARY
|
9
9
|
s.description = "Verify that an IP is in a range"
|
10
|
-
s.authors = AUTHORS
|
11
|
-
s.email =
|
12
|
-
s.files = %w~ip_in_range~.collect{|f| 'bin/' << f} + %w~
|
10
|
+
s.authors = AUTHORS.join ','
|
11
|
+
s.email = AUTHORS_MAIL.join ','
|
12
|
+
s.files = %w~ip_in_range~.collect{|f| 'bin/' << f} + %w~ip_range.rb version.rb file_checking.rb basic_logging.rb email.rb~.collect{|f| 'lib/' << f} + ['doc/license.txt','ip_in_range.gemspec'] + ["README.md"]
|
13
13
|
s.homepage = ''
|
14
|
-
#s.requirements = ''
|
15
|
-
#s.add_runtime_dependency ''
|
16
14
|
s.executables = 'ip_in_range'
|
17
|
-
s.license = '
|
18
|
-
s.required_ruby_version = '>=
|
15
|
+
s.license = 'Nonstandard'
|
16
|
+
s.required_ruby_version = '>= 3.0'
|
19
17
|
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
#!/bin/env ruby
|
2
|
+
#encoding: UTF-8
|
3
|
+
=begin
|
4
|
+
/***************************************************************************
|
5
|
+
* ©2013-2023, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
6
|
+
* *
|
7
|
+
* This program is free software; you can redistribute it and/or modify *
|
8
|
+
* it under the terms of the WTFPL 2.0 or later, see *
|
9
|
+
* http://www.wtfpl.net/about/ *
|
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. *
|
14
|
+
* *
|
15
|
+
***************************************************************************/
|
16
|
+
=end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Simplified logging.
|
20
|
+
# See example code at the bottom of this file.
|
21
|
+
# Execute this file to see the output.
|
22
|
+
#
|
23
|
+
|
24
|
+
#require 'time'
|
25
|
+
|
26
|
+
module BasicLogging
|
27
|
+
|
28
|
+
DEBUG = 0
|
29
|
+
INFO = 1
|
30
|
+
WARN = 2
|
31
|
+
ERROR = 3
|
32
|
+
FATAL = 4
|
33
|
+
UNKNOWN = nil
|
34
|
+
|
35
|
+
# this is mainly for the translation of method calls into log levels
|
36
|
+
Levels = {:debug => DEBUG, :info => INFO, :warn => WARN, :error => ERROR,
|
37
|
+
:fatal => FATAL, :unknown => UNKNOWN}
|
38
|
+
|
39
|
+
@@log_level = UNKNOWN
|
40
|
+
@@target = STDOUT
|
41
|
+
@@muted = []
|
42
|
+
|
43
|
+
# do not log, if caller is obj (class or instance)
|
44
|
+
def self.mute(obj)
|
45
|
+
name = obj.class == Class ? obj.name.dup : obj.class.name
|
46
|
+
@@muted << name
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.is_muted?(obj)
|
50
|
+
name = obj.class == Class ? obj.name.dup : obj.class.name
|
51
|
+
@@muted.include?(name)
|
52
|
+
end
|
53
|
+
|
54
|
+
# set the log level
|
55
|
+
def set_level(lv)
|
56
|
+
if lv.respond_to?(:to_str)
|
57
|
+
lv = Levels[lv.to_sym]
|
58
|
+
end
|
59
|
+
if(!lv || (lv.respond_to?(:to_int) && lv >= DEBUG && lv <= FATAL) )
|
60
|
+
@@log_level = lv
|
61
|
+
else
|
62
|
+
STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << lv.to_s << "\""
|
63
|
+
STDERR.puts "Keepinng old log level " << Levels.keys.detect {| k| Levels[k] == @@log_level}.to_s
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# set the log target
|
68
|
+
def set_target(tg)
|
69
|
+
if tg.respond_to?(:to_io)
|
70
|
+
@@target = tg
|
71
|
+
elsif(!File::exist?(tg) || ( File.file?(tg) && File.writable?(tg) ) )
|
72
|
+
@@target = File.open(tg, 'w+')
|
73
|
+
else
|
74
|
+
STDERR.puts __FILE__.dup << ': ERROR : target ' << tg << ' cannot be set'
|
75
|
+
STDERR.puts "Keeping old target " << @@target.inspect
|
76
|
+
return
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Output of log messages, depending on the log level set for the calling class
|
81
|
+
# and the name of the alias method which is actually called.
|
82
|
+
def log(message)
|
83
|
+
if !BasicLogging.is_muted?(self)
|
84
|
+
# how has this method been called?
|
85
|
+
mlevel = __callee__
|
86
|
+
if Levels.has_key?(mlevel) && Levels[mlevel] <= FATAL
|
87
|
+
# output only for levels equal or above the value that corresponds to
|
88
|
+
# the calling alias.
|
89
|
+
format_log( message, mlevel) if @@log_level && Levels[mlevel] >= @@log_level
|
90
|
+
else
|
91
|
+
STDERR.puts __FILE__.dup << ": ERROR : invalid log level \"" << mlevel.to_s << "\""
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
alias :debug :log
|
97
|
+
alias :info :log
|
98
|
+
alias :warn :log
|
99
|
+
alias :error :log
|
100
|
+
alias :fatal :log
|
101
|
+
|
102
|
+
attr_reader :target, :log_level
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
# 1 format_log for all loggers.
|
107
|
+
def format_log(message, mlevel)
|
108
|
+
# indicate if a registered class or the registered object of a class is calling.
|
109
|
+
name = self.class == Class ? self.name.dup << ' [class]' : self.class.name
|
110
|
+
@@target.puts '' << name << ' ' << mlevel.to_s << ' ' << Time.now.strftime("%H:%M:%S:%6N") << ': ' << message
|
111
|
+
end
|
112
|
+
end
|
113
|
+
#---------test: execute file----------
|
114
|
+
if $0 == __FILE__
|
115
|
+
Array.extend(BasicLogging)
|
116
|
+
Array.set_level(BasicLogging::INFO)
|
117
|
+
Array.info('TEST')
|
118
|
+
ar = Array.new
|
119
|
+
ar.extend(BasicLogging)
|
120
|
+
# --- no output :
|
121
|
+
l = __LINE__
|
122
|
+
ar.debug(l.next.to_s << ': debug-test 0')
|
123
|
+
# output
|
124
|
+
ar.set_level(BasicLogging::DEBUG)
|
125
|
+
l = __LINE__
|
126
|
+
ar.debug(l.next.to_s << ': debug-test 1')
|
127
|
+
|
128
|
+
obj = Object.new
|
129
|
+
obj.extend(BasicLogging)
|
130
|
+
obj.set_level(BasicLogging::DEBUG)
|
131
|
+
puts "--------debug-----------"
|
132
|
+
obj.debug('debug')
|
133
|
+
obj.info('info')
|
134
|
+
obj.warn('warn')
|
135
|
+
obj.error('error')
|
136
|
+
obj.fatal('fatal')
|
137
|
+
puts "--------info-----------"
|
138
|
+
obj.set_level("info")
|
139
|
+
obj.debug('debug')
|
140
|
+
obj.info('info')
|
141
|
+
obj.warn('warn')
|
142
|
+
obj.error('error')
|
143
|
+
obj.fatal('fatal')
|
144
|
+
puts "--------fatal-----------"
|
145
|
+
obj.set_level("fatal")
|
146
|
+
obj.debug('debug')
|
147
|
+
obj.info('info')
|
148
|
+
obj.warn('warn')
|
149
|
+
obj.error('error')
|
150
|
+
obj.fatal('fatal')
|
151
|
+
puts "--------UNKNOWN-----------"
|
152
|
+
obj.set_level(nil)
|
153
|
+
obj.debug('debug')
|
154
|
+
obj.info('info')
|
155
|
+
obj.warn('warn')
|
156
|
+
obj.error('error')
|
157
|
+
obj.fatal('fatal')
|
158
|
+
puts " ------ Output into file ----"
|
159
|
+
obj.set_target "/tmp/test_log.log"
|
160
|
+
puts " ------ INFO -----------"
|
161
|
+
obj.set_level BasicLogging::INFO
|
162
|
+
obj.info('info output')
|
163
|
+
|
164
|
+
obj.info('info output 2')
|
165
|
+
puts "---------- invalid -------"
|
166
|
+
obj.set_target "/dev/sr0"
|
167
|
+
obj.set_level "power"
|
168
|
+
end
|
169
|
+
|
170
|
+
# EOF
|
data/lib/email.rb
CHANGED
@@ -2,40 +2,32 @@
|
|
2
2
|
#encoding: UTF-8
|
3
3
|
=begin
|
4
4
|
/***************************************************************************
|
5
|
-
* ©2021-
|
5
|
+
* ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
6
6
|
* *
|
7
7
|
* This program is free software; you can redistribute it and/or modify *
|
8
|
-
* it under the terms of the
|
9
|
-
*
|
10
|
-
* (at your option) any later version. *
|
8
|
+
* it under the terms of the WTFPL 2.0 or later, see *
|
9
|
+
* http://www.wtfpl.net/about/ *
|
11
10
|
* *
|
12
11
|
* This program is distributed in the hope that it will be useful, *
|
13
12
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
14
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
15
|
-
* GNU General Public License for more details. *
|
13
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
16
14
|
* *
|
17
|
-
* You should have received a copy of the GNU General Public License *
|
18
|
-
* along with this program; if not, write to the *
|
19
|
-
* Free Software Foundation, Inc., *
|
20
|
-
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
21
15
|
***************************************************************************/
|
22
16
|
=end
|
23
17
|
|
24
|
-
require_relative '
|
18
|
+
require_relative 'basic_logging'
|
25
19
|
require 'mail'
|
26
20
|
|
27
21
|
class FMail
|
28
|
-
|
29
|
-
@@log = init_logger(STDOUT)
|
22
|
+
include BasicLogging
|
30
23
|
|
31
24
|
def initialize(mail_text)
|
32
|
-
@log = @@log
|
33
25
|
if !mail_text || mail_text.empty?
|
34
|
-
|
26
|
+
error('No mail-text provided. Aborting!')
|
35
27
|
exit false
|
36
28
|
end
|
37
29
|
@mail_text = mail_text
|
38
|
-
#
|
30
|
+
# debug('mail text is ' << @mail_text)
|
39
31
|
extr_received
|
40
32
|
end
|
41
33
|
|
@@ -47,13 +39,13 @@ class FMail
|
|
47
39
|
begin
|
48
40
|
@mail = Mail::read_from_string(@mail_text)
|
49
41
|
rescue Exception => ex
|
50
|
-
|
51
|
-
|
42
|
+
error("cannot analyze this mail: " << ex.message)
|
43
|
+
error("Aborting!")
|
52
44
|
exit false;
|
53
45
|
end
|
54
46
|
headers = @mail.header_fields
|
55
47
|
@received = headers.filter{|h| h.name == 'Received'}.collect{|h| h.unparsed_value}
|
56
|
-
|
48
|
+
debug("found received-headers: " << @received.to_s)
|
57
49
|
end
|
58
50
|
|
59
51
|
end
|
data/lib/file_checking.rb
CHANGED
@@ -1,22 +1,16 @@
|
|
1
1
|
#encoding: UTF-8
|
2
2
|
=begin
|
3
3
|
/***************************************************************************
|
4
|
-
* ©
|
4
|
+
* ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
5
5
|
* *
|
6
6
|
* This program is free software; you can redistribute it and/or modify *
|
7
|
-
* it under the terms of the
|
8
|
-
*
|
9
|
-
* (at your option) any later version. *
|
7
|
+
* it under the terms of the WTFPL 2.0 or later, see *
|
8
|
+
* http://www.wtfpl.net/about/ *
|
10
9
|
* *
|
11
10
|
* This program is distributed in the hope that it will be useful, *
|
12
11
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
13
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
14
|
-
* GNU General Public License for more details. *
|
12
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
15
13
|
* *
|
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
14
|
***************************************************************************/
|
21
15
|
=end
|
22
16
|
|
data/lib/ip_range.rb
CHANGED
@@ -2,29 +2,23 @@
|
|
2
2
|
#encoding: UTF-8
|
3
3
|
=begin
|
4
4
|
/***************************************************************************
|
5
|
-
* ©2021-
|
5
|
+
* ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
6
6
|
* *
|
7
7
|
* This program is free software; you can redistribute it and/or modify *
|
8
|
-
* it under the terms of the
|
9
|
-
*
|
10
|
-
* (at your option) any later version. *
|
8
|
+
* it under the terms of the WTFPL 2.0 or later, see *
|
9
|
+
* http://www.wtfpl.net/about/ *
|
11
10
|
* *
|
12
11
|
* This program is distributed in the hope that it will be useful, *
|
13
12
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
14
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
15
|
-
* GNU General Public License for more details. *
|
13
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
16
14
|
* *
|
17
|
-
* You should have received a copy of the GNU General Public License *
|
18
|
-
* along with this program; if not, write to the *
|
19
|
-
* Free Software Foundation, Inc., *
|
20
|
-
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
21
15
|
***************************************************************************/
|
22
16
|
=end
|
23
17
|
|
24
|
-
|
18
|
+
|
19
|
+
require_relative 'basic_logging'
|
25
20
|
class IPRange
|
26
|
-
extend
|
27
|
-
@@log = init_logger(STDOUT)
|
21
|
+
extend BasicLogging
|
28
22
|
|
29
23
|
private
|
30
24
|
|
@@ -35,7 +29,7 @@ class IPRange
|
|
35
29
|
if nibbles.size == 4
|
36
30
|
( (nibbles[0] * 256 + nibbles[1] ) * 256 + nibbles[2] ) * 256 + nibbles[3]
|
37
31
|
else
|
38
|
-
|
32
|
+
error(ip.dup << " is not an IP-address!")
|
39
33
|
return nil
|
40
34
|
end
|
41
35
|
end
|
@@ -53,7 +47,6 @@ class IPRange
|
|
53
47
|
|
54
48
|
# create an IP-range
|
55
49
|
def initialize(args)
|
56
|
-
@log = @@log
|
57
50
|
if args && args.length >= 2
|
58
51
|
|
59
52
|
#first IP
|
@@ -61,7 +54,7 @@ class IPRange
|
|
61
54
|
#last IP
|
62
55
|
@last = ip_to_number args[1]
|
63
56
|
if @last && @first && ( @last < @first )
|
64
|
-
|
57
|
+
error('ERROR! Last IP is smaller than first. Aborting!')
|
65
58
|
exit false
|
66
59
|
end
|
67
60
|
@vrange = args[0,2]
|
data/lib/version.rb
CHANGED
@@ -1,4 +1,21 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
=begin
|
2
|
+
/***************************************************************************
|
3
|
+
* ©2021-2024, Michael Uplawski <michael.uplawski@uplawski.eu> *
|
4
|
+
* *
|
5
|
+
* This program is free software; you can redistribute it and/or modify *
|
6
|
+
* it under the terms of the WTFPL 2.0 or later, see *
|
7
|
+
* http://www.wtfpl.net/about/ *
|
8
|
+
* *
|
9
|
+
* This program is distributed in the hope that it will be useful, *
|
10
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
11
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
12
|
+
* *
|
13
|
+
***************************************************************************/
|
14
|
+
=end
|
15
|
+
|
16
|
+
VERSION = 1.0
|
17
|
+
SUMMARY="New Logging module, new license, Readme for rubygems.org"
|
3
18
|
AUTHORS=["Michael Uplawski@uplawski.eu"]
|
4
19
|
EMAIL="michael.uplawski@uplawski.eu"
|
20
|
+
AUTHORS_MAIL=["<michael.uplawski@uplawski.eu>"]
|
21
|
+
YEARS= "2021-2024"
|
metadata
CHANGED
@@ -1,34 +1,34 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ip_in_range
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0
|
4
|
+
version: '1.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Uplawski@uplawski.eu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Verify that an IP is in a range
|
14
|
-
email: michael.uplawski@uplawski.eu
|
14
|
+
email: "<michael.uplawski@uplawski.eu>"
|
15
15
|
executables:
|
16
16
|
- ip_in_range
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- README.md
|
20
21
|
- bin/ip_in_range
|
21
22
|
- doc/license.txt
|
22
23
|
- ip_in_range.gemspec
|
24
|
+
- lib/basic_logging.rb
|
23
25
|
- lib/email.rb
|
24
26
|
- lib/file_checking.rb
|
25
27
|
- lib/ip_range.rb
|
26
|
-
- lib/log.conf
|
27
|
-
- lib/logging.rb
|
28
28
|
- lib/version.rb
|
29
29
|
homepage: ''
|
30
30
|
licenses:
|
31
|
-
-
|
31
|
+
- Nonstandard
|
32
32
|
metadata: {}
|
33
33
|
post_install_message:
|
34
34
|
rdoc_options: []
|
@@ -38,15 +38,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
38
|
requirements:
|
39
39
|
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
41
|
+
version: '3.0'
|
42
42
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
47
|
requirements: []
|
48
|
-
rubygems_version: 3.
|
48
|
+
rubygems_version: 3.4.20
|
49
49
|
signing_key:
|
50
50
|
specification_version: 4
|
51
|
-
summary:
|
51
|
+
summary: New Logging module, new license, Readme for rubygems.org
|
52
52
|
test_files: []
|
data/lib/log.conf
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
#encoding: UTF-8
|
2
|
-
=begin
|
3
|
-
/***************************************************************************
|
4
|
-
* ©2013 - 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
|
-
|
22
|
-
A simplified logger configuration. Set the level for each individual logger
|
23
|
-
below. Choose a different log-device or log-file if you like. Keep the
|
24
|
-
formatting intact. Do not change other sections of this file.
|
25
|
-
=end
|
26
|
-
|
27
|
-
# Do not touch from here ----->
|
28
|
-
require 'logger'
|
29
|
-
|
30
|
-
debug = Logger::DEBUG
|
31
|
-
info = Logger::INFO
|
32
|
-
error = Logger::ERROR
|
33
|
-
fatal = Logger::FATAL
|
34
|
-
warn = Logger::WARN
|
35
|
-
unknown = Logger::UNKNOWN
|
36
|
-
{
|
37
|
-
# <---------------- to here !
|
38
|
-
|
39
|
-
# Enter your settings here, but take into consideration that not all
|
40
|
-
# the named classes will really produce readable output. Well, you can
|
41
|
-
# always try... Either name just the log-level or make the log-level
|
42
|
-
# precede the output-device or output-file like in the examples.
|
43
|
-
|
44
|
-
# Example: naming a log-file
|
45
|
-
#
|
46
|
-
# :HtmlBuilder => [info, 'C:\temp\htmlbuilder.log'],
|
47
|
-
#
|
48
|
-
# :HtmlBuilder => [debug, '/tmp/htmlbuilder.log'],
|
49
|
-
|
50
|
-
:FMail => info,
|
51
|
-
:IPRange => info,
|
52
|
-
:TopLevel => debug,
|
53
|
-
|
54
|
-
# And ignore the remainder, too.
|
55
|
-
}
|
56
|
-
#eof
|
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
|