Mercurial > public > mercurial-scm > hg
comparison mercurial/hgweb/server.py @ 34706:8782076874f5
hgweb: fix logging to use native strings as appropriate
Kind of a tangled mess, but now logging works in both Python 2 and 3.
# no-check-commit because of the interface required by Python's HTTP
server code.
Differential Revision: https://phab.mercurial-scm.org/D1080
author | Augie Fackler <augie@google.com> |
---|---|
date | Sat, 14 Oct 2017 15:43:06 -0400 |
parents | 8e5132ece156 |
children | 01206460897a |
comparison
equal
deleted
inserted
replaced
34705:23ed47a895d5 | 34706:8782076874f5 |
---|---|
15 import traceback | 15 import traceback |
16 | 16 |
17 from ..i18n import _ | 17 from ..i18n import _ |
18 | 18 |
19 from .. import ( | 19 from .. import ( |
20 encoding, | |
20 error, | 21 error, |
21 pycompat, | 22 pycompat, |
22 util, | 23 util, |
23 ) | 24 ) |
24 | 25 |
65 def __init__(self, *args, **kargs): | 66 def __init__(self, *args, **kargs): |
66 self.protocol_version = r'HTTP/1.1' | 67 self.protocol_version = r'HTTP/1.1' |
67 httpservermod.basehttprequesthandler.__init__(self, *args, **kargs) | 68 httpservermod.basehttprequesthandler.__init__(self, *args, **kargs) |
68 | 69 |
69 def _log_any(self, fp, format, *args): | 70 def _log_any(self, fp, format, *args): |
70 fp.write("%s - - [%s] %s\n" % (self.client_address[0], | 71 fp.write(pycompat.sysbytes( |
71 self.log_date_time_string(), | 72 r"%s - - [%s] %s" % (self.client_address[0], |
72 format % args)) | 73 self.log_date_time_string(), |
74 format % args)) + '\n') | |
73 fp.flush() | 75 fp.flush() |
74 | 76 |
75 def log_error(self, format, *args): | 77 def log_error(self, format, *args): |
76 self._log_any(self.server.errorlog, format, *args) | 78 self._log_any(self.server.errorlog, format, *args) |
77 | 79 |
78 def log_message(self, format, *args): | 80 def log_message(self, format, *args): |
79 self._log_any(self.server.accesslog, format, *args) | 81 self._log_any(self.server.accesslog, format, *args) |
80 | 82 |
81 def log_request(self, code='-', size='-'): | 83 def log_request(self, code=r'-', size=r'-'): |
82 xheaders = [] | 84 xheaders = [] |
83 if util.safehasattr(self, 'headers'): | 85 if util.safehasattr(self, 'headers'): |
84 xheaders = [h for h in self.headers.items() | 86 xheaders = [h for h in self.headers.items() |
85 if h[0].startswith('x-')] | 87 if h[0].startswith(r'x-')] |
86 self.log_message('"%s" %s %s%s', | 88 self.log_message(r'"%s" %s %s%s', |
87 self.requestline, str(code), str(size), | 89 self.requestline, str(code), str(size), |
88 ''.join([' %s:%s' % h for h in sorted(xheaders)])) | 90 r''.join([r' %s:%s' % h for h in sorted(xheaders)])) |
89 | 91 |
90 def do_write(self): | 92 def do_write(self): |
91 try: | 93 try: |
92 self.do_hgweb() | 94 self.do_hgweb() |
93 except socket.error as inst: | 95 except socket.error as inst: |
99 self.do_write() | 101 self.do_write() |
100 except Exception: | 102 except Exception: |
101 self._start_response("500 Internal Server Error", []) | 103 self._start_response("500 Internal Server Error", []) |
102 self._write("Internal Server Error") | 104 self._write("Internal Server Error") |
103 self._done() | 105 self._done() |
104 tb = "".join(traceback.format_exception(*sys.exc_info())) | 106 tb = r"".join(traceback.format_exception(*sys.exc_info())) |
105 self.log_error("Exception happened during processing " | 107 # We need a native-string newline to poke in the log |
106 "request '%s':\n%s", self.path, tb) | 108 # message, because we won't get a newline when using an |
109 # r-string. This is the easy way out. | |
110 newline = chr(10) | |
111 self.log_error(r"Exception happened during processing " | |
112 r"request '%s':%s%s", self.path, newline, tb) | |
107 | 113 |
108 def do_GET(self): | 114 def do_GET(self): |
109 self.do_POST() | 115 self.do_POST() |
110 | 116 |
111 def do_hgweb(self): | 117 def do_hgweb(self): |
329 port = util.getport(ui.config('web', 'port')) | 335 port = util.getport(ui.config('web', 'port')) |
330 try: | 336 try: |
331 return cls(ui, app, (address, port), handler) | 337 return cls(ui, app, (address, port), handler) |
332 except socket.error as inst: | 338 except socket.error as inst: |
333 raise error.Abort(_("cannot start server at '%s:%d': %s") | 339 raise error.Abort(_("cannot start server at '%s:%d': %s") |
334 % (address, port, inst.args[1])) | 340 % (address, port, encoding.strtolocal(inst.args[1]))) |