Sprichwoerter 0.1 → 1.6
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 +5 -5
- data/Sprichwoerter.gemspec +17 -0
- data/bin/proverbs +43 -2
- data/bin/sprichwoerter +48 -2
- data/doc/html/proverbs.html +472 -0
- data/doc/html/sprichwoerter.html +475 -0
- data/doc/man/proverbs.6.gz +0 -0
- data/doc/man/sprichwoerter.6.gz +0 -0
- data/doc/pdf/proverbs.pdf +0 -0
- data/doc/pdf/sprichwoerter.pdf +0 -0
- data/doc/rst/proverbs.rst +117 -0
- data/doc/rst/sprichwoerter.rst +123 -0
- data/lib/adder.rb +91 -0
- data/lib/argparser.rb +128 -0
- data/lib/color_output.rb +44 -0
- data/lib/liste_de.rb +31 -9
- data/lib/liste_en.rb +134 -86
- data/lib/log.conf +55 -0
- data/lib/logging.rb +193 -0
- data/lib/rearranging.rb +47 -0
- data/lib/sprichw/303/266rter.rb +178 -29
- data/lib/translating.rb +101 -0
- data/lib/translations +164 -0
- data/lib/user_input.rb +48 -0
- data/lib/version.rb +25 -0
- metadata +28 -7
data/lib/color_output.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
#encoding 'UTF-8'
|
2
|
+
|
3
|
+
# functions to apply colors to terminal output
|
4
|
+
|
5
|
+
COLORS = {:default => 9, :black => 0, :red => 1, :green => 2, :yellow => 3, :blue => 4, :purple => 5, :cyan => 6, :white => 7 }
|
6
|
+
|
7
|
+
BG = 4
|
8
|
+
FG = 3
|
9
|
+
REGULAR = 0
|
10
|
+
BOLD = 1
|
11
|
+
UNDERLINE = 4
|
12
|
+
BLINK = 5
|
13
|
+
SWAP = 7
|
14
|
+
NEUTRAL = 0
|
15
|
+
|
16
|
+
STYLES = {:regular => REGULAR, :bold => BOLD, :underline => UNDERLINE, :blink => BLINK, :swap => SWAP, :neutral => NEUTRAL}
|
17
|
+
|
18
|
+
def colorize(text, color_code)
|
19
|
+
"#{color_code}#{text}\033[0m"
|
20
|
+
end
|
21
|
+
|
22
|
+
def style(text, style_code)
|
23
|
+
"#{style_code}#{text}\033[0m"
|
24
|
+
end
|
25
|
+
|
26
|
+
def colored_output(output_text, fg_color = :default, bg_color = :default, style = :regular , mode = :neutral )
|
27
|
+
"\033[%i;%i;%i%i;%i%im%s\033[0m" %[STYLES[mode.to_sym], STYLES[style.to_sym], FG, COLORS[fg_color.to_sym], BG, COLORS[bg_color.to_sym], output_text]
|
28
|
+
end
|
29
|
+
|
30
|
+
def red(text); colorize(text, "\033[31m"); end
|
31
|
+
def green(text); colorize(text, "\033[32m"); end
|
32
|
+
def yellow(text); colorize(text, "\033[33m"); end
|
33
|
+
def purple(text); colorize(text, "\033[35m"); end
|
34
|
+
def cyan(text); colorize(text, "\033[36m"); end
|
35
|
+
def blue(text); colorize(text, "\033[34m"); end
|
36
|
+
def white(text); colorize(text, "\033[37m"); end
|
37
|
+
def black(text); colorize(text, "\033[30m"); end
|
38
|
+
|
39
|
+
def black_on_white(text); colorize(colorize(text, "\033[30m"), "\033[47m");end
|
40
|
+
def white_on_black(text); colorize(colorize(text, "\033[37m"), "\033[40m");end
|
41
|
+
|
42
|
+
def bold(text); style(text, "\033[01m");end
|
43
|
+
def underline(text); style(text, "\033[04m");end
|
44
|
+
|
data/lib/liste_de.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#encoding: UTF-8
|
2
2
|
=begin
|
3
3
|
/***************************************************************************
|
4
|
-
* ©2016-
|
4
|
+
* ©2016-2019 Michael Uplawski <michael.uplawski@uplawski.eu> *
|
5
5
|
* *
|
6
6
|
* This program is free software; you can redistribute it and/or modify *
|
7
7
|
* it under the terms of the GNU General Public License as published by *
|
@@ -20,10 +20,32 @@
|
|
20
20
|
***************************************************************************/
|
21
21
|
=end
|
22
22
|
|
23
|
+
# 1. plural nominativ
|
24
|
+
# TODO: Add proverbs to this first group.
|
25
|
+
$proverbs0=[
|
26
|
+
["Hunde, die bellen,", "beißen nicht"],
|
27
|
+
["Lügen", "haben kurze Beine"],
|
28
|
+
["Scherben", "bringen Glück"],
|
29
|
+
["Kleine Geschenke", "erhalten die Freundschaft"],
|
30
|
+
["Große Ereignisse", "werfen ihre Schatten voraus"],
|
31
|
+
["Viele Köche", "verderben den Brei"],
|
32
|
+
["Viele Jäger", "sind des Hasen Tot"],
|
33
|
+
["Stille Wasser", "sind tief"],
|
34
|
+
["Neue Besen", "kehren gut"],
|
35
|
+
["Viele sind berufen aber nur wenige", "sind auserwählt"],
|
36
|
+
["", ""],
|
37
|
+
["", ""],
|
38
|
+
["", ""],
|
39
|
+
["", ""],
|
40
|
+
["", ""],
|
41
|
+
["", ""],
|
42
|
+
["", ""],
|
43
|
+
["", ""],
|
44
|
+
]
|
23
45
|
|
24
|
-
|
46
|
+
# 2. Singular und unzählbare
|
47
|
+
$proverbs1=[
|
25
48
|
["Der Spatz in der Hand", "ist besser als die Taube auf dem Dach"],
|
26
|
-
["Wer im Glashaus sitzt", "soll nicht mit Steinen werfen"],
|
27
49
|
["Die Axt im Haus", "erspart den Zimmermann"],
|
28
50
|
["Trautes Heim", "- Glück allein"],
|
29
51
|
["Der Fisch", "fängt beim Kopfe an, zu stinken"],
|
@@ -37,14 +59,9 @@ $proverbs=[
|
|
37
59
|
["Gelegenheit", "macht Diebe"],
|
38
60
|
["Kleinvieh", "macht auch Mist"],
|
39
61
|
["Gut Ding", "will Weile haben"],
|
40
|
-
["Wer zuletzt lacht", "lacht am besten"],
|
41
|
-
["Wer den Pfennig nicht ehrt", "ist den Taler nicht wert"],
|
42
62
|
["Ein gebranntes Kind", "scheut das Feuer"],
|
43
63
|
["Ein Unglück", "kommt selten allein"],
|
44
|
-
["Wer den Schaden hat", "braucht für den Spott nicht zu sorgen"],
|
45
64
|
["Ehrlich", "währt am längsten"],
|
46
|
-
["Wer nicht hören will", "muss leiden"],
|
47
|
-
["Wer zuerst kommt", "mahlt zuerst"],
|
48
65
|
["Adel", "verpflichtet"],
|
49
66
|
["Aller Anfang", "ist schwer"],
|
50
67
|
["Alles Gute", "kommt von oben"],
|
@@ -140,6 +157,10 @@ $proverbs=[
|
|
140
157
|
["Versuch", "macht klug"],
|
141
158
|
["Vertrauen", "ist gut, Kontrolle ist besser"],
|
142
159
|
["Vorsicht", "ist besser als Nachsicht"],
|
160
|
+
["Wer nicht hören will", "muss leiden"],
|
161
|
+
["Wer zuerst kommt", "mahlt zuerst"],
|
162
|
+
["Wer zuletzt lacht", "lacht am besten"],
|
163
|
+
["Wer den Pfennig nicht ehrt", "ist den Taler nicht wert"],
|
143
164
|
["Wer A sagt", "muss auch B sagen"],
|
144
165
|
["Wer austeilt", "muss auch einstecken können"],
|
145
166
|
["Wer die Wahl hat", "hat die Qual"],
|
@@ -150,5 +171,6 @@ $proverbs=[
|
|
150
171
|
["Wer sich entschuldigt", "klagt sich an"],
|
151
172
|
["Wer Sorgen hat", "hat auch Likör"],
|
152
173
|
["Wer Wind sät", "wird Sturm ernten"],
|
153
|
-
|
174
|
+
["Wer den Schaden hat", "braucht für den Spott nicht zu sorgen"],
|
175
|
+
["Wer im Glashaus sitzt", "soll nicht mit Steinen werfen"],
|
154
176
|
]
|
data/lib/liste_en.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#encoding: UTF-8
|
2
2
|
=begin
|
3
3
|
/***************************************************************************
|
4
|
-
* ©2016-
|
4
|
+
* ©2016-2019 Michael Uplawski <michael.uplawski@uplawski.eu> *
|
5
5
|
* *
|
6
6
|
* This program is free software; you can redistribute it and/or modify *
|
7
7
|
* it under the terms of the GNU General Public License as published by *
|
@@ -20,89 +20,137 @@
|
|
20
20
|
***************************************************************************/
|
21
21
|
=end
|
22
22
|
|
23
|
+
# plural subjects
|
24
|
+
$proverbs0=[["Good laws", "have sprung from bad customs"],
|
25
|
+
["Beggars", "can't be choosers"],
|
26
|
+
["Birds of a feather", "flock together"],
|
27
|
+
["Fine feathers", "make fine birds"],
|
28
|
+
["All cats", "love fish but hate to get their paws wet"],
|
29
|
+
["Cheaters", "never prosper"],
|
30
|
+
["Children", "are uncertain comforts but certain cares"],
|
31
|
+
["Good clothes", "open all doors"],
|
32
|
+
["Crows", "will not pick out crows eyes"],
|
33
|
+
["Desperate diseases", "must have desperate remedies"],
|
34
|
+
["Barking dogs", "seldom bite"],
|
35
|
+
["Eavesdroppers", "hear no good of themselves"],
|
36
|
+
["Eggs and oaths", "are soon broken"],
|
37
|
+
["Good fences", "make good neighbors"],
|
38
|
+
["Bought friends", "are not friends indeed"],
|
39
|
+
["Two wrongs", "don't make a right"],
|
40
|
+
["Still waters", "run deep"],
|
41
|
+
["Oil and water", "don't mix"],
|
42
|
+
["Marriages", "are made in heaven"],
|
43
|
+
["Mighty oaks" ,"from little acorns grow"],
|
44
|
+
["Manners", "maketh man"],
|
45
|
+
["Whom the Gods love", "die young"],
|
46
|
+
["They who would be young when they are old", "must be old when they are young"],
|
47
|
+
["The best things in life", "are free"],
|
48
|
+
["The more things change, the more", "they stay the same"],
|
49
|
+
["Rats", "desert a sinking ship"],
|
50
|
+
["All roads", "lead to Rome"],
|
51
|
+
["Rules", "were meant to be broken"],
|
52
|
+
["Dead men", "tell no tales"],
|
53
|
+
["Great minds", "agree"],
|
54
|
+
["People who live in glass houses", "shouldn't throw stones"],
|
55
|
+
["Those who do not learn from history", "are doomed to repeat it"],
|
56
|
+
["Many are called but few", "are chosen"],
|
57
|
+
["Sticks and stones", "may break my bones, but words will never hurt me"],
|
58
|
+
["Walls", "have ears"]]
|
23
59
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
]
|
60
|
+
# singular and uncountables
|
61
|
+
$proverbs1=[["The best", "is cheapest"],
|
62
|
+
["The pen", "is mightier than the sword"],
|
63
|
+
["The squeaky wheel", "gets the grease"],
|
64
|
+
["No man", "is an island"],
|
65
|
+
["Fortune", "favors the bold"],
|
66
|
+
["A picture", "is worth a thousand words"],
|
67
|
+
["Discretion", "is the greater part of valor"],
|
68
|
+
["The early bird", "catches the worm"],
|
69
|
+
["God", "helps those who help themselves"],
|
70
|
+
["Cleanliness", "is next to godliness"],
|
71
|
+
["A watched pot", "never boils"],
|
72
|
+
["Practice", "makes perfect"],
|
73
|
+
["One man's trash", "is another man's treasure"],
|
74
|
+
["Beauty", "is in the eye of the beholder"],
|
75
|
+
["Necessity", "is the mother of invention"],
|
76
|
+
["A penny saved", "is a penny earned"],
|
77
|
+
["Familiarity", "breeds contempt"],
|
78
|
+
["The grass", "is always greener on the other side of the hill"],
|
79
|
+
["A chain", "is only as strong as its weakest link"],
|
80
|
+
["Honesty", "is the best policy"],
|
81
|
+
["Absence", "makes the heart grow fonder"],
|
82
|
+
["A broken clock", "is right twice a day"],
|
83
|
+
["A friend in need", "is a friend indeed"],
|
84
|
+
["A little bit of knowledge", "is a dangerous thing"],
|
85
|
+
["A journey of a thousand miles", "begins with a single step"],
|
86
|
+
["All", "is fair in love and war"],
|
87
|
+
["All work and no play", "makes Jack a dull boy"],
|
88
|
+
["Early to bed and early to rise", "makes a man healthy, wealthy and wise"],
|
89
|
+
["Every cloud", "has a silver lining"],
|
90
|
+
["Haste", "makes waste"],
|
91
|
+
["Hindsight", "is 20/20"],
|
92
|
+
["Jack of all trades", "master of none"],
|
93
|
+
["Knowledge", "is power"],
|
94
|
+
["Misery", "loves company"],
|
95
|
+
["Power corrupts and absolute power", "corrupts absolutely"],
|
96
|
+
["Rome", "wasn't build in a day"],
|
97
|
+
["Slow and steady", "wins the race"],
|
98
|
+
["The apple", "doesn't fall far from the tree"],
|
99
|
+
["The road to hell", "is paved with good intentions"],
|
100
|
+
["The best way to a man's heart", "is through his stomach"],
|
101
|
+
["Time", "is money"],
|
102
|
+
["Two is company, three", "is a crowd"],
|
103
|
+
["What goes around", "comes around"],
|
104
|
+
["What goes up", "must come down"],
|
105
|
+
["What's good for the goose", "is good for the gender"],
|
106
|
+
["He who does not advance", "goes backward"],
|
107
|
+
["Advice most needed", "is least heeded"],
|
108
|
+
["An apple a day", "keeps the doctor away"],
|
109
|
+
["A rotten apple", "injures its companions"],
|
110
|
+
["A bad settlement", "is better than a good lawsuit"],
|
111
|
+
["Better", "is the enemy of good"],
|
112
|
+
["A good beginning", "makes a good ending"],
|
113
|
+
["Well begun", "is half done"],
|
114
|
+
["A bird in the hand", "is worth two in the bush"],
|
115
|
+
["Blood", "is thicker than water"],
|
116
|
+
["Good blood", "always shows itself"],
|
117
|
+
["A book", "is a friend"],
|
118
|
+
["A rising tide", "lifts all boats"],
|
119
|
+
["Half a loaf", "is better than no bread"],
|
120
|
+
["A new broome", "sweapeth cleane"],
|
121
|
+
["The younger brother", "the better gentleman"],
|
122
|
+
["Business", "is business"],
|
123
|
+
["Everyone's business", "is no one's business"],
|
124
|
+
["A candle", "loses nothing by lighting another candle"],
|
125
|
+
["Curiosity", "killed the cat"],
|
126
|
+
["The child", "is father to the man and to the woman"],
|
127
|
+
["Who has not served", "cannot command"],
|
128
|
+
["Common sense", "is not so common"],
|
129
|
+
["Confidence", "begets confidence"],
|
130
|
+
["A guilty conscience", "needs an accuser"],
|
131
|
+
["He's an ill cook that", "cannot lick his own fingers"],
|
132
|
+
["Counsel", "is no command"],
|
133
|
+
["The customer", "is always right"],
|
134
|
+
["The best defense", "is a good offence"],
|
135
|
+
["Who digs a trap for others", "ends up in it himself"],
|
136
|
+
["An ounce of discretion", "is worth a pound of wit"],
|
137
|
+
["\"Well done\"", "is better than \"well said\""],
|
138
|
+
["The door", "swings both ways"],
|
139
|
+
["Constant dropping", "wears away the stone"],
|
140
|
+
["Dwarf on a giants shoulder", "sees farther of the two"],
|
141
|
+
["Good eating", "deserves good drinking"],
|
142
|
+
["He that steals an egg", "will steal an ox"],
|
143
|
+
["An empty vessel", "makes much noise"],
|
144
|
+
["All's well that", "ends well"],
|
145
|
+
["The enemy of my enemy", "is my friend"],
|
146
|
+
["Every rose", "has its thorn"],
|
147
|
+
["Evil", "begets evil"],
|
148
|
+
["Example", "is better than correction"],
|
149
|
+
["The exception", "proves the rule"],
|
150
|
+
["A staff", "is quickly found to beat a dog"],
|
151
|
+
["Brag is a good dog, but holdfast", "is better"],
|
152
|
+
["He that would hang his dog", "gives out first that he is mad"],
|
153
|
+
["Common fame", "is often to blame"],
|
154
|
+
["A burnt child", "dreads the fire"],
|
155
|
+
["All is fish that", "comes to the net"],
|
156
|
+
["A fish", "always rots from the head down"]]
|
data/lib/log.conf
ADDED
@@ -0,0 +1,55 @@
|
|
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
|
+
:SprichWoerter => info,
|
51
|
+
:Adder => info,
|
52
|
+
|
53
|
+
# And ignore the remainder, too.
|
54
|
+
}
|
55
|
+
#eof
|
data/lib/logging.rb
ADDED
@@ -0,0 +1,193 @@
|
|
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
|
+
# allow to override the set log-levels with an
|
73
|
+
# external configuration (log.conf).
|
74
|
+
log_conf(cn)
|
75
|
+
# Or use the defaults as set here or elsewhere...
|
76
|
+
|
77
|
+
@level ||= level
|
78
|
+
@target ||= target
|
79
|
+
|
80
|
+
@log = Logger.new(@target)
|
81
|
+
@log.level = @level
|
82
|
+
|
83
|
+
@log.formatter = proc do |severity, datetime, progname, msg|
|
84
|
+
t = Time.now
|
85
|
+
"#{cn}: #{severity} #{t.hour}-#{t.min}-#{t.sec}: #{msg}\n"
|
86
|
+
end
|
87
|
+
if ! @@have_log
|
88
|
+
@log.debug cn.dup << ' reading logging-configuration from ' << @@LOG_CONF
|
89
|
+
@@have_log = true
|
90
|
+
@log.debug('level is ' << level.to_s)
|
91
|
+
end
|
92
|
+
return @log
|
93
|
+
end
|
94
|
+
|
95
|
+
# Set the log-target to an IO object.
|
96
|
+
def log_target=(target)
|
97
|
+
@target = target
|
98
|
+
@log = Logger.new(@@target)
|
99
|
+
@log.level = @level
|
100
|
+
end
|
101
|
+
|
102
|
+
# set the log-level
|
103
|
+
def log_level=(level)
|
104
|
+
@level = level
|
105
|
+
@log.level = @level
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
# Override or set the log-level and target-device, as set in a file 'log.conf'.
|
111
|
+
# I do not like the look of this, but it works just the way I want it to.
|
112
|
+
# "HEAVANS! Isn't there a standard way to do this in Ruby, for Christ's sake?", you say.
|
113
|
+
# Heck, I don't care. <= Read that again, I say.
|
114
|
+
def log_conf(cn = nil)
|
115
|
+
config = level = target = nil
|
116
|
+
# puts 'log-config is in ' << @@LOG_CONF
|
117
|
+
if(File::exist?(@@LOG_CONF) )
|
118
|
+
begin
|
119
|
+
conf = File.read(@@LOG_CONF)
|
120
|
+
config = instance_eval(conf)
|
121
|
+
rescue Exception => ex
|
122
|
+
STDERR.puts "WARNING! Cannot evaluate the logger-configuration!" << ' ' << ex.message
|
123
|
+
STDERR.puts "Default log-levels apply."
|
124
|
+
end
|
125
|
+
else
|
126
|
+
puts "Default log-levels apply."
|
127
|
+
end
|
128
|
+
|
129
|
+
if(config && config.respond_to?(:to_hash) )
|
130
|
+
config.default = nil
|
131
|
+
if cn
|
132
|
+
config = config[cn.to_sym]
|
133
|
+
else
|
134
|
+
config = config[self.class.name.to_sym]
|
135
|
+
end
|
136
|
+
|
137
|
+
if(config )
|
138
|
+
if(config.respond_to?(:to_ary) && config.size == 2)
|
139
|
+
@level, @target = config
|
140
|
+
@target.downcase!
|
141
|
+
logdir = File.dirname(@target)
|
142
|
+
[:exist?, :directory?, :writable?].each do |m|
|
143
|
+
msg = File.send(m, logdir)
|
144
|
+
if(msg)
|
145
|
+
STDERR.puts "WARNING! A logfile for '%s' cannot be written to %s (%s)!" %[self.class.name, logdir, msg]
|
146
|
+
@target = nil
|
147
|
+
end
|
148
|
+
end
|
149
|
+
else
|
150
|
+
@level = config
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
######### test
|
158
|
+
if __FILE__ == $0
|
159
|
+
class TClass
|
160
|
+
# class level ---->
|
161
|
+
self.extend(Logging)
|
162
|
+
@@log = init_logger(STDOUT, Logger::INFO)
|
163
|
+
# <------
|
164
|
+
# object-level ---->
|
165
|
+
include Logging
|
166
|
+
# <---------
|
167
|
+
|
168
|
+
def test_log
|
169
|
+
@@log.info('class-level logger called from instance: ' << @@log.to_s)
|
170
|
+
#@log = @@log # works too
|
171
|
+
@log = TClass.class_eval{@log}
|
172
|
+
@log.info('AGAIN: class-level logger called from instance: ' << @log.to_s)
|
173
|
+
@log.debug("you won't see this on log-level INFO")
|
174
|
+
|
175
|
+
# object-level ---->
|
176
|
+
init_logger
|
177
|
+
# <-----------
|
178
|
+
@log.info("That's a different thing: " << @log.to_s << " - object-level logger!")
|
179
|
+
|
180
|
+
end
|
181
|
+
def self::test_log
|
182
|
+
@log.info('class-level logger called from class: ' << @log.to_s)
|
183
|
+
@@log.info('AGAIN: class-level logger called from class: ' << @log.to_s)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
TClass.new.test_log # class-logger + 1st object-logger
|
188
|
+
TClass.new.test_log # same class-logger + 2nd object-logger
|
189
|
+
|
190
|
+
TClass::test_log # same class-logger
|
191
|
+
puts 'And just say it once clearly: THIS IS COOOL!!'
|
192
|
+
end
|
193
|
+
#EOF
|