Skip to content

Commit

Permalink
fix #705
Browse files Browse the repository at this point in the history
* add class ReVIEW::Logger, sub class of ::Logger
* add methods ReVIEW.logger() and ReVIEW.logger(logger)
* use @logger for error() and warn()
  • Loading branch information
takahashim committed Jul 7, 2017
1 parent 1bc601f commit 78354eb
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 92 deletions.
8 changes: 5 additions & 3 deletions bin/review-catalog-converter
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ bindir = Pathname.new(__FILE__).realpath.dirname
$LOAD_PATH.unshift((bindir + '../lib').realpath)
require 'review/version'
require 'review/extentions'
require 'review/logger'

def main
@logger = ReVIEW.logger
opts = OptionParser.new
opts.version = ReVIEW::VERSION
opts.banner = "Usage: #{File.basename($0)} dirname"
Expand All @@ -29,7 +31,7 @@ def main
begin
opts.parse!
rescue OptionParser::ParseError => err
$stderr.puts err.message
@logger.error err.message
$stderr.puts opts.help
exit 1
end
Expand All @@ -42,10 +44,10 @@ def main
print "The catalog.yml already exists. Do you want to overwrite it? [y/n]"
case gets
when /^[yY]/
puts "Start writing..."
@logger.info "Start writing..."
break
when /^[nN]/, /^$/
puts "bye."
@logger.info "bye."
exit
end
end
Expand Down
8 changes: 5 additions & 3 deletions bin/review-check
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def main
@book = ReVIEW::Book::Base.load
@book.config = @config

@logger = ReVIEW.logger

modes = nil
files = ARGV unless ARGV.empty?
opts = OptionParser.new
Expand All @@ -57,12 +59,12 @@ def main
begin
opts.parse!
rescue OptionParser::ParseError => err
$stderr.puts err.message
$stderr.puts opts.help
@logger.error(err.message)
puts opts.help
exit 1
end
unless files
$stderr.puts "no input"
@logger.error("no input")
exit 1
end
modes ||= [:text]
Expand Down
15 changes: 6 additions & 9 deletions bin/review-compile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rescue Errno::EPIPE
end

def _main
@logger = ReVIEW.logger
mode = :files
basedir = nil
if /\Areview2/ =~ File.basename($0)
Expand Down Expand Up @@ -101,8 +102,8 @@ def _main
end
end
rescue OptionParser::ParseError => err
error err.message
$stderr.puts opts.help
@logger.error(err.message)
stderr.puts opts.help
exit 1
end

Expand All @@ -121,7 +122,7 @@ def _main
begin
config.check_version(ReVIEW::VERSION)
rescue ReVIEW::ConfigError => e
warn e.message
@logger.warn e.message
end

if ARGV.blank?
Expand All @@ -131,7 +132,7 @@ def _main
case mode
when :files
if ARGV.empty?
error 'no input'
@logger.error('no input')
exit 1
end

Expand Down Expand Up @@ -167,15 +168,11 @@ def _main
end
rescue ReVIEW::ApplicationError => err
raise if $DEBUG
error err.message
@logger.error(err.message)
exit 1
end
end

def error(msg)
$stderr.puts "#{File.basename($0, '.*')}: error: #{msg}"
end

def load_strategy_class(target, strict)
require "review/#{target}builder"
ReVIEW.const_get("#{target.upcase}Builder").new(strict)
Expand Down
4 changes: 3 additions & 1 deletion bin/review-epubmaker
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ $LOAD_PATH.unshift((bindir + '../lib').realpath)
require 'review/epubmaker'
require 'review/version'

@logger = ReVIEW.logger

rv = ReVIEW::EPUBMaker.new

opts = OptionParser.new
Expand All @@ -28,7 +30,7 @@ end
begin
opts.parse!
rescue OptionParser::ParseError => err
$stderr.puts err.message
@logger.error err.message
$stderr.puts opts.help
exit 1
end
Expand Down
6 changes: 4 additions & 2 deletions bin/review-index
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ require 'review/tocprinter'
require 'review/version'
require 'optparse'


def main
@logger = ReVIEW.logger
Signal.trap(:INT) { exit 1 }
if RUBY_PLATFORM !~ /mswin(?!ce)|mingw|cygwin|bccwin/
Signal.trap(:PIPE, 'IGNORE')
Expand Down Expand Up @@ -74,7 +76,7 @@ def _main
begin
opts.parse!
rescue OptionParser::ParseError => err
$stderr.puts err.message
@logger.error err.message
$stderr.puts opts.help
exit 1
end
Expand All @@ -101,7 +103,7 @@ def _main
end

def error_exit(msg)
$stderr.puts "#{File.basename($0)}: #{msg}"
@logger.error "#{File.basename($0)}: #{msg}"
exit 1
end

Expand Down
8 changes: 5 additions & 3 deletions bin/review-init
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ require 'pathname'
bindir = Pathname.new(__FILE__).realpath.dirname
$LOAD_PATH.unshift((bindir + '../lib').realpath)

require 'review/version'
require 'review'


def main
@logger = ReVIEW.logger
opts = OptionParser.new
opts.version = ReVIEW::VERSION
opts.banner = "Usage: #{File.basename($0)} [option] dirname"
Expand All @@ -37,7 +39,7 @@ def main
begin
opts.parse!
rescue OptionParser::ParseError => err
$stderr.puts err.message
@logger.error err.message
$stderr.puts opts.help
exit 1
end
Expand Down Expand Up @@ -67,7 +69,7 @@ end

def generate_dir(dir)
if File.exist?(dir) && !@force
puts "#{dir} already exists."
@logger.error "#{dir} already exists."
exit
end
FileUtils.mkdir_p dir
Expand Down
8 changes: 5 additions & 3 deletions bin/review-preproc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require 'review/preprocessor'
require 'review/unfold'
require 'review/version'
require 'review/extentions'
require 'review/logger'
require 'lineinput'
require 'stringio'
require 'fileutils'
Expand All @@ -36,9 +37,10 @@ rescue Errno::EPIPE
end

def main
@logger = ReVIEW.logger
if File.file?("review-preproc-ext.rb")
if ENV["REVIEW_SAFE_MODE"].to_i & 2 > 0
warn "review-preproc-ext.rb is prohibited in safe mode. ignored."
@logger.warn "review-preproc-ext.rb is prohibited in safe mode. ignored."
else
Kernel.load File.expand_path("review-preproc-ext.rb")
end
Expand Down Expand Up @@ -73,7 +75,7 @@ def main
begin
opts.parse!
rescue OptionParser::ParseError => err
$stderr.puts err.message
@logger.error err.message
$stderr.puts opts.help
exit 1
end
Expand Down Expand Up @@ -119,7 +121,7 @@ def main
end
rescue ReVIEW::Error => err
raise if $DEBUG
$stderr.puts err.message
@logger.error err.message
exit 1
end

Expand Down
5 changes: 3 additions & 2 deletions bin/review-vol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require 'optparse'
include ReVIEW::TextUtils

def main
@logger = ReVIEW.logger
@config = ReVIEW::Configure.values

part_sensitive = false
Expand All @@ -44,7 +45,7 @@ def main
begin
opts.parse!
rescue OptionParser::ParseError => err
$stderr.puts err.message
@logger.error err.message
$stderr.puts opts.help
exit 1
end
Expand Down Expand Up @@ -76,7 +77,7 @@ def main
print_volume book.volume #puts "Total #{book.volume}"
rescue ReVIEW::ApplicationError, Errno::ENOENT => err
raise if $DEBUG
$stderr.puts "#{File.basename($0)}: #{err.message}"
@logger.error "#{File.basename($0)}: #{err.message}"
exit 1
end

Expand Down
4 changes: 2 additions & 2 deletions lib/review/book/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Base
attr_reader :basedir

def self.load_default
warn 'Book::Base.load_default() is obsoleted. Use Book::Base.load().'
ReVIEW.logger.warn 'Book::Base.load_default() is obsoleted. Use Book::Base.load().'
load()
end

Expand All @@ -35,7 +35,7 @@ def self.update_rubyenv(dir)
return if @basedir_seen.key?(dir)
if File.file?("#{dir}/review-ext.rb")
if ENV["REVIEW_SAFE_MODE"].to_i & 2 > 0
warn "review-ext.rb is prohibited in safe mode. ignored."
ReVIEW.logger.warn "review-ext.rb is prohibited in safe mode. ignored."
else
Kernel.load File.expand_path("#{dir}/review-ext.rb")
end
Expand Down
10 changes: 6 additions & 4 deletions lib/review/book/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'review/exception'
require 'review/book/image_finder'
require 'review/i18n'
require 'review/logger'

module ReVIEW
module Book
Expand All @@ -25,7 +26,7 @@ def Index.parse(src, *args)
items.push item_class().new(id, seq)
seq += 1
if id == ""
warn "warning: no ID of #{item_type()} in #{line}"
ReVIEW.logger.warn "warning: no ID of #{item_type()} in #{line}"
end
end
end
Expand All @@ -47,8 +48,9 @@ def item_type
def initialize(items)
@items = items
@index = {}
@logger = ReVIEW.logger
items.each do |i|
warn "warning: duplicate ID: #{i.id} (#{i})" unless @index[i.id].nil?
@logger.warn "warning: duplicate ID: #{i.id} (#{i})" unless @index[i.id].nil?
@index[i.id] = i
end
@image_finder = nil
Expand Down Expand Up @@ -158,7 +160,7 @@ def self.parse(src, *args)
seq += 1
end
if elements[1] == ""
warn "warning: no ID of #{item_type()} in #{line}"
ReVIEW.logger.warn "warning: no ID of #{item_type()} in #{line}"
end
end
end
Expand Down Expand Up @@ -340,7 +342,7 @@ def initialize(items, chap)
@chap = chap
@index = {}
items.each do |i|
warn "warning: duplicate ID: #{i.id}" unless @index[i.id].nil?
@logger.warn "warning: duplicate ID: #{i.id}" unless @index[i.id].nil?
@index[i.id] = i
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def post_paragraph
def initialize(strict = false, *args)
@strict = strict
@output = nil
@logger = ReVIEW.logger
builder_init(*args)
end

Expand Down Expand Up @@ -363,7 +364,7 @@ def embed(lines, arg = nil)
end

def warn(msg)
$stderr.puts "#{@location}: warning: #{msg}"
@logger.warn "#{@location}: #{msg}"
end

def error(msg)
Expand Down
18 changes: 0 additions & 18 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,6 @@ def xmlns_ops_prefix
end
end

def warn(msg)
if @no_error
@warns.push [@location.filename, @location.lineno, msg]
puts "----WARNING: #{escape_html(msg)}----"
else
$stderr.puts "#{@location}: warning: #{msg}"
end
end

def error(msg)
if @no_error
@errors.push [@location.filename, @location.lineno, msg]
puts "----ERROR: #{escape_html(msg)}----"
else
$stderr.puts "#{@location}: error: #{msg}"
end
end

def headline(level, label, caption)
prefix, anchor = headline_prefix(level)
unless prefix.nil?
Expand Down
18 changes: 0 additions & 18 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,6 @@ def result
@output.string + s + "</#{@rootelement}>\n"
end

def warn(msg)
if @no_error
@warns.push [@location.filename, @location.lineno, msg]
puts "----WARNING: #{escape_html(msg)}----"
else
$stderr.puts "#{@location}: warning: #{msg}"
end
end

def error(msg)
if @no_error
@errors.push [@location.filename, @location.lineno, msg]
puts "----ERROR: #{escape_html(msg)}----"
else
$stderr.puts "#{@location}: error: #{msg}"
end
end

def headline(level, label, caption)
case level
when 1
Expand Down
Loading

0 comments on commit 78354eb

Please sign in to comment.