growl-logger 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,13 +3,14 @@ require 'rubygems'
3
3
  require 'logger'
4
4
 
5
5
  class GrowlLogger < Logger
6
- VERSION = '0.1.3'
6
+ VERSION = '0.2.0'
7
7
 
8
8
  def initialize(args = {})
9
9
  super(GrowlLogger::LogDevice.new(
10
10
  args[:name] || 'growl-logger',
11
11
  args[:host] || 'localhost',
12
- args[:growlnotify] || false
12
+ :mode => args[:mode],
13
+ :editor => args[:editor]
13
14
  ))
14
15
  self.level = args[:level] if args[:level]
15
16
  self.datetime_format = args[:datetime_format] || '%X'
@@ -17,28 +18,61 @@ class GrowlLogger < Logger
17
18
  end
18
19
 
19
20
  class LogDevice
20
- def initialize(name, host, growlnotify_mode = true, &block)
21
+ def initialize(name, host, options = {}, &block)
21
22
  @name = name
22
- @growlnotify_mode = growlnotify_mode
23
- unless @growlnotify_mode
23
+ @mode = options[:mode] ? options[:mode].to_sym : nil
24
+ @editor = options[:editor] || 'mate -l'
25
+ unless @mode.to_s == 'growlnotify'
24
26
  begin
25
- require 'ruby-growl'
26
- @growl = Growl.new host, @name, ["log"]
27
+ require 'meow'
28
+ @meow = Meow.new('log')
27
29
  rescue LoadError
28
30
  end
31
+ unless @meow
32
+ begin
33
+ require 'ruby-growl'
34
+ @growl = Growl.new host, @name, ["log"]
35
+ rescue LoadError
36
+ end
37
+ end
29
38
  end
30
39
  @formatter = block if block_given?
31
40
  end
32
41
 
33
42
  def write(message)
34
43
  priority = get_priority(message)
35
- if @growl
36
- @growl.notify "log", @name, message, priority
44
+ case @mode
45
+ when :meow
46
+ notify_by_meow(message, priority)
47
+ when :'ruby-growl'
48
+ notify_by_rubygrowl(message, priority)
49
+ when :growlnotify
50
+ notify_by_growlnotify(message, priority)
37
51
  else
38
- system 'growlnotify', @name, '-p', priority.to_s, '-m', message
52
+ if @meow then notify_by_meow(message, priority)
53
+ elsif @growl then notify_by_rubygrowl(message, priority)
54
+ else notify_by_growlnotify(message, priority)
55
+ end
56
+ end
57
+ end
58
+
59
+ def notify_by_meow(message, priority)
60
+ call_point = caller.last
61
+ line_number = call_point[/\d+$/]
62
+ file_name = call_point.sub(/:\d+$/, '')
63
+ @meow.notify @name, message, :priority => priority do
64
+ system *(@editor.split(/\s/) + [line_number, File.expand_path(file_name)])
39
65
  end
40
66
  end
41
67
 
68
+ def notify_by_rubygrowl(message, priority)
69
+ @growl.notify "log", @name, message, priority
70
+ end
71
+
72
+ def notify_by_growlnotify(message, priority)
73
+ system 'growlnotify', @name, '-p', priority.to_s, '-m', message
74
+ end
75
+
42
76
  def get_priority(message)
43
77
  case message
44
78
  when /^DEBUG/
@@ -12,7 +12,7 @@ describe GrowlLogger do
12
12
  logdev.instance_eval{@name}.should == 'growl-logger'
13
13
  logger.datetime_format.should == '%X'
14
14
  logger.formatter.call('DEBUG', Time.now, 'foo', 'message').should == "DEBUG: message"
15
- logdev.instance_eval{@growlnotify_mode}.should == false
15
+ logdev.instance_eval{@mode}.should == nil
16
16
  end
17
17
 
18
18
  it 'init with name option' do
@@ -21,10 +21,46 @@ describe GrowlLogger do
21
21
  logdev.instance_eval{@name}.should == 'foo'
22
22
  end
23
23
 
24
- it 'init with growlnotify option' do
25
- logger = GrowlLogger.new :growlnotify => true
24
+ it 'init as growlnotify mode' do
25
+ logger = GrowlLogger.new :mode => :growlnotify
26
26
  logdev = logger.instance_eval{@logdev}.dev
27
- logdev.instance_eval{@growlnotify_mode}.should == true
27
+ logdev.instance_eval{@mode}.should == :growlnotify
28
+ logdev.should_receive(:notify_by_growlnotify)
29
+ logdev.should_not_receive(:notify_by_meow)
30
+ logdev.should_not_receive(:notify_by_rubygrowl)
31
+ logdev.write('test')
32
+ end
33
+
34
+ it 'init as meow mode' do
35
+ logger = GrowlLogger.new :mode => :meow
36
+ logdev = logger.instance_eval{@logdev}.dev
37
+ logdev.instance_eval{@mode}.should == :meow
38
+ logdev.should_receive(:notify_by_meow)
39
+ logdev.should_not_receive(:notify_by_growlnotify)
40
+ logdev.should_not_receive(:notify_by_rubygrowl)
41
+ logdev.write('test')
42
+ end
43
+
44
+ it 'init as ruby-growl mode' do
45
+ logger = GrowlLogger.new :mode => 'ruby-growl'
46
+ logdev = logger.instance_eval{@logdev}.dev
47
+ logdev.instance_eval{@mode}.should == :'ruby-growl'
48
+ logdev.should_receive(:notify_by_rubygrowl)
49
+ logdev.should_not_receive(:notify_by_growlnotify)
50
+ logdev.should_not_receive(:notify_by_meow)
51
+ logdev.write('test')
52
+ end
53
+
54
+ it 'not specify editor' do
55
+ logger = GrowlLogger.new
56
+ logdev = logger.instance_eval{@logdev}.dev
57
+ logdev.instance_eval{@editor}.should == 'mate -l'
58
+ end
59
+
60
+ it 'specify editor' do
61
+ logger = GrowlLogger.new :editor => 'foo'
62
+ logdev = logger.instance_eval{@logdev}.dev
63
+ logdev.instance_eval{@editor}.should == 'foo'
28
64
  end
29
65
 
30
66
  it 'init with datetime_format option' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: growl-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-27 00:00:00 +09:00
12
+ date: 2009-04-11 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15