Skip to content
Browse files

Merge pull request #29 from jonmagic/logging

Add Grim.logger
  • Loading branch information...
2 parents 6b8a8e1 + e8fda58 commit 50e973d3bafb9988a0f5e6043f965e1abb9d0647 @jonmagic committed on GitHub Jun 13, 2016
Showing with 79 additions and 4 deletions.
  1. +13 −0 README.md
  2. +16 −1 lib/grim.rb
  3. +7 −1 lib/grim/image_magick_processor.rb
  4. +9 −0 lib/grim/null_logger.rb
  5. +4 −1 lib/grim/page.rb
  6. +11 −0 spec/lib/grim/null_logger_spec.rb
  7. +19 −1 spec/lib/grim_spec.rb
View
13 README.md
@@ -86,6 +86,19 @@ pdf[0].save('/path/to/image.png', {
})
```
+Grim has limited logging abilities. The default logger is `Grim::NullLogger` but you can also set your own logger.
+
+```ruby
+require "logger"
+Grim.logger = Logger.new($stdout).tap { |logger| logger.progname = 'Grim' }
+Grim.processor = Grim::ImageMagickProcessor.new({:ghostscript_path => "/path/to/bin/gs"})
+pdf = Grim.reap("/path/to/pdf")
+pdf[3].save('/path/to/image.png')
+# D, [2016-06-09T22:43:07.046532 #69344] DEBUG -- grim: Running imagemagick command
+# D, [2016-06-09T22:43:07.046626 #69344] DEBUG -- grim: PATH=/path/to/bin:/usr/local/bin:/usr/bin
+# D, [2016-06-09T22:43:07.046787 #69344] DEBUG -- grim: convert -resize 1024 -antialias -render -quality 90 -colorspace RGB -interlace none -density 300 /path/to/pdf /path/to/image.png
+```
+
## Reference
* [jonmagic.com: Grim](http://jonmagic.com/blog/archives/2011/09/06/grim/)
View
17 lib/grim.rb
@@ -50,11 +50,26 @@ class UnprocessablePage < Grim::Exception
def self.reap(path)
Grim::Pdf.new(path)
end
+
+ # Set a Logger compatible logger for Grim.
+ #
+ # logger - Object with same interface as Logger.new
+ def self.logger=(logger)
+ @logger = logger
+ end
+
+ # Logger to use internally. Defaults to Grim::NullLogger.
+ #
+ # Returns a Logger compatible logger.
+ def self.logger
+ @logger ||= Grim::NullLogger.new
+ end
end
require 'grim/pdf'
require 'grim/page'
require 'grim/image_magick_processor'
require 'grim/multi_processor'
+require 'grim/null_logger'
-Grim.processor = Grim::ImageMagickProcessor.new
+Grim.processor = Grim::ImageMagickProcessor.new
View
8 lib/grim/image_magick_processor.rb
@@ -48,7 +48,13 @@ def save(pdf, index, path, options)
command_env['PATH'] = "#{File.dirname(@ghostscript_path)}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
end
- result, status = Open3.capture2e(command_env, command.join(' '))
+ Grim.logger.debug { "Running imagemagick command" }
+ if command_env.any?
+ Grim.logger.debug { command_env.map {|k,v| "#{k}=#{v}" }.join(" ") }
+ end
+ Grim.logger.debug { command.join(" ") }
+
+ result, status = Open3.capture2e(command_env, command.join(" "))
status.success? || raise(UnprocessablePage, result)
end
View
9 lib/grim/null_logger.rb
@@ -0,0 +1,9 @@
+require "logger"
+
+module Grim
+ class NullLogger
+ def initialize(*args); end
+ def debug(*args); end
+ def info(*args); end
+ end
+end
View
5 lib/grim/page.rb
@@ -50,7 +50,10 @@ def save(path, options={})
# Returns a String.
#
def text
- `#{[@pdftotext_path, "-enc", "UTF-8", "-f", @number, "-l", @number, Shellwords.escape(@pdf.path), "-"].join(' ')}`
+ command = [@pdftotext_path, "-enc", "UTF-8", "-f", @number, "-l", @number, Shellwords.escape(@pdf.path), "-"].join(' ')
+ Grim.logger.debug { "Running pdftotext command" }
+ Grim.logger.debug { command }
+ `#{command}`
end
end
end
View
11 spec/lib/grim/null_logger_spec.rb
@@ -0,0 +1,11 @@
+require 'spec_helper'
+require "stringio"
+
+describe Grim::NullLogger do
+ it "acts like Logger but doesn't log anything" do
+ io = StringIO.new
+ logger = Grim::NullLogger.new(io)
+ logger.debug "hello world"
+ expect(io.string).to eq("")
+ end
+end
View
20 spec/lib/grim_spec.rb
@@ -26,9 +26,27 @@
expect(Grim::COLORSPACE).to eq('RGB')
end
- describe "#reap" do
+ describe ".reap" do
it "should return an instance of Grim::Pdf" do
expect(Grim.reap(fixture_path("smoker.pdf")).class).to eq(Grim::Pdf)
end
end
+
+ describe ".logger=" do
+ it "sets Grim.logger" do
+ original_logger = Grim.logger
+ begin
+ Grim.logger = "foo"
+ expect(Grim.logger).to eq("foo")
+ ensure
+ Grim.logger = original_logger
+ end
+ end
+ end
+
+ describe ".logger" do
+ it "returns a Grim::NullLogger by default" do
+ expect(Grim.logger).to be_instance_of(Grim::NullLogger)
+ end
+ end
end

0 comments on commit 50e973d

Please sign in to comment.
Something went wrong with that request. Please try again.