Skip to content

Commit

Permalink
Merge pull request #29 from jonmagic/logging
Browse files Browse the repository at this point in the history
Add Grim.logger
  • Loading branch information
jonmagic authored Jun 14, 2016
2 parents 6b8a8e1 + e8fda58 commit 50e973d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
17 changes: 16 additions & 1 deletion lib/grim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion lib/grim/image_magick_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions lib/grim/null_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "logger"

module Grim
class NullLogger
def initialize(*args); end
def debug(*args); end
def info(*args); end
end
end
5 changes: 4 additions & 1 deletion lib/grim/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions spec/lib/grim/null_logger_spec.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 19 additions & 1 deletion spec/lib/grim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.