html2index 1.2.1 → 1.31

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,206 +0,0 @@
1
- #encoding: UTF-8
2
- =begin
3
- /***************************************************************************
4
- * ©2011-2017 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
- require_relative 'file_checking'
24
-
25
- =begin Creates a member @log and precede its output with the name of the class
26
- of the object.
27
- Example for a class-level logger:
28
- # --------------------
29
- class TClass
30
- self.extend(Logging)
31
- @@log = init_logger(STDOUT)
32
- def test_log
33
- @@log.info('class-level logger called from instance: ' << @@log.to_s)
34
- @log = @@log
35
- @log.info('AGAIN: class-level logger called from instance: ' << @log.to_s)
36
- end
37
- def self::test_log
38
- @log.info('class-level logger called from class: ' << @log.to_s)
39
- @@log.info('AGAIN: class-level logger called from class: ' << @@log.to_s)
40
- end
41
- end
42
- #---------------------
43
- Example for a object-level logger:
44
- ATTN! This means 1 logger per object.
45
- # --------------------
46
- class TClass
47
- include Logging
48
- def initialize
49
- init_logger(STDOUT, Logger::DEBUG)
50
- end
51
- def test_log
52
- @log.debug('called test_log() ')
53
- end
54
- end
55
- =end
56
- module Logging
57
- @@LEVELS = {:debug => Logger::DEBUG, :info => Logger::INFO, :error => Logger::ERROR, :warn => Logger::WARN, :unknown => Logger::UNKNOWN, :fatal => Logger::FATAL}
58
-
59
- @@have_log = false
60
- @@LOG_CONF = File.dirname(File.absolute_path(__FILE__)) << File::Separator << 'log.conf'
61
-
62
- # Call this method in an instance-method (e.g. initialize() ) to define the
63
- # object-level logger; i.e. an object-specific member @log.
64
- # Call this method within the class-definition for a class-level logger; i.e.
65
- # a member @log for class-level acces.
66
- # The method returns the logger, so you can actually do what you want with it.
67
- def init_logger(target = STDOUT, level = Logger::INFO)
68
- # Prepare for a class-level logger. This is actually quite cool.
69
- # ---> Ingeniuous code starts here
70
- cn = (self.class == Class ? name : self.class.name)
71
- # <--- Ingeniuous code ends here
72
-
73
- # allow to override the set log-levels with an
74
- # external configuration (log.conf).
75
- log_conf(cn)
76
- # Or use the defaults as set here or elsewhere...
77
-
78
- @level ||= level
79
- @target ||= target
80
- @log = Logger.new(@target)
81
- @log.level = @level
82
- @log.formatter = formatter(cn)
83
- if ! @@have_log
84
- @log.debug cn.dup << ' reading logging-configuration from ' << @@LOG_CONF
85
- @@have_log = true
86
- @log.debug('level is ' << level.to_s)
87
- end
88
- return @log
89
- end
90
-
91
- def formatter(classname)
92
- proc do |severity, datetime, progname, msg|
93
- t = Time.now
94
- "#{classname} (#{__LINE__}): #{severity} #{t.hour}-#{t.min}-#{t.sec}: #{msg}\n"
95
- end
96
- end
97
-
98
- # Set the log-target to an IO object.
99
- def log_target=(target)
100
- @target = target
101
- @log = Logger.new(@target)
102
- @log.level = @level
103
- end
104
-
105
- # set the log-level
106
-
107
- def log_level=(level)
108
- @level = level
109
- @log.level = @level
110
- end
111
-
112
- # verify log level
113
- def log_level?(level)
114
- return @log.level == level
115
- end
116
-
117
- # brutal log-function: print message, than exit.
118
- def debout(str)
119
- puts self.class.name.dup << " DEBOUT: " << str
120
- exit true
121
- end
122
-
123
- private
124
-
125
- # Override or set the log-level and target-device, as set in a file 'log.conf'.
126
- # I do not like the look of this, but it works just the way I want it to.
127
- # "HEAVANS! Isn't there a standard way to do this in Ruby, for Christ's sake?", you say.
128
- # Heck, I don't care. <= Read that again, I say.
129
- def log_conf(cn = nil)
130
- config = level = target = nil
131
- if(File::exist?(@@LOG_CONF) )
132
- begin
133
- conf = File.read(@@LOG_CONF)
134
- config = instance_eval(conf)
135
- rescue Exception => ex
136
- STDERR.puts "WARNING! Cannot evaluate the logger-configuration!" << ' ' << ex.message
137
- STDERR.puts "Default log-levels apply."
138
- end
139
- else
140
- puts "Default log-levels apply."
141
- end
142
-
143
- if(config && config.respond_to?(:to_hash) )
144
- config.default = nil
145
- if cn
146
- config = config[cn.to_sym]
147
- else
148
- config = config[self.class.name.to_sym]
149
- end
150
-
151
- if(config )
152
- if(config.respond_to?(:to_ary) && config.size == 2)
153
- @level, @target = config
154
- @target.downcase!
155
- logdir = File.dirname(@target)
156
- msg = File_Checking::file_check(logdir, :exist?, :directory?, :writable?)
157
- if(msg)
158
- STDERR.puts "WARNING! A logfile for '%s' cannot be written to %s (%s)!" %[self.class.name, logdir, msg]
159
- @target = nil
160
- end
161
- else
162
- @level = config
163
- end
164
- end
165
- end
166
- end
167
- attr_reader :level
168
- end
169
-
170
- ######### test
171
- if __FILE__ == $0
172
- class TClass
173
- # class level ---->
174
- self.extend(Logging)
175
- @@log = init_logger(STDOUT, Logger::INFO)
176
- # <------
177
- # object-level ---->
178
- include Logging
179
- # <---------
180
-
181
- def test_log
182
- @@log.info('class-level logger called from instance: ' << @@log.to_s)
183
- #@log = @@log # works too
184
- @log = TClass.class_eval{@log}
185
- @log.info('AGAIN: class-level logger called from instance: ' << @log.to_s)
186
- @log.debug("you won't see this on log-level INFO")
187
-
188
- # object-level ---->
189
- init_logger
190
- # <-----------
191
- @log.info("That's a different thing: " << @log.to_s << " - object-level logger!")
192
-
193
- end
194
- def self::test_log
195
- @log.info('class-level logger called from class: ' << @log.to_s)
196
- @@log.info('AGAIN: class-level logger called from class: ' << @log.to_s)
197
- end
198
- end
199
-
200
- TClass.new.test_log # class-logger + 1st object-logger
201
- TClass.new.test_log # same class-logger + 2nd object-logger
202
-
203
- TClass::test_log # same class-logger
204
- puts 'And just say it once clearly: THIS IS COOOL!!'
205
- end
206
- #EOF