You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger.rb 828B

1234567891011121314151617181920212223242526272829303132333435363738
  1. module CryptCheck
  2. class Logger
  3. LEVELS = %i(trace debug info warning error fatal none)
  4. @@level = :info
  5. def self.level=(level)
  6. @@level = level.to_sym
  7. end
  8. def self.log(level, string=nil, output: $stdout, &block)
  9. return unless enabled? level
  10. output.puts(string ? string : block.call)
  11. end
  12. if Object.respond_to? :ai
  13. def self.ap(name, object)
  14. self.debug { "#{name} : #{object.ai}" }
  15. end
  16. else
  17. def self.ap(name, object)
  18. self.debug { "#{name} : #{object}" }
  19. end
  20. end
  21. LEVELS.each do |level|
  22. class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
  23. def self.#{level}(string=nil, output: $stdout, &block)
  24. self.log :#{level}, string, output: output, &block
  25. end
  26. RUBY_EVAL
  27. end
  28. private
  29. def self.enabled?(level)
  30. LEVELS.index(level) >= LEVELS.index(@@level)
  31. end
  32. end
  33. end