# HG changeset patch # User mpm@selenic.com # Date 1120452665 28800 # Node ID 8e82fd763be22ef2ec1445e29d2264f47531fe4c # Parent 40a66d464ac2e916898fa7eccc027202de3c4c2f [PATCH] Get "hg serve" to optionally log accesses and errors to files -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [PATCH] Get "hg serve" to optionally log accesses and errors to files From: Bryan O'Sullivan Get "hg serve" to log accesses and errors to files. manifest hash: 573ef524d84cc7d2777f5fd982f2ef47f4bcf668 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCyMA5ywK+sNU5EO8RAp1eAJoD6Qqy6XcGInzZKdo0Qp7gLttYzACfRywL fSGapmCAIaZPoBvoxXTk8Zo= =ZicU -----END PGP SIGNATURE----- diff --git a/doc/hg.1.txt b/doc/hg.1.txt --- a/doc/hg.1.txt +++ b/doc/hg.1.txt @@ -274,13 +274,18 @@ revert [names ...]:: root:: Print the root directory of the current repository. -serve [-a addr -n name -p port -t templatedir]:: +serve [-a addr -l logfile -n name -p port -t templatedir]:: Start a local HTTP repository browser and pull server. + By default, the server logs accesses to stdout and errors to + stderr. Use the "-A" and "-E" options to log to files. + options: - -a, --address address to use - -p, --port port to use (default: 8000) - -n, --name name to show in web pages (default: working dir) + -A, --accesslog name of access log file to write to + -E, --errorlog name of error log file to write to + -a, --address address to use + -p, --port port to use (default: 8000) + -n, --name name to show in web pages (default: working dir) -t, --templatedir web templates to use status:: diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -794,8 +794,13 @@ def root(ui, repo): def serve(ui, repo, **opts): """export the repository via HTTP""" + def openlog(opt, default): + if opts[opt] and opts[opt] != '-': return open(opts[opt], 'w') + else: return default httpd = hgweb.create_server(repo.root, opts["name"], opts["templates"], - opts["address"], opts["port"]) + opts["address"], opts["port"], + openlog('accesslog', sys.stdout), + openlog('errorlog', sys.stderr)) if ui.verbose: addr, port = httpd.socket.getsockname() if addr == '0.0.0.0': @@ -805,9 +810,9 @@ def serve(ui, repo, **opts): addr = socket.gethostbyaddr(addr)[0] except: pass if port != 80: - ui.status('listening on http://%s:%d/\n' % (addr, port)) + ui.status('listening at http://%s:%d/\n' % (addr, port)) else: - ui.status('listening on http://%s/\n' % addr) + ui.status('listening at http://%s/\n' % addr) httpd.serve_forever() def status(ui, repo): @@ -973,10 +978,12 @@ table = { ("r", "rev", "", "revision")], "hg revert [files|dirs]"), "root": (root, [], "hg root"), - "^serve": (serve, [('p', 'port', 8000, 'listen port'), - ('a', 'address', '', 'interface address'), - ('n', 'name', os.getcwd(), 'repository name'), - ('t', 'templates', "", 'template map')], + "^serve": (serve, [('A', 'accesslog', '', 'access log file'), + ('E', 'errorlog', '', 'error log file'), + ('p', 'port', 8000, 'listen port'), + ('a', 'address', '', 'interface address'), + ('n', 'name', os.getcwd(), 'repository name'), + ('t', 'templates', "", 'template map')], "hg serve [options]"), "^status": (status, [], 'hg status'), "tag": (tag, [('t', 'text', "", 'commit text'), diff --git a/mercurial/hgweb.py b/mercurial/hgweb.py --- a/mercurial/hgweb.py +++ b/mercurial/hgweb.py @@ -694,12 +694,22 @@ class hgweb: else: write(self.t("error")) -def create_server(path, name, templates, address, port): +def create_server(path, name, templates, address, port, + accesslog = sys.stdout, errorlog = sys.stderr): import BaseHTTPServer - import sys, os class hgwebhandler(BaseHTTPServer.BaseHTTPRequestHandler): + def log_error(self, format, *args): + errorlog.write("%s - - [%s] %s\n" % (self.address_string(), + self.log_date_time_string(), + format % args)) + + def log_message(self, format, *args): + accesslog.write("%s - - [%s] %s\n" % (self.address_string(), + self.log_date_time_string(), + format % args)) + def do_POST(self): try: self.do_hgweb() @@ -761,6 +771,8 @@ def create_server(path, name, templates, hg = hgweb(path, name, templates) return BaseHTTPServer.HTTPServer((address, port), hgwebhandler) -def server(path, name, templates, address, port): - httpd = create_server(path, name, templates, address, port) +def server(path, name, templates, address, port, + accesslog = sys.stdout, errorlog = sys.stderr): + httpd = create_server(path, name, templates, address, port, + accesslog, errorlog) httpd.serve_forever()