clogger 0.0.3
[clogger.git] / lib / clogger.rb
blob8f3a8f5c1c2fa5aa3f9dd8b5774aecda189ea85f
1 # -*- encoding: binary -*-
2 class Clogger
3   VERSION = '0.0.3'
5   OP_LITERAL = 0
6   OP_REQUEST = 1
7   OP_RESPONSE = 2
8   OP_SPECIAL = 3
9   OP_EVAL = 4
10   OP_TIME_LOCAL = 5
11   OP_TIME_UTC = 6
12   OP_REQUEST_TIME = 7
13   OP_TIME = 8
14   OP_COOKIE = 9
16   # support nginx variables that are less customizable than our own
17   ALIASES = {
18     '$request_time' => '$request_time{3}',
19     '$time_local' => '$time_local{%d/%b/%Y:%H:%M:%S %z}',
20     '$msec' => '$time{3}',
21     '$usec' => '$time{6}',
22   }
24   SPECIAL_VARS = {
25     :body_bytes_sent => 0,
26     :status => 1,
27     :request => 2, # REQUEST_METHOD PATH_INFO?QUERY_STRING HTTP_VERSION
28     :request_length => 3, # env['rack.input'].size
29     :response_length => 4, # like body_bytes_sent, except "-" instead of "0"
30     :ip => 5, # HTTP_X_FORWARDED_FOR || REMOTE_ADDR || -
31     :pid => 6, # getpid()
32     :request_uri => 7
33   }
35 private
37   CGI_ENV = Regexp.new('\A\$(' <<
38       %w(remote_addr remote_ident remote_user
39          path_info query_string script_name
40          server_name server_port).join('|') << ')\z').freeze
42   SCAN = /([^$]*)(\$+(?:env\{\w+(?:\.[\w\.]+)?\}|
43                         e\{[^\}]+\}|
44                         (?:request_)?time\{\d+\}|
45                         time_(?:utc|local)\{[^\}]+\}|
46                         \w*))?([^$]*)/x
48   def compile_format(str)
49     rv = []
50     str.scan(SCAN).each do |pre,tok,post|
51       rv << [ OP_LITERAL, pre ] if pre && pre != ""
53       unless tok.nil?
54         if tok.sub!(/\A(\$+)\$/, '$')
55           rv << [ OP_LITERAL, $1.dup ]
56         end
58         compat = ALIASES[tok] and tok = compat
60         case tok
61         when /\A(\$*)\z/
62           rv << [ OP_LITERAL, $1.dup ]
63         when /\A\$env\{(\w+(?:\.[\w\.]+))\}\z/
64           rv << [ OP_REQUEST, $1.freeze ]
65         when /\A\$e\{([^\}]+)\}\z/
66           rv << [ OP_EVAL, $1.dup ]
67         when /\A\$cookie_(\w+)\z/
68           rv << [ OP_COOKIE, $1.dup.freeze ]
69         when CGI_ENV, /\A\$(http_\w+)\z/
70           rv << [ OP_REQUEST, $1.upcase.freeze ]
71         when /\A\$sent_http_(\w+)\z/
72           rv << [ OP_RESPONSE, $1.downcase.tr('_','-').freeze ]
73         when /\A\$time_local\{([^\}]+)\}\z/
74           rv << [ OP_TIME_LOCAL, $1.dup ]
75         when /\A\$time_utc\{([^\}]+)\}\z/
76           rv << [ OP_TIME_UTC, $1.dup ]
77         when /\A\$time\{(\d+)\}\z/
78           rv << [ OP_TIME, *usec_conv_pair(tok, $1.to_i) ]
79         when /\A\$request_time\{(\d+)\}\z/
80           rv << [ OP_REQUEST_TIME, *usec_conv_pair(tok, $1.to_i) ]
81         else
82           tok_sym = tok[1..-1].to_sym
83           if special_code = SPECIAL_VARS[tok_sym]
84             rv << [ OP_SPECIAL, special_code ]
85           else
86             raise ArgumentError, "unable to make sense of token: #{tok}"
87           end
88         end
89       end
91       rv << [ OP_LITERAL, post ] if post && post != ""
92     end
94     # auto-append a newline
95     last = rv.last or return rv
96     op = last.first
97     if (op == OP_LITERAL && /\n\z/ !~ last.last) || op != OP_LITERAL
98       rv << [ OP_LITERAL, "\n" ]
99     end
101     rv
102   end
104   def usec_conv_pair(tok, prec)
105     if prec == 0
106       [ "%d", 1 ] # stupid...
107     elsif prec > 6
108       raise ArgumentError, "#{tok}: too high precision: #{prec} (max=6)"
109     else
110       [ "%d.%0#{prec}d", 10 ** (6 - prec) ]
111     end
112   end
114   def need_response_headers?(fmt_ops)
115     fmt_ops.any? { |op| OP_RESPONSE == op[0] }
116   end
118   def need_wrap_body?(fmt_ops)
119     fmt_ops.any? do |op|
120       (OP_REQUEST_TIME == op[0]) || (OP_SPECIAL == op[0] &&
121         (SPECIAL_VARS[:body_bytes_sent] == op[1] ||
122          SPECIAL_VARS[:response_length] == op[1]))
123     end
124   end
128 require 'clogger/format'
130 begin
131   require 'clogger_ext'
132 rescue LoadError
133   require 'clogger/pure'