public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
wincent (author)
Sun Mar 02 16:58:06 -0800 2008
macournoyer (committer)
Sun Mar 02 17:07:01 -0800 2008
commit  e546c5da487d51056631ac49caf9e699da6b1808
tree    9aefc467a1530f1e9db5c5466fa4b218effb68f1
parent  59a972ced83e99eec7e95896fc5a6bdc32aa9ca6
thin / lib / thin / logging.rb
100644 49 lines (43 sloc) 1.557 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Thin
  # To be included in classes to allow some basic logging
  # that can be silenced (<tt>Logging.silent=</tt>) or made
  # more verbose.
  # <tt>Logging.debug=</tt>: log all error backtrace and messages
  # logged with +debug+.
  # <tt>Logging.trace=</tt>: log all raw request and response and
  # messages logged with +trace+.
  module Logging
    class << self
      attr_writer :trace, :debug, :silent
      
      def trace?; !@silent && @trace end
      def debug?; !@silent && @debug end
      def silent?; @silent end
    end
    
    # Deprecated silencer methods, those are now a module methods
    def silent
      warn "`#{self.class.name}\#silent` deprecated, use `Thin::Logging.silent?` instead"
      Logging.silent?
    end
    def silent=(value)
      warn "`#{self.class.name}\#silent=` deprecated, use `Thin::Logging.silent = #{value}` instead"
      Logging.silent = value
    end
    
    protected
      # Log a message to the console
      def log(msg)
        puts msg unless Logging.silent?
      end
      
      # Log a message to the console if tracing is activated
      def trace(msg=nil)
        log msg || yield if Logging.trace?
      end
      
      # Log a message to the console if debugging is activated
      def debug(msg=nil)
        log msg || yield if Logging.debug?
      end
      
      # Log an error backtrace if debugging is activated
      def log_error(e=$!)
        debug "#{e}\n\t" + e.backtrace.join("\n\t")
      end
  end
end