logue 1.0.5 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 385cb8613ee04140fab6eacde15fa7ed4cb67ba1
4
- data.tar.gz: e1e1649d69e92e3440dd02e43605debf063c72be
3
+ metadata.gz: a3d4405454050091913dfa971aeb278078d431b1
4
+ data.tar.gz: 8a07f226220c9829f09f62da75a3fad08ac12f0f
5
5
  SHA512:
6
- metadata.gz: 2c8ba5bd3f9f039a49eba59cb4d41cb929adfa6aa886d93044039abc8824bc03d73e4be0cbc053432dfe915d867ce19eebbc8474bc0dc11aa61984984450c853
7
- data.tar.gz: 07d5016f53177fe85a744bc6c588d10da300856bdc43db0d2c1183a41ee01fa0b7a058e25a5fbcddace3565d948aff3c14fe0c55f6247e5d728dde429e3633d4
6
+ metadata.gz: db149d129f19bc569a6dce1cb945741821bdcc7ab5c89511463cd4f7f3cfcd8e55b135aa3f8c71a28341bd8af6f4fa319ce83c8970e35fb0f2287ea2848a0794
7
+ data.tar.gz: 50aeb6028db277427a7ae453205df31d61148bfcd0e5478cf126d65ab0d64f5e065294005fc196bf45f035df3702b6fadb5d59477cd5e34b4cb3e3306d7fdf28
data/lib/logue/format.rb CHANGED
@@ -1,52 +1,38 @@
1
1
  #!/usr/bin/ruby -w
2
2
  # -*- ruby -*-
3
3
 
4
- module Logue
5
- class Format
6
-
7
- def trim_left str, maxlen
8
- str[0 ... maxlen.to_i.abs]
9
- end
10
-
11
- def trim_right str, maxlen
12
- mxln = maxlen.abs
13
-
14
- if str.length > mxln
15
- trim_path_right str, maxlen
16
- else
17
- str
18
- end
19
- end
4
+ require 'logue/pathutil'
20
5
 
21
- def trim_path_right path, maxlen
22
- mxln = maxlen.abs
23
-
24
- comps = path.split "/"
25
- str = comps.pop
26
- comps.reverse.each do |comp|
27
- newstr = comp + "/" + str
28
- if newstr.length + 4 <= mxln
29
- str = newstr
30
- else
31
- newstr = "..." + "/" + str
32
- if newstr.length <= mxln
33
- str = newstr
34
- end
35
- break
36
- end
37
- end
38
- str
39
- end
6
+ class Logue::FormatWidths
7
+ DEFAULT_FILENAME = -25
8
+ DEFAULT_LINENUM = 4
9
+ DEFAULT_FUNCTION = -20
10
+ end
40
11
 
41
- def print_formatted file, line, func, msg, lvl, &blk
42
- if trim
43
- file = trim_right file, @file_width
44
- line = trim_left line, @line_width
45
- func = trim_left func, @function_width
46
- end
12
+ class Logue::Format
13
+ def initialize args = Hash.new
14
+ @file_width = args.fetch :file_width, Logue::FormatWidths::DEFAULT_FILENAME
15
+ @line_width = args.fetch :line_width, Logue::FormatWidths::DEFAULT_LINENUM
16
+ @method_width = args.fetch :method_width, Logue::FormatWidths::DEFAULT_FUNCTION
17
+ @trim = args.fetch :trim, true
18
+ end
47
19
 
48
- hdr = sprintf @format, file, line, func
49
- print hdr, msg, lvl, &blk
20
+ def copy args
21
+ values = { file_width: @file_width, line_width: @line_width, method_width: @method_width, trim: @trim }
22
+ self.class.new values.merge(args)
23
+ end
24
+
25
+ def format path, lineno, cls, func
26
+ if cls
27
+ func = cls.to_s + "#" + func
50
28
  end
29
+
30
+ if @trim
31
+ path = Logue::PathUtil.trim_right path, @file_width
32
+ func = Logue::PathUtil.trim_left func, @method_width
33
+ end
34
+
35
+ format = "[%#{@file_width}s:%#{@line_width}d] {%#{@method_width}s}"
36
+ sprintf format, path, lineno, func
51
37
  end
52
38
  end
data/lib/logue/log.rb CHANGED
@@ -5,14 +5,13 @@
5
5
  #
6
6
  # Logging Module
7
7
  #
8
- # Author:: Jeff Pace <jeugenepace@gmail.com>
8
+ # Author:: Jeff Pace
9
9
  # Documentation:: Author
10
10
  #
11
11
 
12
12
  require 'logue/logger'
13
13
  require 'logue/severity'
14
14
  require 'logue/colors'
15
- require 'rainbow'
16
15
 
17
16
  #
18
17
  # == Log
@@ -37,7 +36,7 @@ require 'rainbow'
37
36
  # That will simply log the given message.
38
37
  #
39
38
  # class YourClass
40
- # include Loggable
39
+ # include Logue::Loggable
41
40
  #
42
41
  # def some_method(...)
43
42
  # log "my message"
@@ -55,10 +54,6 @@ module Logue
55
54
  class Log
56
55
  $LOGGING_LEVEL = nil
57
56
 
58
- DEFAULT_FILENAME_WIDTH = -25
59
- DEFAULT_LINENUM_WIDTH = 4
60
- DEFAULT_FUNCTION_WIDTH = -20
61
-
62
57
  include Log::Severity
63
58
 
64
59
  # by default, class methods delegate to a single app-wide log.
@@ -71,134 +66,78 @@ module Logue
71
66
  @@log
72
67
  end
73
68
 
69
+ def self.accessors methname
70
+ [ methname.to_sym, (methname.to_s + "=").to_sym ]
71
+ end
72
+
73
+ def self.logger_delegated? meth
74
+ @@logger_delegated ||= Array.new.tap do |ary|
75
+ acc_methods = [
76
+ :colorize_line,
77
+ :format,
78
+ :level,
79
+ :outfile,
80
+ :output,
81
+ :quiet,
82
+ :verbose,
83
+ ]
84
+ ary.concat acc_methods.collect { |am| accessors(am) }.flatten!
85
+ read_methods = [
86
+ :ignore_class,
87
+ :ignore_file,
88
+ :ignore_method,
89
+ :log_class,
90
+ :log_file,
91
+ :log_method,
92
+ :set_color,
93
+ :set_default_widths,
94
+ :set_widths,
95
+ ]
96
+ ary.concat read_methods
97
+ end
98
+ @@logger_delegated.include? meth
99
+ end
100
+
74
101
  def self.method_missing meth, *args, &blk
75
- # only handling foregrounds, not backgrounds
76
- if code = Colors::valid_colors[meth]
77
- add_color_method meth.to_s, code + 30
78
- send meth, *args, &blk
102
+ if logger_delegated? meth
103
+ logger.send meth, *args, &blk
104
+ elsif Colors::valid_colors[meth]
105
+ # only handling foregrounds, not backgrounds
106
+ logger.send meth, *args, &blk
79
107
  else
80
108
  super
81
109
  end
82
110
  end
83
111
 
84
112
  def self.respond_to? meth
85
- Colors::valid_colors.include?(meth) || super
113
+ logger_delegated?(meth) || Colors::valid_colors.include?(meth) || super
86
114
  end
87
115
 
88
116
  def self.add_color_method color, code
89
117
  instmeth = Array.new
90
118
  instmeth << "def #{color} msg = \"\", lvl = Log::DEBUG, depth = 1, cname = nil, &blk"
91
- instmeth << " log(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
119
+ instmeth << " logger.#{color} (\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
92
120
  instmeth << "end"
93
- instance_eval instmeth.join("\n")
94
-
95
- clsmeth = Array.new
96
- clsmeth << "def #{color} msg = \"\", lvl = Log::DEBUG, depth = 1, cname = nil, &blk"
97
- clsmeth << " logger.#{color}(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
98
- clsmeth << "end"
99
-
100
- class_eval clsmeth.join("\n")
101
- end
102
-
103
- def self.set_default_widths
104
- logger.set_default_widths
105
- end
106
-
107
- def self.verbose
108
- logger.verbose
109
- end
110
-
111
- def self.verbose= v
112
- logger.verbose = v && v != 0 ? DEBUG : FATAL
113
- end
114
-
115
- def self.level= lvl
116
- logger.level = lvl
117
- end
118
-
119
- def self.quiet
120
- logger.quiet
121
- end
122
-
123
- def self.quiet= q
124
- logger.quiet = q
125
- end
126
-
127
- def self.format
128
- logger.format
129
- end
130
-
131
- def self.format= fmt
132
- logger.format = fmt
133
- end
134
-
135
- # Assigns output to the given stream.
136
- def self.output= io
137
- logger.output = io
138
- end
139
-
140
- def self.output
141
- logger.output
142
- end
143
-
144
- # sets whether to colorize the entire line, or just the message.
145
- def self.colorize_line= col
146
- logger.colorize_line = col
147
- end
148
-
149
- def self.colorize_line
150
- logger.colorize_line
151
- end
152
-
153
- # Assigns output to a file with the given name. Returns the file; client
154
- # is responsible for closing it.
155
- def self.outfile= fname
156
- logger.outfile = fname
157
- end
158
-
159
- def self.outfile
160
- logger.outfile
121
+
122
+ # an instance, but on the class object, not the log instance:
123
+ self.instance_eval instmeth.join("\n")
161
124
  end
162
125
 
163
126
  # Creates a printf format for the given widths, for aligning output.
164
127
  def self.set_widths file_width, line_width, func_width
165
128
  logger.set_widths file_width, line_width, func_width
166
129
  end
167
-
168
- def self.ignore_file fname
169
- logger.ignore_file fname
170
- end
171
130
 
172
- def self.ignore_method methname
173
- logger.ignored_method methname
174
- end
175
-
176
- def self.ignore_class classname
177
- logger.ignored_class classname
178
- end
179
-
180
- def self.log_file fname
181
- logger.log_file fname
182
- end
183
-
184
- def self.log_method methname
185
- logger.log_method methname
186
- end
187
-
188
- def self.log_class classname
189
- logger.log_class classname
190
- end
191
-
192
- def self.debug msg = "", depth = 1, &blk
193
- logger.log msg, DEBUG, depth + 1, &blk
131
+ def self.debug msg = "", depth = 1, cname = nil, &blk
132
+ logger.debug msg, depth + 1, cname, &blk
194
133
  end
195
134
 
196
- def self.info msg = "", depth = 1, &blk
197
- logger.log msg, INFO, depth + 1, &blk
135
+ def self.info msg = "", depth = 1, cname = nil, &blk
136
+ logger.info msg, depth + 1, cname, &blk
198
137
  end
199
138
 
200
- def self.fatal msg = "", depth = 1, &blk
201
- logger.log msg, FATAL, depth + 1, &blk
139
+ def self.fatal msg = "", depth = 1, cname = nil, &blk
140
+ logger.fatal msg, depth + 1, cname, &blk
202
141
  end
203
142
 
204
143
  def self.log msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
@@ -209,17 +148,17 @@ module Logue
209
148
  logger.stack msg, lvl, depth + 1, cname, &blk
210
149
  end
211
150
 
212
- def self.warn msg = "", depth = 1, &blk
151
+ def self.warn msg = "", depth = 1, cname = nil, &blk
213
152
  if verbose
214
- logger.log msg, WARN, depth + 1, &blk
153
+ logger.log msg, WARN, depth + 1, cname, &blk
215
154
  else
216
155
  $stderr.puts "WARNING: " + msg
217
156
  end
218
157
  end
219
158
 
220
- def self.error msg = "", depth = 1, &blk
159
+ def self.error msg = "", depth = 1, cname = nil, &blk
221
160
  if verbose
222
- logger.log msg, ERROR, depth + 1, &blk
161
+ logger.log msg, ERROR, depth + 1, cname, &blk
223
162
  else
224
163
  $stderr.puts "ERROR: " + msg
225
164
  end
@@ -234,9 +173,5 @@ module Logue
234
173
  $stderr.puts msg
235
174
  end
236
175
  end
237
-
238
- def self.set_color lvl, color
239
- logger.set_color lvl, color
240
- end
241
176
  end
242
177
  end
@@ -24,7 +24,7 @@ require 'logue/colors'
24
24
  # == Usage
25
25
  #
26
26
  # class YourClass
27
- # include Loggable
27
+ # include Logue::Loggable
28
28
  #
29
29
  # def some_method(...)
30
30
  # log "my message"
@@ -42,35 +42,35 @@ module Logue
42
42
  module Loggable
43
43
  # Logs the given message, including the class whence invoked.
44
44
  def log msg = "", lvl = Log::DEBUG, depth = 1, &blk
45
- Log.log msg, lvl, depth + 1, self.class.to_s, &blk
45
+ delegate_log_class.log msg, lvl, depth + 1, self.class.to_s, &blk
46
46
  end
47
47
 
48
48
  def debug msg = "", depth = 1, &blk
49
- Log.log msg, Log::DEBUG, depth + 1, self.class.to_s, &blk
49
+ delegate_log_class.debug msg, depth + 1, self.class.to_s, &blk
50
50
  end
51
51
 
52
52
  def info msg = "", depth = 1, &blk
53
- Log.log msg, Log::INFO, depth + 1, self.class.to_s, &blk
53
+ delegate_log_class.info msg, depth + 1, self.class.to_s, &blk
54
54
  end
55
55
 
56
56
  def warn msg = "", depth = 1, &blk
57
- Log.log msg, Log::WARN, depth + 1, self.class.to_s, &blk
57
+ delegate_log_class.warn msg, depth + 1, self.class.to_s, &blk
58
58
  end
59
59
 
60
60
  def error msg = "", depth = 1, &blk
61
- Log.log msg, Log::ERROR, depth + 1, self.class.to_s, &blk
61
+ delegate_log_class.error msg, depth + 1, self.class.to_s, &blk
62
62
  end
63
63
 
64
64
  def fatal msg = "", depth = 1, &blk
65
- Log.log msg, Log::FATAL, depth + 1, self.class.to_s, &blk
65
+ delegate_log_class.fatal msg, depth + 1, self.class.to_s, &blk
66
66
  end
67
67
 
68
68
  def stack msg = "", lvl = Log::DEBUG, depth = 1, &blk
69
- Log.stack msg, lvl, depth + 1, self.class.to_s, &blk
69
+ delegate_log_class.stack msg, lvl, depth + 1, self.class.to_s, &blk
70
70
  end
71
71
 
72
72
  def write msg = "", depth = 1, &blk
73
- Log.write msg, depth + 1, self.class.to_s, &blk
73
+ delegate_log_class.write msg, depth + 1, self.class.to_s, &blk
74
74
  end
75
75
 
76
76
  def method_missing meth, *args, &blk
@@ -94,5 +94,9 @@ module Logue
94
94
  meth << "end"
95
95
  self.class.module_eval meth.join("\n")
96
96
  end
97
+
98
+ def delegate_log_class
99
+ Log
100
+ end
97
101
  end
98
102
  end