logue 1.0.5 → 1.0.8

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/logue/logger.rb CHANGED
@@ -9,10 +9,11 @@
9
9
  # Documentation:: Author
10
10
  #
11
11
 
12
- require 'rainbow'
12
+ require 'rainbow/x11_color_names'
13
13
  require 'pathname'
14
14
  require 'logue/severity'
15
15
  require 'logue/format'
16
+ require 'logue/pathutil'
16
17
 
17
18
  #
18
19
  # == Logger
@@ -26,241 +27,233 @@ require 'logue/format'
26
27
  #
27
28
 
28
29
  module Logue
29
- class Logger
30
- $LOGGING_LEVEL = nil
30
+ end
31
31
 
32
- attr_accessor :quiet
33
- attr_accessor :output
34
- attr_accessor :colorize_line
35
- attr_accessor :level
36
- attr_accessor :ignored_files
37
- attr_accessor :ignored_methods
38
- attr_accessor :ignored_classes
39
- attr_accessor :trim
32
+ class Logue::Logger
33
+ $LOGGING_LEVEL = nil
40
34
 
41
- include Log::Severity
35
+ attr_accessor :quiet
36
+ attr_accessor :output
37
+ attr_accessor :colorize_line
38
+ attr_accessor :level
39
+ attr_accessor :ignored_files
40
+ attr_accessor :ignored_methods
41
+ attr_accessor :ignored_classes
42
+
43
+ attr_reader :trim
42
44
 
43
- FRAME_RE = Regexp.new('(.*):(\d+)(?::in \`(.*)\')?')
45
+ include Logue::Log::Severity
44
46
 
45
- def initialize
46
- set_defaults
47
- end
48
-
49
- def verbose= v
50
- @level = case v
51
- when TrueClass
52
- DEBUG
53
- when FalseClass
54
- FATAL
55
- when Integer
56
- v
57
- end
58
- end
47
+ FRAME_RE = Regexp.new('(.*):(\d+)(?::in \`(.*)\')?')
59
48
 
60
- def set_defaults
61
- $LOGGING_LEVEL = @level = FATAL
62
- @ignored_files = Hash.new
63
- @ignored_methods = Hash.new
64
- @ignored_classes = Hash.new
65
- @width = 0
66
- @output = $stdout
67
- @colors = Array.new
68
- @colorize_line = false
69
- @quiet = false
70
- @trim = true
71
-
72
- set_default_widths
73
- end
49
+ def initialize
50
+ set_defaults
51
+ end
52
+
53
+ def verbose= v
54
+ @level = case v
55
+ when TrueClass
56
+ DEBUG
57
+ when FalseClass
58
+ FATAL
59
+ when Integer
60
+ v
61
+ end
62
+ end
74
63
 
75
- def set_default_widths
76
- set_widths Log::DEFAULT_FILENAME_WIDTH, Log::DEFAULT_LINENUM_WIDTH, Log::DEFAULT_FUNCTION_WIDTH
77
- end
64
+ def set_defaults
65
+ $LOGGING_LEVEL = @level = FATAL
66
+ @ignored_files = Hash.new
67
+ @ignored_methods = Hash.new
68
+ @ignored_classes = Hash.new
69
+ @output = $stdout
70
+ @colors = Array.new
71
+ @colorize_line = false
72
+ @quiet = false
73
+ @trim = true
78
74
 
79
- def verbose
80
- level <= DEBUG
81
- end
75
+ @format = Logue::Format.new
82
76
 
83
- # Assigns output to a file with the given name. Returns the file; client
84
- # is responsible for closing it.
85
- def outfile= f
86
- @output = f.kind_of?(IO) ? f : File.new(f, "w")
87
- end
77
+ set_default_widths
78
+ end
88
79
 
89
- # Creates a printf format for the given widths, for aligning output. To lead
90
- # lines with zeros (e.g., "00317") the line_width argument must be a string,
91
- # not an integer.
92
- def set_widths file_width, line_width, func_width
93
- @file_width = file_width
94
- @line_width = line_width
95
- @function_width = func_width
96
-
97
- @format = "[%#{file_width}s:%#{line_width}d] {%#{func_width}s}"
98
- end
80
+ def trim= what
81
+ end
99
82
 
100
- def ignore_file fname
101
- ignored_files[fname] = true
102
- end
103
-
104
- def ignore_method methname
105
- ignored_methods[methname] = true
106
- end
107
-
108
- def ignore_class classname
109
- ignored_classes[classname] = true
110
- end
83
+ def set_default_widths
84
+ set_widths Logue::FormatWidths::DEFAULT_FILENAME, Logue::FormatWidths::DEFAULT_LINENUM, Logue::FormatWidths::DEFAULT_FUNCTION
85
+ end
111
86
 
112
- def log_file fname
113
- ignored_files.delete fname
114
- end
115
-
116
- def log_method methname
117
- ignored_methods.delete methname
118
- end
119
-
120
- def log_class classname
121
- ignored_classes.delete classname
122
- end
87
+ def verbose
88
+ level <= DEBUG
89
+ end
123
90
 
124
- def debug msg = "", depth = 1, &blk
125
- log msg, DEBUG, depth + 1, &blk
126
- end
91
+ # Assigns output to a file with the given name. Returns the file; client
92
+ # is responsible for closing it.
93
+ def outfile= f
94
+ @output = f.kind_of?(IO) ? f : File.new(f, "w")
95
+ end
127
96
 
128
- def info msg = "", depth = 1, &blk
129
- log msg, INFO, depth + 1, &blk
130
- end
97
+ # Creates a printf format for the given widths, for aligning output. To lead
98
+ # lines with zeros (e.g., "00317") the line_width argument must be a string,
99
+ # not an integer.
100
+ def set_widths file_width, line_width, func_width
101
+ @file_width = file_width
102
+ @line_width = line_width
103
+ @function_width = func_width
104
+ end
131
105
 
132
- def warn msg = "", depth = 1, &blk
133
- log msg, WARN, depth + 1, &blk
134
- end
106
+ def ignore_file fname
107
+ ignored_files[fname] = true
108
+ end
109
+
110
+ def ignore_method methname
111
+ ignored_methods[methname] = true
112
+ end
113
+
114
+ def ignore_class classname
115
+ ignored_classes[classname] = true
116
+ end
135
117
 
136
- def error msg = "", depth = 1, &blk
137
- log msg, ERROR, depth + 1, &blk
138
- end
118
+ def log_file fname
119
+ ignored_files.delete fname
120
+ end
121
+
122
+ def log_method methname
123
+ ignored_methods.delete methname
124
+ end
125
+
126
+ def log_class classname
127
+ ignored_classes.delete classname
128
+ end
139
129
 
140
- def fatal msg = "", depth = 1, &blk
141
- log msg, FATAL, depth + 1, &blk
142
- end
130
+ def debug msg = "", depth = 1, cname = nil, &blk
131
+ log msg, DEBUG, depth + 1, cname, &blk
132
+ end
143
133
 
144
- # Logs the given message.
145
- def log msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
146
- if lvl >= level
147
- frame = nil
148
-
149
- stk = caller 0
150
- stk.reverse.each_with_index do |frm, idx|
151
- if frm.index(%r{logue/log.*:\d+:in\b})
152
- break
153
- else
154
- frame = frm
155
- end
156
- end
134
+ def info msg = "", depth = 1, cname = nil, &blk
135
+ log msg, INFO, depth + 1, cname, &blk
136
+ end
157
137
 
158
- print_stack_frame frame, cname, msg, lvl, &blk
159
- end
160
- end
138
+ def warn msg = "", depth = 1, cname = nil, &blk
139
+ log msg, WARN, depth + 1, cname, &blk
140
+ end
161
141
 
162
- # Shows the current stack.
163
- def stack msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
164
- if lvl >= level
165
- stk = caller depth
166
- for frame in stk
167
- print_stack_frame frame, cname, msg, lvl, &blk
168
- cname = nil
169
- msg = ""
170
- end
171
- end
172
- end
142
+ def error msg = "", depth = 1, cname = nil, &blk
143
+ log msg, ERROR, depth + 1, cname, &blk
144
+ end
173
145
 
174
- def print_stack_frame frame, cname, msg, lvl, &blk
175
- md = FRAME_RE.match frame
176
- file, line, func = md[1], md[2], (md[3] || "")
177
- # file.sub!(/.*\//, "")
146
+ def fatal msg = "", depth = 1, cname = nil, &blk
147
+ log msg, FATAL, depth + 1, cname, &blk
148
+ end
178
149
 
179
- # Ruby 1.9 expands the file name, but 1.8 doesn't:
180
- pn = Pathname.new(file).expand_path
181
-
182
- file = pn.to_s
150
+ # Logs the given message.
151
+ def log msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
152
+ if lvl >= level
153
+ frame = nil
183
154
 
184
- if cname
185
- func = cname + "#" + func
186
- end
187
-
188
- if ignored_files[file] || (cname && ignored_classes[cname]) || ignored_methods[func]
189
- # skip this one.
190
- else
191
- print_formatted(file, line, func, msg, lvl, &blk)
155
+ stk = caller 0
156
+ stk.reverse.each_with_index do |frm, idx|
157
+ if frm.index(%r{logue/log.*:\d+:in\b})
158
+ break
159
+ else
160
+ frame = frm
161
+ end
192
162
  end
163
+
164
+ print_stack_frame frame, cname, msg, lvl, &blk
193
165
  end
166
+ end
194
167
 
195
- def print_formatted file, line, func, msg, lvl, &blk
196
- if trim
197
- fmt = Format.new
198
- file = fmt.trim_right file, @file_width
199
- line = fmt.trim_left line, @line_width
200
- func = fmt.trim_left func, @function_width
168
+ # Shows the current stack.
169
+ def stack msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
170
+ if lvl >= level
171
+ stk = caller depth
172
+ stk.each do |frame|
173
+ print_stack_frame frame, cname, msg, lvl, &blk
174
+ cname = nil
175
+ msg = ""
201
176
  end
202
-
203
- hdr = sprintf @format, file, line, func
204
- print hdr, msg, lvl, &blk
205
177
  end
206
-
207
- def print hdr, msg, lvl, &blk
208
- if blk
209
- x = blk.call
210
- if x.kind_of? String
211
- msg = x
212
- else
213
- return
214
- end
215
- end
178
+ end
216
179
 
217
- msg = msg.to_s.chomp
180
+ def print_stack_frame frame, cname, msg, lvl, &blk
181
+ md = FRAME_RE.match frame
182
+ file, line, func = md[1], md[2], (md[3] || "")
183
+ # file.sub!(/.*\//, "")
218
184
 
219
- if lvlcol = @colors[lvl]
220
- if colorize_line
221
- line = hdr + " " + msg
222
- @output.puts line.color(lvlcol)
223
- else
224
- @output.puts hdr + " " + msg.color(lvlcol)
225
- end
226
- else
227
- @output.puts hdr + " " + msg
228
- end
229
- end
185
+ # Ruby 1.9 expands the file name, but 1.8 doesn't:
186
+ pn = Pathname.new(file).expand_path
187
+
188
+ file = pn.to_s
230
189
 
231
- def set_color lvl, color
232
- @colors[lvl] = color
190
+ if cname
191
+ func = cname + "#" + func
233
192
  end
193
+
194
+ if ignored_files[file] || (cname && ignored_classes[cname]) || ignored_methods[func]
195
+ # skip this one.
196
+ else
197
+ print_formatted(file, line, func, msg, lvl, &blk)
198
+ end
199
+ end
234
200
 
235
- def method_missing meth, *args, &blk
236
- validcolors = Sickill::Rainbow::TERM_COLORS
237
- # only handling foregrounds, not backgrounds
238
- if code = validcolors[meth]
239
- add_color_method meth.to_s, code + 30
240
- send meth, *args, &blk
201
+ def print_formatted file, line, func, msg, lvl, &blk
202
+ fmt = Logue::Format.new file_width: @file_width, line_width: @line_width, method_width: @function_width, trim: @trim
203
+ location = fmt.format file, line, nil, func
204
+ print location, msg, lvl, &blk
205
+ end
206
+
207
+ def print hdr, msg, lvl, &blk
208
+ if blk
209
+ x = blk.call
210
+ if x.kind_of? String
211
+ msg = x
241
212
  else
242
- super
213
+ return
243
214
  end
244
215
  end
245
216
 
246
- def respond_to? meth
247
- validcolors = Sickill::Rainbow::TERM_COLORS
248
- validcolors.include?(meth) || super
249
- end
217
+ msg = msg.to_s.chomp
250
218
 
251
- def add_color_method color, code
252
- instmeth = Array.new
253
- instmeth << "def #{color}(msg = \"\", lvl = DEBUG, depth = 1, cname = nil, &blk)"
254
- instmeth << " log(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
255
- instmeth << "end"
256
- instance_eval instmeth.join("\n")
219
+ if lvlcol = @colors[lvl]
220
+ if colorize_line
221
+ line = hdr + " " + msg
222
+ @output.puts line.color(lvlcol)
223
+ else
224
+ @output.puts hdr + " " + msg.color(lvlcol)
225
+ end
226
+ else
227
+ @output.puts hdr + " " + msg
228
+ end
229
+ end
257
230
 
258
- clsmeth = Array.new
259
- clsmeth << "def #{color}(msg = \"\", lvl = DEBUG, depth = 1, cname = nil, &blk)"
260
- clsmeth << " logger.#{color}(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
261
- clsmeth << "end"
231
+ def set_color lvl, color
232
+ @colors[lvl] = color
233
+ end
262
234
 
263
- class_eval clsmeth.join("\n")
235
+ def method_missing meth, *args, &blk
236
+ # validcolors = Rainbow::X11ColorNames::NAMES
237
+ validcolors = Rainbow::Color::Named::NAMES
238
+ # only handling foregrounds, not backgrounds
239
+ if code = validcolors[meth]
240
+ add_color_method meth.to_s, code + 30
241
+ send meth, *args, &blk
242
+ else
243
+ super
264
244
  end
265
245
  end
246
+
247
+ def respond_to? meth
248
+ validcolors = Rainbow::X11ColorNames::NAMES
249
+ validcolors.include?(meth) || super
250
+ end
251
+
252
+ def add_color_method color, code
253
+ instmeth = Array.new
254
+ instmeth << "def #{color}(msg = \"\", lvl = DEBUG, depth = 1, cname = nil, &blk)"
255
+ instmeth << " log(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
256
+ instmeth << "end"
257
+ instance_eval instmeth.join("\n")
258
+ end
266
259
  end
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ module Logue
5
+ end
6
+
7
+ class Logue::PathUtil
8
+ class << self
9
+ def trim_left str, maxlen
10
+ str[0 ... maxlen.to_i.abs]
11
+ end
12
+
13
+ def trim_right str, maxlen
14
+ mxln = maxlen.abs
15
+
16
+ if str.length > mxln
17
+ trim_path_right str, maxlen
18
+ else
19
+ str
20
+ end
21
+ end
22
+
23
+ def trim_path_right path, maxlen
24
+ mxln = maxlen.abs
25
+
26
+ comps = path.split "/"
27
+ str = comps.pop
28
+ comps.reverse.each do |comp|
29
+ newstr = comp + "/" + str
30
+ if newstr.length + 4 <= mxln
31
+ str = newstr
32
+ else
33
+ newstr = "..." + "/" + str
34
+ if newstr.length <= mxln
35
+ str = newstr
36
+ end
37
+ break
38
+ end
39
+ end
40
+ str
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'logue/format'
5
+
6
+ module Logue
7
+ end
8
+
9
+ class Logue::Writer
10
+ attr_accessor :out
11
+
12
+ def initialize out = $stdout
13
+ @out = out
14
+ end
15
+
16
+ def write fmt, stack, nframes, cls = nil
17
+ stack[0 ... nframes].each do |frame|
18
+ path = frame.absolute_path
19
+ lineno = frame.lineno
20
+ func = frame.label
21
+ @out.puts fmt.format path, lineno, cls, func
22
+ end
23
+ end
24
+ end
data/lib/logue.rb CHANGED
@@ -13,5 +13,5 @@ Pathname.glob(loguelibdir + '/logue/**/*.rb').each do |file|
13
13
  end
14
14
 
15
15
  module Logue
16
- VERSION = '1.0.5'
16
+ VERSION = '1.0.8'
17
17
  end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'logue/colors'
5
+ require 'test/unit'
6
+
7
+ class ColorsTest < Test::Unit::TestCase
8
+ def test_valid_colors
9
+ result = Colors.valid_colors
10
+ assert_equal [ :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :default ], result.keys
11
+ end
12
+ end
@@ -1,80 +1,59 @@
1
1
  #!/usr/bin/ruby -w
2
2
  # -*- ruby -*-
3
3
 
4
- require 'pathname'
5
4
  require 'test/unit'
6
- require 'stringio'
7
5
  require 'logue/format'
8
- require 'logue/loggable'
9
6
 
10
- module Logue
11
- class FormatTestCase < Test::Unit::TestCase
12
- include Logue::Loggable
13
-
14
- # trim_left
15
-
16
- def run_trim_left_test expected, length, str = "something"
17
- trimmed = Format.new.trim_left(str, length)
18
- assert_equal expected, trimmed
19
- end
20
-
21
- def test_trim_left_short_positive_number
22
- run_trim_left_test "some", 4
23
- end
24
-
25
- def test_trim_left_long
26
- run_trim_left_test "something", 10
27
- end
28
-
29
- def test_trim_left_short_negative_number
30
- run_trim_left_test "some", -4
31
- end
32
-
33
- # trim_right
34
-
35
- def assert_trim_right expected, length, str
36
- trimmed = Format.new.trim_right str, length
37
- assert_equal expected, trimmed, "length: #{length}"
38
- end
39
-
40
- def test_trim_right_path_excess
41
- assert_trim_right "ab/cd/ef.t", 11, "ab/cd/ef.t"
42
- end
43
-
44
- def test_trim_right_path_at_length
45
- assert_trim_right "ab/cd/ef.t", 10, "ab/cd/ef.t"
46
- end
47
-
48
- def test_trim_right_path_one_less
49
- assert_trim_right ".../ef.t", 9, "ab/cd/ef.t"
50
- end
51
-
52
- def test_trim_right_path_two_less
53
- assert_trim_right ".../ef.t", 8, "ab/cd/ef.t"
54
- end
55
-
56
- def test_trim_right_path_three_less
57
- assert_trim_right "ef.t", 7, "ab/cd/ef.t"
58
- end
7
+ class Logue::FormatTestCase < Test::Unit::TestCase
8
+ # use as:
9
+ # msg "path", path, "lineno", lineno, "cls", cls, "func", func
10
+ def message(*fields)
11
+ fields.each_slice(2).collect do |field, value|
12
+ "#{field}: #{value}"
13
+ end.join "; "
14
+ end
59
15
 
60
- def test_trim_right_path_four_less
61
- assert_trim_right "ef.t", 6, "ab/cd/ef.t"
62
- end
16
+ def assert_instance_variable expected, obj, name
17
+ val = obj.instance_eval name
18
+ assert_equal expected, val, "name: #{name}; expected: #{expected}; result: #{val}"
19
+ val
20
+ end
63
21
 
64
- def test_trim_right_path_five_less
65
- assert_trim_right "ef.t", 5, "ab/cd/ef.t"
66
- end
22
+ def test_default_values
23
+ fmt = Logue::Format.new
24
+ assert_instance_variable Logue::FormatWidths::DEFAULT_FILENAME, fmt, "@file_width"
25
+ assert_instance_variable Logue::FormatWidths::DEFAULT_LINENUM, fmt, "@line_width"
26
+ assert_instance_variable Logue::FormatWidths::DEFAULT_FUNCTION, fmt, "@method_width"
27
+ assert_instance_variable true, fmt, "@trim"
28
+ end
67
29
 
68
- def test_trim_right_path_six_less
69
- assert_trim_right "ef.t", 4, "ab/cd/ef.t"
70
- end
30
+ def assert_format expected, path, lineno, cls, func
31
+ msg = message "path", path, "lineno", lineno, "cls", cls, "func", func
32
+ fmt = Logue::Format.new
33
+ fmt.format path, lineno, cls, func
34
+ result = fmt.format path, lineno, cls, func
35
+ assert_equal expected, result, msg
36
+ end
71
37
 
72
- def test_trim_right_path_seven_less
73
- assert_trim_right "ef.t", 3, "ab/cd/ef.t"
74
- end
38
+ def test_write
39
+ path = "/a/long/path/to/the/directory/abc.t"
40
+ lineno = 1
41
+ func = "block (2 levels) in one"
42
+ cls = nil
43
+ expected = "[.../the/directory/abc.t : 1] {block (2 levels) in }"
44
+ assert_format expected, path, lineno, cls, func
45
+ end
75
46
 
76
- def test_trim_right_path_eight_less
77
- assert_trim_right "ef.t", 2, "ab/cd/ef.t"
78
- end
47
+ def test_copy
48
+ fmt = Logue::Format.new line_width: 77
49
+ val = fmt.instance_eval "@line_width"
50
+ assert_equal 77, val
51
+
52
+ copy = fmt.copy method_width: 123
53
+ val = copy.instance_eval "@line_width"
54
+ assert_equal 77, val
55
+
56
+ val = copy.instance_eval "@method_width"
57
+ assert_equal 123, val
79
58
  end
80
59
  end