~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to lib/webrick/log.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# log.rb -- Log Class
 
3
#
 
4
# Author: IPR -- Internet Programming with Ruby -- writers
 
5
# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
 
6
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
 
7
# reserved.
 
8
#
 
9
# $IPR: log.rb,v 1.26 2002/10/06 17:06:10 gotoyuzo Exp $
 
10
 
 
11
module WEBrick
 
12
  class BasicLog
 
13
    # log-level constant
 
14
    FATAL, ERROR, WARN, INFO, DEBUG = 1, 2, 3, 4, 5
 
15
 
 
16
    attr_accessor :level
 
17
 
 
18
    def initialize(log_file=nil, level=nil)
 
19
      @level = level || INFO
 
20
      case log_file
 
21
      when String
 
22
        @log = open(log_file, "a+")
 
23
        @log.sync = true
 
24
        @opened = true
 
25
      when NilClass
 
26
        @log = $stderr
 
27
      else
 
28
        @log = log_file  # requires "<<". (see BasicLog#log)
 
29
      end
 
30
    end
 
31
 
 
32
    def close
 
33
      @log.close if @opened
 
34
      @log = nil
 
35
    end
 
36
 
 
37
    def log(level, data)
 
38
      if @log && level <= @level
 
39
        data += "\n" if /\n\Z/ !~ data
 
40
        @log << data
 
41
      end
 
42
    end
 
43
 
 
44
    def <<(obj)
 
45
      log(INFO, obj.to_s)
 
46
    end
 
47
 
 
48
    def fatal(msg) log(FATAL, "FATAL " << format(msg)); end
 
49
    def error(msg) log(ERROR, "ERROR " << format(msg)); end
 
50
    def warn(msg)  log(WARN,  "WARN  " << format(msg)); end
 
51
    def info(msg)  log(INFO,  "INFO  " << format(msg)); end
 
52
    def debug(msg) log(DEBUG, "DEBUG " << format(msg)); end
 
53
 
 
54
    def fatal?; @level >= FATAL; end
 
55
    def error?; @level >= ERROR; end
 
56
    def warn?;  @level >= WARN; end
 
57
    def info?;  @level >= INFO; end
 
58
    def debug?; @level >= DEBUG; end
 
59
 
 
60
    private
 
61
 
 
62
    def format(arg)
 
63
      str = if arg.is_a?(Exception)
 
64
        "#{arg.class}: #{arg.message}\n\t" <<
 
65
        arg.backtrace.join("\n\t") << "\n"
 
66
      elsif arg.respond_to?(:to_str)
 
67
        arg.to_str
 
68
      else
 
69
        arg.inspect
 
70
      end
 
71
    end
 
72
  end
 
73
 
 
74
  class Log < BasicLog
 
75
    attr_accessor :time_format 
 
76
 
 
77
    def initialize(log_file=nil, level=nil)
 
78
      super(log_file, level)
 
79
      @time_format = "[%Y-%m-%d %H:%M:%S]"
 
80
    end
 
81
 
 
82
    def log(level, data)
 
83
      tmp = Time.now.strftime(@time_format)
 
84
      tmp << " " << data
 
85
      super(level, tmp)
 
86
    end
 
87
  end
 
88
end