Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

+ Picky logging extracted. Now logs by default using the concise logg…

…er, set like this: Picky.logger = Picky::Loggers::Concise.new(STDOUT)
  • Loading branch information...
commit a7f877ccc2869026282e318d41de85eb444aaab5 1 parent 8b8296f
Florian R. Hanke authored
5 history.textile
View
@@ -1,5 +1,10 @@
h2. Upcoming Version
+h2. Version 4.2.0 "talk to the hand"
+
+* hanke: (server) Redesigned how Picky logs: Picky itself logs its index handling (tokenizing/dumping/loading) using one of its built-in loggers. Set a logger after requiring 'picky' like this: @Picky.logger = Picky::Loggers::Verbose.new(STDOUT) # or any IO@. Default is @Picky::Loggers::Concise.new(STDOUT)@ aka @Picky::Loggers::Default@. Also an option is @Picky::Loggers::Silent@. This closes "issue 70":https://github.com/floere/picky/issues/70.
+* hanke: (server) Note: Logging searches is your job (see generated examples on how to do this).
+
h2. Version 4.1.0 "identification needed"
* hanke: (server) @Picky::Results#ids(only = nil)@ returns the amount of ids originally requested, except if an @only@ amount gets passed in (then that amount is used).
2  server/lib/picky.rb
View
@@ -32,5 +32,7 @@ module Picky
class << self
attr_accessor :logger
end
+
+ self.logger = Loggers::Default
end
2  server/lib/picky/category.rb
View
@@ -79,7 +79,7 @@ def reindex
def dump
exact.dump
partial.dump
- timed_exclaim %Q{ "#{identifier}": Dumped -> #{index_directory.gsub("#{PICKY_ROOT}/", '')}/#{name}_*.}
+ Picky.logger.dump self
end
# Returns the backend.
2  server/lib/picky/category_indexed.rb
View
@@ -7,7 +7,7 @@ class Category
# Loads the index from cache.
#
def load
- timed_exclaim %Q{ "#{identifier}": Loading index from cache.}
+ Picky.logger.load self
clear_realtime # THINK Should we really explicitly clear the realtime? Or should it just be loaded?
exact.load
partial.load
8 server/lib/picky/extensions/object.rb
View
@@ -1,5 +1,7 @@
class Object # :nodoc:all
-
+
+ # TODO Remove.
+
# Puts a text in the form:
# 12:34:56: text here
#
@@ -10,8 +12,8 @@ def timed_exclaim text
# Just puts the given text.
#
def exclaim text
- STDOUT.puts text
- STDOUT.flush
+ Picky.logger.info text
+ Picky.logger.flush
end
# Puts a text that informs the user of a missing gem.
8 server/lib/picky/helpers/indexing.rb
View
@@ -6,12 +6,10 @@ module Helpers
module Indexing
include Measuring
-
- # Returns a duration in seconds.
- #
+
def timed_indexing scheduler, &block
- timed_exclaim "Indexing using #{scheduler.fork? ? 'multiple processes' : 'a single process'}."
- timed_exclaim "Indexing finished after #{timed(&block).round}s."
+ Picky.logger.info "Picky is indexing using #{scheduler.fork? ? 'multiple processes' : 'a single process'}: "
+ Picky.logger.info " Done in #{timed(&block).round}s.\n"
end
end
2  server/lib/picky/indexers/base.rb
View
@@ -40,7 +40,7 @@ def check source # :nodoc:
end
def notify_finished prepared_file
- timed_exclaim %Q{ "#{@index_or_category.identifier}": Tokenized -> #{prepared_file.path.gsub("#{PICKY_ROOT}/", '')}.}
+ Picky.logger.tokenize @index_or_category, prepared_file
end
end
2  server/lib/picky/indexes_indexing.rb
View
@@ -26,7 +26,7 @@ def index scheduler = Scheduler.new
indexes.each { |index| index.prepare scheduler }
scheduler.finish
- timed_exclaim "Tokenizing finished, generating data for indexes from tokenized data."
+ # timed_exclaim "Tokenizing finished, generating data for indexes from tokenized data."
indexes.each { |index| index.cache scheduler }
scheduler.finish
7 server/lib/picky/loader.rb
View
@@ -192,6 +192,13 @@ def load_user_interface
load_relative 'api/category/partial'
load_relative 'api/category/similarity'
load_relative 'api/search/boost'
+
+ # Loggers.
+ #
+ load_relative 'loggers/silent'
+ load_relative 'loggers/concise'
+ load_relative 'loggers/verbose'
+ load_relative 'loggers/default'
# Tokenizer.
#
49 server/lib/picky/loggers/concise.rb
View
@@ -0,0 +1,49 @@
+module Picky
+
+ module Loggers
+
+ # The verbose logger outputs little information.
+ #
+ class Concise < Silent
+
+ attr_reader :tokenized,
+ :dumped,
+ :loaded
+
+ def initialize *args
+ super *args
+
+ reset
+ end
+
+ def reset
+ @tokenized = false
+ @dumped = false
+ @loaded = false
+ end
+
+ def info text
+ io.write text
+ end
+
+ def tokenize(*)
+ progress 'T'
+ end
+
+ def dump(*)
+ progress 'D'
+ end
+
+ def load(*)
+ progress
+ end
+
+ def progress type = '.'
+ io.write type
+ end
+
+ end
+
+ end
+
+end
10 server/lib/picky/loggers/default.rb
View
@@ -0,0 +1,10 @@
+module Picky
+ module Loggers
+
+ # Default is the concise logger.
+ #
+ remove_const :Default if defined? Default
+ Default = Concise.new
+
+ end
+end
41 server/lib/picky/loggers/silent.rb
View
@@ -0,0 +1,41 @@
+module Picky
+
+ module Loggers
+
+ # The silent logger just gobbles up all information.
+ #
+ class Silent
+
+ attr_reader :io
+
+ def initialize io = STDOUT
+ @io = io
+ end
+
+ def info(*)
+
+ end
+
+ def tokenize(*)
+
+ end
+
+ def dump(*)
+
+ end
+
+ def load(*)
+
+ end
+
+ # Flush this logger.
+ #
+ def flush
+ io.flush
+ end
+
+ end
+
+ end
+
+end
37 server/lib/picky/loggers/verbose.rb
View
@@ -0,0 +1,37 @@
+module Picky
+
+ module Loggers
+
+ # The verbose logger outputs all information.
+ #
+ class Verbose < Silent
+
+ def info text
+ timed_exclaim text
+ end
+
+ def tokenize index_or_category, prepared_file
+ timed_exclaim %Q{ "#{index_or_category.identifier}": Tokenized -> #{prepared_file.path.gsub("#{PICKY_ROOT}/", '')}.}
+ end
+
+ def dump category
+ timed_exclaim %Q{ "#{category.identifier}": Dumped -> #{category.index_directory.gsub("#{PICKY_ROOT}/", '')}/#{category.name}_*.}
+ end
+
+ def load category
+ timed_exclaim %Q{ "#{category.identifier}": Loading index from cache.}
+ end
+
+ # Puts a text in the form:
+ # 12:34:56: text here
+ #
+ def timed_exclaim text
+ io.puts "#{Time.now.strftime("%H:%M:%S")}: #{text}"
+ flush
+ end
+
+ end
+
+ end
+
+end
27 server/spec/lib/loggers/concise_spec.rb
View
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+describe Picky::Loggers::Concise do
+
+ let(:io) { StringIO.new }
+ let(:logger) { described_class.new io }
+
+ describe 'more complicated test case' do
+ it 'is correct' do
+ logger.info 'Tokenizing '
+ logger.tokenize :some_category
+ logger.tokenize :some_category
+ logger.tokenize :some_category
+ logger.info ' Dumping '
+ logger.dump :some_category
+ logger.dump :some_category
+ logger.info ' Loading '
+ logger.load :some_category
+ logger.load :some_category
+ logger.load :some_category
+ logger.load :some_category
+
+ io.string.should == 'Tokenizing TTT Dumping DD Loading ....'
+ end
+ end
+
+end
27 server/spec/lib/loggers/silent_spec.rb
View
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+describe Picky::Loggers::Silent do
+
+ let(:io) { StringIO.new }
+ let(:logger) { described_class.new io }
+
+ describe 'more complicated test case' do
+ it 'is correct' do
+ logger.info 'Tokenizing '
+ logger.tokenize :some_category
+ logger.tokenize :some_category
+ logger.tokenize :some_category
+ logger.info ' Dumping '
+ logger.dump :some_category
+ logger.dump :some_category
+ logger.info ' Loading '
+ logger.load :some_category
+ logger.load :some_category
+ logger.load :some_category
+ logger.load :some_category
+
+ io.string.should == ''
+ end
+ end
+
+end
33 server/spec/lib/loggers/verbose_spec.rb
View
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe Picky::Loggers::Verbose do
+
+ let(:io) { StringIO.new }
+ let(:logger) { described_class.new io }
+
+ describe 'more complicated test case' do
+ it 'is correct' do
+ index = Picky::Index.new :some_index
+ category = Picky::Category.new :some_category, index
+ file = stub :file, :path => 'some/path'
+
+ Time.stub! :now => Time.new('zeros')
+
+ logger.info 'Tokenizing '
+ logger.tokenize category, file
+ logger.tokenize category, file
+ logger.tokenize category, file
+ logger.info ' Dumping '
+ logger.dump category
+ logger.dump category
+ logger.info ' Loading '
+ logger.load category
+ logger.load category
+ logger.load category
+ logger.load category
+
+ io.string.should == "00:00:00: Tokenizing \n00:00:00: \"some_index:some_category\": Tokenized -> some/path.\n00:00:00: \"some_index:some_category\": Tokenized -> some/path.\n00:00:00: \"some_index:some_category\": Tokenized -> some/path.\n00:00:00: Dumping \n00:00:00: \"some_index:some_category\": Dumped -> index/test/some_index/some_category_*.\n00:00:00: \"some_index:some_category\": Dumped -> index/test/some_index/some_category_*.\n00:00:00: Loading \n00:00:00: \"some_index:some_category\": Loading index from cache.\n00:00:00: \"some_index:some_category\": Loading index from cache.\n00:00:00: \"some_index:some_category\": Loading index from cache.\n00:00:00: \"some_index:some_category\": Loading index from cache.\n"
+ end
+ end
+
+end
6 server/spec/spec_helper.rb
View
@@ -18,12 +18,12 @@
PICKY_ROOT = 'spec/test_directory'
puts "Redefined PICKY_ROOT to '#{PICKY_ROOT}' for the tests."
-Picky.logger = Logger.new STDOUT
-puts "Using STDOUT as test log."
+Picky.logger = Picky::Loggers::Silent.new STDOUT
+puts "Using Picky::Loggers::Silent.new(STDOUT) as test logger."
class Object
def exclaim(*)
- # Tke sound of silence.
+ # The sound of silence.
end
end
2  server/test_project/app.rb
View
@@ -20,6 +20,8 @@ def initialize id, name
end
+Picky.logger = Picky::Loggers::Verbose.new
+
class BookSearch < Sinatra::Application
include Picky
2  server/test_project/spec/integration_spec.rb
View
@@ -81,7 +81,7 @@
it 'finds the same after reloading' do
csv.search('soledad human').ids.should == [72]
- puts "Reloading the Indexes."
+ Picky.logger.info "Reloading"
Picky::Indexes.load
csv.search('soledad human').ids.should == [72]
end
2  version.rb
View
@@ -1,3 +1,3 @@
module Picky
- VERSION = '4.1.0'
+ VERSION = '4.2.0'
end
Please sign in to comment.
Something went wrong with that request. Please try again.