logue 1.0.11 → 1.0.13

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: be0ec7812a60a739464699d8c086904cfd5e4b80
4
- data.tar.gz: 1fc954e903970d9c00b5a5a5750f3c6919d04d86
3
+ metadata.gz: 27c5cf726353cfb91ce2c443c8cd02f5a8c0e272
4
+ data.tar.gz: 02125714a6032756c58acff5522fe104748bd270
5
5
  SHA512:
6
- metadata.gz: c78f24d2d9627010644a85b84f358cb96b09fb8f14cdbe2f3fa818e7550e0c818c760a31791c81a11c7629791a50557cd4e737965c2d96c6f7036958a1a0a816
7
- data.tar.gz: 632c4304e9c77de1b879b44f85df1d916ad6a196a7931464e83171a8475d72639979ae06c6041bcfd23b0c5a33ee4156b687102cbb16e21d3883920d43ab5d34
6
+ metadata.gz: 43947a643388fa154feb99be7c02a3a33dcc701e75e256738f97911bdd982ecb23a236922e8db4bd4693b43eb46a28b56e14313221da001bbeb95b79da67d4a5
7
+ data.tar.gz: f3aa71bcd285189271ec1a5d48463c79c247d2b57ee58fece9ae4f483ae9f7f69621e103ef5f803d639a7678f3014179d82038067f78649d7cecc89b11799636
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ *~
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- logue (1.0.11)
4
+ logue (1.0.13)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,9 +1,8 @@
1
1
  logue
2
2
  =====
3
3
 
4
- A Ruby gem for generating logging and debugging output. Logging statements
5
- include the file, line, class and method from which the logging method was
6
- called.
4
+ A Ruby gem for generating logging and debugging output. Logging statements include the file, line,
5
+ class and method from which the logging method was called.
7
6
 
8
7
  ## EXAMPLES
9
8
 
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+ #
4
+ # = colorlog.rb
5
+ #
6
+ # Logging Module
7
+ #
8
+ # Author:: Jeff Pace <jeugenepace@gmail.com>
9
+ # Documentation:: Author
10
+ #
11
+
12
+ require 'rainbow/x11_color_names'
13
+ require 'rainbow/color'
14
+ require 'logue/level'
15
+
16
+ #
17
+ # == ColorLog
18
+ #
19
+ # This class logs messages using terminal colors, forwarding them to a log method.
20
+ #
21
+ # == Examples
22
+ #
23
+ # See the unit tests in colorlog_test.rb
24
+ #
25
+
26
+ module Logue
27
+ module ColorLog
28
+ def method_missing meth, *args, &blk
29
+ # validcolors = Rainbow::X11ColorNames::NAMES
30
+ validcolors = Rainbow::Color::Named::NAMES
31
+ if code = validcolors[meth]
32
+ add_color_method meth.to_s, code + 30
33
+ send meth, *args, &blk
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ def respond_to? meth
40
+ # validcolors = Rainbow::X11ColorNames::NAMES
41
+ validcolors = Rainbow::Color::Named::NAMES
42
+ validcolors.include?(meth) || super
43
+ end
44
+
45
+ def add_color_method color, code
46
+ instmeth = Array.new.tap do |a|
47
+ a << 'def ' + color.to_s + '(msg = "", lvl = Logue::Level::DEBUG, cname = nil, &blk)'
48
+ a << ' log("\e[' + code.to_s + 'm#{msg}\e[0m", lvl, cname, &blk)'
49
+ a << 'end'
50
+ end
51
+ instance_eval instmeth.join "\n"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'pathname'
5
+
6
+ module Logue
7
+ class Filter
8
+ include Comparable
9
+
10
+ attr_reader :ignored_files
11
+ attr_reader :ignored_methods
12
+ attr_reader :ignored_classes
13
+
14
+ def initialize ignored_files: Array.new, ignored_methods: Array.new, ignored_classes: Array.new
15
+ @ignored_files = ignored_files
16
+ @ignored_methods = ignored_methods
17
+ @ignored_classes = ignored_classes
18
+ end
19
+
20
+ def ignore_file file
21
+ @ignored_files << file
22
+ end
23
+
24
+ def log_file file
25
+ @ignored_files.delete file
26
+ end
27
+
28
+ def ignore_class cls
29
+ @ignored_classes << cls
30
+ end
31
+
32
+ def log_class cls
33
+ @ignored_classes.delete cls
34
+ end
35
+
36
+ def ignore_method meth
37
+ @ignored_methods << meth
38
+ end
39
+
40
+ def log_method meth
41
+ @ignored_methods.delete meth
42
+ end
43
+
44
+ def log? file, cls, meth
45
+ !@ignored_files.include?(file) && !@ignored_classes.include?(cls) && !@ignored_methods.include?(meth)
46
+ end
47
+
48
+ def compare_fields
49
+ [ :ignored_files, :ignored_methods, :ignored_classes ]
50
+ end
51
+
52
+ def <=> other
53
+ compare_fields.each do |field|
54
+ cmp = send(field) <=> other.send(field)
55
+ return cmp if cmp.nonzero?
56
+ end
57
+ 0
58
+ end
59
+ end
60
+ end
data/lib/logue/frame.rb CHANGED
@@ -9,20 +9,29 @@ module Logue
9
9
 
10
10
  attr_reader :path
11
11
  attr_reader :line
12
- attr_reader :function
12
+ attr_reader :method
13
13
 
14
- def initialize args
15
- if entry = args[:entry]
14
+ def initialize entry: nil, path: nil, line: nil, method: nil
15
+ # entry if called from "caller(x)" elements, path/line/method if called from
16
+ # "caller_location(x)" elements.
17
+ if entry
16
18
  md = FRAME_RE.match entry
17
- # Ruby 1.9 expands the file name, but 1.8 doesn't:
18
- @path = Pathname.new(md[1]).expand_path.to_s
19
- @line = md[2].to_i
20
- @function = md[3] || ""
19
+ @path = md[1]
20
+ @line = md[2].to_i
21
+ @method = md[3] || ""
21
22
  else
22
- @path = args[:path]
23
- @line = args[:line]
24
- @function = args[:function]
23
+ @path = path
24
+ @line = line
25
+ @method = method
25
26
  end
26
27
  end
28
+
29
+ def to_s
30
+ [ :path, :line, :method ].collect { |field| "#{field}: " + send(field).to_s }.join ", "
31
+ end
32
+
33
+ def formatted format, cname
34
+ format.format @path, @line, cname, @method
35
+ end
27
36
  end
28
37
  end
@@ -0,0 +1,69 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+ #
4
+ # = legacy_logger.rb
5
+ #
6
+ # Logging Module
7
+ #
8
+ # Author:: Jeff Pace <jeugenepace@gmail.com>
9
+ # Documentation:: Author
10
+ #
11
+
12
+ require 'logue/filter'
13
+ require 'logue/location_format'
14
+
15
+ module Logue
16
+ module LegacyLogger
17
+ def trim= what
18
+ $stderr.puts "Logger#trim is deprecated, and ignored"
19
+ end
20
+
21
+ def ignore_file fname
22
+ filter.ignore_file fname
23
+ end
24
+
25
+ def ignore_method methname
26
+ filter.ignore_method methname
27
+ end
28
+
29
+ def ignore_class classname
30
+ filter.ignore_class classname
31
+ end
32
+
33
+ def log_file fname
34
+ filter.log_file fname
35
+ end
36
+
37
+ def log_method methname
38
+ filter.log_method methname
39
+ end
40
+
41
+ def log_class classname
42
+ filter.log_class classname
43
+ end
44
+
45
+ def output
46
+ writer.output
47
+ end
48
+
49
+ def output= obj
50
+ writer.output = obj
51
+ end
52
+
53
+ def colorize_line
54
+ writer.colorize_line
55
+ end
56
+
57
+ def colorize_line= b
58
+ writer.colorize_line = b
59
+ end
60
+
61
+ def set_default_widths
62
+ self.format = LocationFormat.new
63
+ end
64
+
65
+ def set_color lvl, color
66
+ writer.colors[lvl] = color
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+ #
4
+ # = severity.rb
5
+ #
6
+ # Logging Module
7
+ #
8
+ # Author:: Jeff Pace <jeugenepace@gmail.com>
9
+ # Documentation:: Author
10
+ #
11
+
12
+ module Logue
13
+ module Level
14
+ DEBUG = 0
15
+ INFO = 1
16
+ WARN = 2
17
+ ERROR = 3
18
+ FATAL = 4
19
+ end
20
+ end
data/lib/logue/line.rb CHANGED
@@ -4,19 +4,18 @@
4
4
  require 'logue/location_format'
5
5
 
6
6
  module Logue
7
- end
8
-
9
- class Logue::Line
10
- attr_reader :location
11
- attr_reader :msg
12
-
13
- def initialize location, msg
14
- @location = location
15
- @msg = msg
16
- end
7
+ class Line
8
+ attr_reader :location
9
+ attr_reader :msg
10
+
11
+ def initialize location, msg
12
+ @location = location
13
+ @msg = msg
14
+ end
17
15
 
18
- def format locformat
19
- logmsg = @location.format locformat
20
- logmsg + " " + @msg
16
+ def format locformat
17
+ logmsg = @location.format locformat
18
+ logmsg + " " + @msg
19
+ end
21
20
  end
22
21
  end
@@ -4,22 +4,21 @@
4
4
  require 'logue/location_format'
5
5
 
6
6
  module Logue
7
- end
8
-
9
- class Logue::Location
10
- attr_reader :path
11
- attr_reader :lineno
12
- attr_reader :cls
13
- attr_reader :function
14
-
15
- def initialize path, lineno, cls, function
16
- @path = path
17
- @lineno = lineno
18
- @cls = cls
19
- @function = function
20
- end
7
+ class Location
8
+ attr_reader :path
9
+ attr_reader :lineno
10
+ attr_reader :cls
11
+ attr_reader :method
12
+
13
+ def initialize path, lineno, cls, method
14
+ @path = path
15
+ @lineno = lineno
16
+ @cls = cls
17
+ @method = method
18
+ end
21
19
 
22
- def format locformat
23
- locformat.format @path, @lineno, @cls, @function
20
+ def format locformat
21
+ locformat.format @path, @lineno, @cls, @method
22
+ end
24
23
  end
25
24
  end
@@ -3,45 +3,46 @@
3
3
 
4
4
  require 'logue/pathutil'
5
5
 
6
- class Logue::LocationFormatWidths
7
- DEFAULT_FILENAME = -25
8
- DEFAULT_LINE = 4
9
- DEFAULT_FUNCTION = -20
10
- end
11
-
12
- class Logue::LocationFormat
13
- attr_accessor :file
14
- attr_accessor :line
15
- attr_accessor :function
16
- attr_accessor :trim
17
-
18
- def initialize args = Hash.new
19
- @file = args.fetch :file, Logue::LocationFormatWidths::DEFAULT_FILENAME
20
- @line = args.fetch :line, Logue::LocationFormatWidths::DEFAULT_LINE
21
- @function = args.fetch :function, Logue::LocationFormatWidths::DEFAULT_FUNCTION
22
- @trim = args.fetch :trim, true
23
- end
24
-
25
- def copy args
26
- values = { file: @file, line: @line, function: @function, trim: @trim }
27
- self.class.new values.merge(args)
28
- end
29
-
30
- def format path, line, cls, func
31
- if cls
32
- func = cls.to_s + "#" + func
6
+ module Logue
7
+ class LocationFormat
8
+ module Defaults
9
+ FILENAME = -25
10
+ LINE = 4
11
+ METHOD = -20
33
12
  end
34
13
 
35
- if trim
36
- path = Logue::PathUtil.trim_right path, @file
37
- func = Logue::PathUtil.trim_left func, @function
14
+ attr_accessor :file
15
+ attr_accessor :line
16
+ attr_accessor :method
17
+ attr_accessor :trim
18
+
19
+ def initialize file: Defaults::FILENAME, line: Defaults::LINE, method: Defaults::METHOD, trim: true
20
+ @file = file
21
+ @line = line
22
+ @method = method
23
+ @trim = trim
24
+ end
25
+
26
+ def copy args
27
+ values = { file: @file, line: @line, method: @method, trim: @trim }
28
+ self.class.new values.merge(args)
38
29
  end
39
30
 
40
- format = "[%#{file}s:%#{@line}d] {%#{function}s}"
41
- sprintf format, path, line, func
42
- end
31
+ def format path, line, cls, func
32
+ if cls
33
+ func = cls.to_s + "#" + func
34
+ end
35
+
36
+ if trim
37
+ path = PathUtil.trim_right path.to_s, @file
38
+ func = PathUtil.trim_left func, @method
39
+ end
40
+
41
+ sprintf format_string, path, line, func
42
+ end
43
43
 
44
- def format_string
45
- "[%#{file}s:%#{line}d] {%#{function}s}"
44
+ def format_string
45
+ "[%#{@file}s:%#{@line}d] {%#{@method}s}"
46
+ end
46
47
  end
47
48
  end
data/lib/logue/log.rb CHANGED
@@ -10,7 +10,7 @@
10
10
  #
11
11
 
12
12
  require 'logue/logger'
13
- require 'logue/severity'
13
+ require 'logue/level'
14
14
  require 'logue/colors'
15
15
 
16
16
  #
@@ -54,7 +54,7 @@ module Logue
54
54
  class Log
55
55
  $LOGGING_LEVEL = nil
56
56
 
57
- include Log::Severity
57
+ include Level
58
58
 
59
59
  # by default, class methods delegate to a single app-wide log.
60
60
 
@@ -115,8 +115,8 @@ module Logue
115
115
 
116
116
  def self.add_color_method color, code
117
117
  instmeth = Array.new
118
- instmeth << "def #{color} msg = \"\", lvl = Log::DEBUG, depth = 1, cname = nil, &blk"
119
- instmeth << " logger.#{color} (\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
118
+ instmeth << "def #{color} msg = \"\", lvl = Log::DEBUG, cname = nil, &blk"
119
+ instmeth << " logger.#{color} (\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, cname, &blk)"
120
120
  instmeth << "end"
121
121
 
122
122
  # an instance, but on the class object, not the log instance:
@@ -128,45 +128,45 @@ module Logue
128
128
  logger.set_widths file_width, line_width, func_width
129
129
  end
130
130
 
131
- def self.debug msg = "", depth = 1, cname = nil, &blk
132
- logger.debug msg, depth + 1, cname, &blk
131
+ def self.debug msg = "", cname = nil, &blk
132
+ logger.debug msg, cname, &blk
133
133
  end
134
134
 
135
- def self.info msg = "", depth = 1, cname = nil, &blk
136
- logger.info msg, depth + 1, cname, &blk
135
+ def self.info msg = "", cname = nil, &blk
136
+ logger.info msg, cname, &blk
137
137
  end
138
138
 
139
- def self.fatal msg = "", depth = 1, cname = nil, &blk
140
- logger.fatal msg, depth + 1, cname, &blk
139
+ def self.fatal msg = "", cname = nil, &blk
140
+ logger.fatal msg, cname, &blk
141
141
  end
142
142
 
143
- def self.log msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
144
- logger.log msg, lvl, depth + 1, cname, &blk
143
+ def self.log msg = "", lvl = DEBUG, cname = nil, &blk
144
+ logger.log msg, lvl, cname, &blk
145
145
  end
146
146
 
147
- def self.stack msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
148
- logger.stack msg, lvl, depth + 1, cname, &blk
147
+ def self.stack msg = "", lvl = DEBUG, cname = nil, &blk
148
+ logger.stack msg, lvl, cname, &blk
149
149
  end
150
150
 
151
- def self.warn msg = "", depth = 1, cname = nil, &blk
151
+ def self.warn msg = "", cname = nil, &blk
152
152
  if verbose
153
- logger.log msg, WARN, depth + 1, cname, &blk
153
+ logger.log msg, WARN, cname, &blk
154
154
  else
155
155
  $stderr.puts "WARNING: " + msg
156
156
  end
157
157
  end
158
158
 
159
- def self.error msg = "", depth = 1, cname = nil, &blk
159
+ def self.error msg = "", cname = nil, &blk
160
160
  if verbose
161
- logger.log msg, ERROR, depth + 1, cname, &blk
161
+ logger.log msg, ERROR, cname, &blk
162
162
  else
163
163
  $stderr.puts "ERROR: " + msg
164
164
  end
165
165
  end
166
166
 
167
- def self.write msg, depth = 1, cname = nil, &blk
167
+ def self.write msg, cname = nil, &blk
168
168
  if verbose
169
- stack msg, Log::WARN, depth + 1, cname, &blk
169
+ stack msg, Log::WARN, cname, &blk
170
170
  elsif quiet
171
171
  # nothing
172
172
  else
@@ -41,36 +41,36 @@ require 'logue/colors'
41
41
  module Logue
42
42
  module Loggable
43
43
  # Logs the given message, including the class whence invoked.
44
- def log msg = "", lvl = Log::DEBUG, depth = 1, &blk
45
- delegate_log_class.log msg, lvl, depth + 1, self.class.to_s, &blk
44
+ def log msg = "", lvl = Log::DEBUG, &blk
45
+ delegate_log_class.log msg, lvl, self.class.to_s, &blk
46
46
  end
47
47
 
48
- def debug msg = "", depth = 1, &blk
49
- delegate_log_class.debug msg, depth + 1, self.class.to_s, &blk
48
+ def debug msg = "", &blk
49
+ delegate_log_class.debug msg, self.class.to_s, &blk
50
50
  end
51
51
 
52
- def info msg = "", depth = 1, &blk
53
- delegate_log_class.info msg, depth + 1, self.class.to_s, &blk
52
+ def info msg = "", &blk
53
+ delegate_log_class.info msg, self.class.to_s, &blk
54
54
  end
55
55
 
56
- def warn msg = "", depth = 1, &blk
57
- delegate_log_class.warn msg, depth + 1, self.class.to_s, &blk
56
+ def warn msg = "", &blk
57
+ delegate_log_class.warn msg, self.class.to_s, &blk
58
58
  end
59
59
 
60
- def error msg = "", depth = 1, &blk
61
- delegate_log_class.error msg, depth + 1, self.class.to_s, &blk
60
+ def error msg = "", &blk
61
+ delegate_log_class.error msg, self.class.to_s, &blk
62
62
  end
63
63
 
64
- def fatal msg = "", depth = 1, &blk
65
- delegate_log_class.fatal msg, depth + 1, self.class.to_s, &blk
64
+ def fatal msg = "", &blk
65
+ delegate_log_class.fatal msg, self.class.to_s, &blk
66
66
  end
67
67
 
68
- def stack msg = "", lvl = Log::DEBUG, depth = 1, &blk
69
- delegate_log_class.stack msg, lvl, depth + 1, self.class.to_s, &blk
68
+ def stack msg = "", lvl = Log::DEBUG, &blk
69
+ delegate_log_class.stack msg, lvl, self.class.to_s, &blk
70
70
  end
71
71
 
72
- def write msg = "", depth = 1, &blk
73
- delegate_log_class.write msg, depth + 1, self.class.to_s, &blk
72
+ def write msg = "", &blk
73
+ delegate_log_class.write msg, self.class.to_s, &blk
74
74
  end
75
75
 
76
76
  def method_missing meth, *args, &blk
@@ -89,8 +89,8 @@ module Logue
89
89
 
90
90
  def add_color_method color, code
91
91
  meth = Array.new
92
- meth << "def #{color}(msg = \"\", lvl = Log::DEBUG, depth = 1, cname = nil, &blk)"
93
- meth << " Log.#{color} msg, lvl, depth + 1, self.class.to_s, &blk"
92
+ meth << "def #{color}(msg = \"\", lvl = Log::DEBUG, cname = nil, &blk)"
93
+ meth << " Log.#{color} msg, lvl, self.class.to_s, &blk"
94
94
  meth << "end"
95
95
  self.class.module_eval meth.join("\n")
96
96
  end
data/lib/logue/logger.rb CHANGED
@@ -9,13 +9,12 @@
9
9
  # Documentation:: Author
10
10
  #
11
11
 
12
- require 'rainbow/x11_color_names'
13
- require 'rainbow/color'
14
- require 'pathname'
15
- require 'logue/severity'
16
- require 'logue/location_format'
17
- require 'logue/pathutil'
18
- require 'logue/frame'
12
+ require 'logue/level'
13
+ require 'logue/colorlog'
14
+ require 'logue/writer'
15
+ require 'logue/filter'
16
+ require 'logue/legacy_logger'
17
+ require 'logue/stack'
19
18
 
20
19
  #
21
20
  # == Logger
@@ -29,220 +28,114 @@ require 'logue/frame'
29
28
  #
30
29
 
31
30
  module Logue
32
- end
33
-
34
- class Logue::Logger
35
- attr_accessor :output
36
- attr_accessor :colorize_line
37
- attr_accessor :level
38
- attr_accessor :ignored_files
39
- attr_accessor :ignored_methods
40
- attr_accessor :ignored_classes
41
-
42
- attr_accessor :format
43
-
44
- include Logue::Log::Severity
45
-
46
- def initialize
47
- reset
48
- end
49
-
50
- def verbose= v
51
- @level = case v
52
- when TrueClass
53
- DEBUG
54
- when FalseClass
55
- FATAL
56
- when Integer
57
- v
58
- end
59
- end
60
-
61
- def reset
62
- @level = FATAL
63
- @ignored_files = Hash.new
64
- @ignored_methods = Hash.new
65
- @ignored_classes = Hash.new
66
- @output = $stdout
67
- @colors = Array.new
68
- @colorize_line = false
69
- @format = Logue::LocationFormat.new
70
- end
71
-
72
- def trim= what
73
- end
74
-
75
- def verbose
76
- level <= DEBUG
77
- end
78
-
79
- def set_default_widths
80
- @format = Logue::LocationFormat.new
81
- end
82
-
83
- def verbose
84
- level <= DEBUG
85
- end
86
-
87
- def quiet
88
- level >= WARN
89
- end
90
-
91
- def quiet= b
92
- level = b ? WARN : DEBUG
93
- end
94
-
95
- # Assigns output to a file with the given name. Returns the file; the client is responsible for
96
- # closing it.
97
- def outfile= f
98
- @output = f.kind_of?(IO) ? f : File.new(f, "w")
99
- end
100
-
101
- # Creates a printf format for the given widths, for aligning output. To lead lines with zeros
102
- # (e.g., "00317") the line_width argument must be a string, not an integer.
103
- def set_widths file, line, function
104
- @format = Logue::LocationFormat.new file: file, line: line, function: function
105
- end
31
+ class Logger
32
+ include LegacyLogger
33
+ include ColorLog
34
+
35
+ attr_accessor :level
36
+ attr_accessor :format
37
+ attr_accessor :filter
38
+ attr_accessor :writer
39
+
40
+ include Level
106
41
 
107
- def ignore_file fname
108
- ignored_files[fname] = true
109
- end
110
-
111
- def ignore_method methname
112
- ignored_methods[methname] = true
113
- end
114
-
115
- def ignore_class classname
116
- ignored_classes[classname] = true
117
- end
42
+ def initialize format: LocationFormat.new, level: WARN, filter: Filter.new, writer: Writer.new
43
+ reset format: format, level: level, filter: filter, writer: writer
44
+ end
45
+
46
+ def verbose= v
47
+ @level = case v
48
+ when TrueClass
49
+ DEBUG
50
+ when FalseClass
51
+ FATAL
52
+ when Integer
53
+ v
54
+ end
55
+ end
118
56
 
119
- def log_file fname
120
- ignored_files.delete fname
121
- end
122
-
123
- def log_method methname
124
- ignored_methods.delete methname
125
- end
126
-
127
- def log_class classname
128
- ignored_classes.delete classname
129
- end
57
+ def verbose
58
+ @level <= DEBUG
59
+ end
130
60
 
131
- def debug msg = "", depth = 1, cname = nil, &blk
132
- log msg, DEBUG, depth + 1, cname, &blk
133
- end
61
+ def reset format: LocationFormat.new, level: FATAL, filter: Filter.new, writer: Writer.new
62
+ @level = level
63
+ @filter = filter
64
+ @format = format
65
+ @writer = writer
66
+ end
67
+
68
+ def quiet
69
+ @level >= WARN
70
+ end
134
71
 
135
- def info msg = "", depth = 1, cname = nil, &blk
136
- log msg, INFO, depth + 1, cname, &blk
137
- end
72
+ def quiet= b
73
+ @level = b ? WARN : DEBUG
74
+ end
138
75
 
139
- def warn msg = "", depth = 1, cname = nil, &blk
140
- log msg, WARN, depth + 1, cname, &blk
141
- end
76
+ # Assigns output to a file with the given name. Returns the file; the client is responsible for
77
+ # closing it.
78
+ def outfile= f
79
+ io = f.kind_of?(IO) ? f : File.new(f, "w")
80
+ @writer.output = io
81
+ end
142
82
 
143
- def error msg = "", depth = 1, cname = nil, &blk
144
- log msg, ERROR, depth + 1, cname, &blk
145
- end
83
+ # Creates a printf format for the given widths, for aligning output. To lead lines with zeros
84
+ # (e.g., "00317") the line argument must be a string, with leading zeros, not an integer.
85
+ def set_widths file, line, method
86
+ @format = LocationFormat.new file: file, line: line, method: method
87
+ end
146
88
 
147
- def fatal msg = "", depth = 1, cname = nil, &blk
148
- log msg, FATAL, depth + 1, cname, &blk
149
- end
89
+ def debug msg = "", cname = nil, &blk
90
+ log msg, DEBUG, cname, &blk
91
+ end
150
92
 
151
- # Logs the given message.
152
- def log msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
153
- if lvl >= level
154
- frame = nil
155
-
156
- stk = caller 0
157
- stk.reverse.each_with_index do |frm, idx|
158
- if frm.index(%r{logue/log.*:\d+:in\b})
159
- break
160
- else
161
- frame = frm
162
- end
163
- end
93
+ def info msg = "", cname = nil, &blk
94
+ log msg, INFO, cname, &blk
95
+ end
164
96
 
165
- print_stack_frame frame, cname, msg, lvl, &blk
97
+ def warn msg = "", cname = nil, &blk
98
+ log msg, WARN, cname, &blk
166
99
  end
167
- end
168
100
 
169
- # Shows the current stack.
170
- def stack msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
171
- if lvl >= level
172
- stk = caller depth
173
- stk.each do |frame|
174
- print_stack_frame frame, cname, msg, lvl, &blk
175
- cname = nil
176
- msg = ""
177
- end
101
+ def error msg = "", cname = nil, &blk
102
+ log msg, ERROR, cname, &blk
178
103
  end
179
- end
180
104
 
181
- def print_stack_frame frame, cname, msg, lvl, &blk
182
- frm = Logue::Frame.new entry: frame
183
- func = cname ? cname + "#" + frm.function : frm.function
184
-
185
- unless ignored_files[frm.path] || (cname && ignored_classes[cname]) || ignored_methods[func]
186
- print_formatted(frm.path, frm.line, func, msg, lvl, &blk)
105
+ def fatal msg = "", cname = nil, &blk
106
+ log msg, FATAL, cname, &blk
187
107
  end
188
- end
189
108
 
190
- def print_formatted file, line, func, msg, lvl, &blk
191
- location = @format.format file, line, nil, func
192
- print location, msg, lvl, &blk
193
- end
194
-
195
- def print hdr, msg, lvl, &blk
196
- if blk
197
- x = blk.call
198
- if x.kind_of? String
199
- msg = x
200
- else
201
- return
202
- end
109
+ # Logs the given message.
110
+ def log msg = "", lvl = DEBUG, cname = nil, &blk
111
+ log_frames cname, msg, lvl, 0, &blk
203
112
  end
204
113
 
205
- msg = msg.to_s.chomp
114
+ # Shows the current stack.
115
+ def stack msg = "", lvl = DEBUG, cname = nil, &blk
116
+ log_frames cname, msg, lvl, -1, &blk
117
+ end
206
118
 
207
- if lvlcol = @colors[lvl]
208
- if colorize_line
209
- line = hdr + " " + msg
210
- @output.puts line.color(lvlcol)
211
- else
212
- @output.puts hdr + " " + msg.color(lvlcol)
119
+ def log_frames cname, msg, lvl, num, &blk
120
+ if lvl >= level
121
+ stack = Stack.new
122
+ stack.filtered[0 .. num].each do |frame|
123
+ log_frame frame, cname, msg, lvl, &blk
124
+ cname = nil
125
+ msg = ""
126
+ end
213
127
  end
214
- else
215
- @output.puts hdr + " " + msg
216
- end
217
- end
218
-
219
- def set_color lvl, color
220
- @colors[lvl] = color
221
- end
128
+ end
222
129
 
223
- def method_missing meth, *args, &blk
224
- # validcolors = Rainbow::X11ColorNames::NAMES
225
- validcolors = Rainbow::Color::Named::NAMES
226
- # only handling foregrounds, not backgrounds
227
- if code = validcolors[meth]
228
- add_color_method meth.to_s, code + 30
229
- send meth, *args, &blk
230
- else
231
- super
130
+ def log_frame frame, cname, msg, lvl, &blk
131
+ if @filter.log? frame.path, cname, frame.method
132
+ print_formatted frame, cname, msg, lvl, &blk
133
+ end
232
134
  end
233
- end
234
-
235
- def respond_to? meth
236
- # validcolors = Rainbow::X11ColorNames::NAMES
237
- validcolors = Rainbow::Color::Named::NAMES
238
- validcolors.include?(meth) || super
239
- end
240
135
 
241
- def add_color_method color, code
242
- instmeth = Array.new
243
- instmeth << "def #{color}(msg = \"\", lvl = DEBUG, depth = 1, cname = nil, &blk)"
244
- instmeth << " log(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
245
- instmeth << "end"
246
- instance_eval instmeth.join("\n")
136
+ def print_formatted frame, cname, msg, lvl, &blk
137
+ location = frame.formatted @format, cname
138
+ @writer.print location, msg, level, &blk
139
+ end
247
140
  end
248
141
  end
@@ -2,42 +2,41 @@
2
2
  # -*- ruby -*-
3
3
 
4
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
5
+ class PathUtil
6
+ class << self
7
+ def trim_left str, maxlen
8
+ str[0 ... maxlen.to_i.abs]
9
+ end
12
10
 
13
- def trim_right str, maxlen
14
- mxln = maxlen.abs
11
+ def trim_right str, maxlen
12
+ mxln = maxlen.abs
15
13
 
16
- if str.length > mxln
17
- trim_path_right str, maxlen
18
- else
19
- str
14
+ if str.length > mxln
15
+ trim_path_right str, maxlen
16
+ else
17
+ str
18
+ end
20
19
  end
21
- end
22
20
 
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
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
35
29
  str = newstr
30
+ else
31
+ newstr = "..." + "/" + str
32
+ if newstr.length <= mxln
33
+ str = newstr
34
+ end
35
+ break
36
36
  end
37
- break
38
37
  end
39
- end
40
- str
41
- end
38
+ str
39
+ end
40
+ end
42
41
  end
43
42
  end
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/ruby -w
2
2
  # -*- ruby -*-
3
3
  #
4
- # = severity.rb
4
+ # = level.rb
5
5
  #
6
6
  # Logging Module
7
7
  #
@@ -9,14 +9,13 @@
9
9
  # Documentation:: Author
10
10
  #
11
11
 
12
+ require 'logue/level'
13
+
12
14
  module Logue
13
15
  class Log
16
+ # legacy module that is replaced by Logue::Level
14
17
  module Severity
15
- DEBUG = 0
16
- INFO = 1
17
- WARN = 2
18
- ERROR = 3
19
- FATAL = 4
18
+ include Logue::Level
20
19
  end
21
20
  end
22
21
  end
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'logue/frame'
5
+
6
+ module Logue
7
+ class Stack
8
+ attr_reader :frames
9
+
10
+ def initialize depth: 2
11
+ # caller_locations requires Ruby 2.0+
12
+ locations = caller_locations depth
13
+ @frames = locations.collect do |loc|
14
+ # no absolute_path from "(eval)"
15
+ Frame.new path: loc.absolute_path || loc.path, line: loc.lineno, method: loc.label
16
+ end
17
+ end
18
+
19
+ def filtered
20
+ logframe = @frames.rindex { |frm| frm.path.include? "logue/lib/logue" }
21
+ @frames[logframe + 1 .. -1]
22
+ end
23
+ end
24
+ end
data/lib/logue/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # -*- ruby -*-
3
3
 
4
4
  module Logue
5
- VERSION = '1.0.11'
5
+ VERSION = '1.0.13'
6
6
  end
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ module Logue
5
+ class Writer
6
+ attr_accessor :output
7
+ attr_accessor :colors
8
+ attr_accessor :colorize_line
9
+
10
+ def initialize output: $stdout, colors: Array.new, colorize_line: false
11
+ @output = output
12
+ @colors = colors
13
+ @colorize_line = colorize_line
14
+ end
15
+
16
+ def print location, msg, lvl, &blk
17
+ if blk
18
+ x = blk.call
19
+ if x.kind_of? String
20
+ msg = x
21
+ else
22
+ return
23
+ end
24
+ end
25
+
26
+ msg = msg.to_s.chomp
27
+ line = line location, msg, lvl
28
+ @output.puts line
29
+ end
30
+
31
+ def line location, msg, lvl
32
+ if lvlcol = @colors[lvl]
33
+ if @colorize_line
34
+ line = location + " " + msg
35
+ line.color lvlcol
36
+ else
37
+ location + " " + msg.color(lvlcol)
38
+ end
39
+ else
40
+ location + " " + msg
41
+ end
42
+ end
43
+ end
44
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logue
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.11
4
+ version: 1.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Pace
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-26 00:00:00.000000000 Z
11
+ date: 2018-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,8 +97,12 @@ files:
97
97
  - bin/console
98
98
  - bin/setup
99
99
  - lib/logue.rb
100
+ - lib/logue/colorlog.rb
100
101
  - lib/logue/colors.rb
102
+ - lib/logue/filter.rb
101
103
  - lib/logue/frame.rb
104
+ - lib/logue/legacy_logger.rb
105
+ - lib/logue/level.rb
102
106
  - lib/logue/line.rb
103
107
  - lib/logue/location.rb
104
108
  - lib/logue/location_format.rb
@@ -107,7 +111,9 @@ files:
107
111
  - lib/logue/logger.rb
108
112
  - lib/logue/pathutil.rb
109
113
  - lib/logue/severity.rb
114
+ - lib/logue/stack.rb
110
115
  - lib/logue/version.rb
116
+ - lib/logue/writer.rb
111
117
  - logue.gemspec
112
118
  homepage: https://www.github.com/jpace/logue
113
119
  licenses: