Skip to content

Commit

Permalink
Add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
meltingice committed Aug 2, 2013
1 parent 0068f06 commit 462cc8b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
34 changes: 17 additions & 17 deletions examples/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
psd.parse!
end

puts "\nVisible Layers:\n===================="
psd.layers.each do |layer|
next if layer.folder? || layer.hidden?
# puts "\nVisible Layers:\n===================="
# psd.layers.each do |layer|
# next if layer.folder? || layer.hidden?

puts "Name: #{layer.name}"
puts "Position: top = #{layer.top}, left = #{layer.left}"
puts "Size: width = #{layer.width}, height = #{layer.height}"
puts "Mask: width = #{layer.mask.width}, height = #{layer.mask.height}"
puts "Reference point: #{layer.ref_x}, #{layer.ref_y}"
# puts "Name: #{layer.name}"
# puts "Position: top = #{layer.top}, left = #{layer.left}"
# puts "Size: width = #{layer.width}, height = #{layer.height}"
# puts "Mask: width = #{layer.mask.width}, height = #{layer.mask.height}"
# puts "Reference point: #{layer.ref_x}, #{layer.ref_y}"

puts ""
end
# puts ""
# end

puts "\nPSD Info:\n===================="
puts "#{psd.width}x#{psd.height} #{psd.header.mode_name}"
puts "#{psd.resources.size} resources parsed"
puts "#{psd.layers.size} layers, #{psd.folders.size} folders"
# puts "\nPSD Info:\n===================="
# puts "#{psd.width}x#{psd.height} #{psd.header.mode_name}"
# puts "#{psd.resources.size} resources parsed"
# puts "#{psd.layers.size} layers, #{psd.folders.size} folders"

puts "\nBenchmark Results (seconds):\n===================="
puts " "*7 + Benchmark::CAPTION
puts "parse: " + results.to_s
# puts "\nBenchmark Results (seconds):\n===================="
# puts " "*7 + Benchmark::CAPTION
# puts "parse: " + results.to_s
3 changes: 3 additions & 0 deletions lib/psd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# the header, resources, the layer mask (including layers), and the preview image. We parse
# each of these sections in order.
class PSD
include Logger
include Helpers
include NodeExporting

Expand Down Expand Up @@ -50,6 +51,8 @@ def initialize(file, opts={})
# parse all sections of the PSD.
def parse!
header
PSD.logger.debug header.inspect

resources
layer_mask
image if @opts[:parse_image]
Expand Down
2 changes: 2 additions & 0 deletions lib/psd/layer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def parse(index=nil)
parse_legacy_layer_name
parse_extra_data

PSD.logger.debug "Layer name = #{name}"

@file.seek @layer_end # Skip over any filler zeros

end_section
Expand Down
42 changes: 42 additions & 0 deletions lib/psd/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'logger'

class PSD
module Logger
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
@@debug = false

def logger
return @logger if @logger

if @@debug || ENV['PSD_DEBUG']
@logger = ::Logger.new(debug_output)
@logger.formatter = proc do |severity, datetime, progname, msg|
"#{severity}: #{msg}\n"
end
else
@logger = DisabledLogger.new
end

return @logger
end

def debug_output
if ENV['PSD_DEBUG']
ENV['PSD_DEBUG'] == 'STDOUT' ? STDOUT : ENV['PSD_DEBUG']
end

STDOUT
end
end
end

class DisabledLogger
def method_missing(method, *args, &block)
# silence
end
end
end

0 comments on commit 462cc8b

Please sign in to comment.