##// END OF EJS Templates
[PATCH] Get "hg serve" to optionally log accesses and errors to files...
mpm@selenic.com -
r605:8e82fd76 default
parent child Browse files
Show More
@@ -274,13 +274,18 b' revert [names ...]::'
274 root::
274 root::
275 Print the root directory of the current repository.
275 Print the root directory of the current repository.
276
276
277 serve [-a addr -n name -p port -t templatedir]::
277 serve [-a addr -l logfile -n name -p port -t templatedir]::
278 Start a local HTTP repository browser and pull server.
278 Start a local HTTP repository browser and pull server.
279
279
280 By default, the server logs accesses to stdout and errors to
281 stderr. Use the "-A" and "-E" options to log to files.
282
280 options:
283 options:
281 -a, --address <addr> address to use
284 -A, --accesslog <file> name of access log file to write to
282 -p, --port <n> port to use (default: 8000)
285 -E, --errorlog <file> name of error log file to write to
283 -n, --name <name> name to show in web pages (default: working dir)
286 -a, --address <addr> address to use
287 -p, --port <n> port to use (default: 8000)
288 -n, --name <name> name to show in web pages (default: working dir)
284 -t, --templatedir <path> web templates to use
289 -t, --templatedir <path> web templates to use
285
290
286 status::
291 status::
@@ -794,8 +794,13 b' def root(ui, repo):'
794
794
795 def serve(ui, repo, **opts):
795 def serve(ui, repo, **opts):
796 """export the repository via HTTP"""
796 """export the repository via HTTP"""
797 def openlog(opt, default):
798 if opts[opt] and opts[opt] != '-': return open(opts[opt], 'w')
799 else: return default
797 httpd = hgweb.create_server(repo.root, opts["name"], opts["templates"],
800 httpd = hgweb.create_server(repo.root, opts["name"], opts["templates"],
798 opts["address"], opts["port"])
801 opts["address"], opts["port"],
802 openlog('accesslog', sys.stdout),
803 openlog('errorlog', sys.stderr))
799 if ui.verbose:
804 if ui.verbose:
800 addr, port = httpd.socket.getsockname()
805 addr, port = httpd.socket.getsockname()
801 if addr == '0.0.0.0':
806 if addr == '0.0.0.0':
@@ -805,9 +810,9 b' def serve(ui, repo, **opts):'
805 addr = socket.gethostbyaddr(addr)[0]
810 addr = socket.gethostbyaddr(addr)[0]
806 except: pass
811 except: pass
807 if port != 80:
812 if port != 80:
808 ui.status('listening on http://%s:%d/\n' % (addr, port))
813 ui.status('listening at http://%s:%d/\n' % (addr, port))
809 else:
814 else:
810 ui.status('listening on http://%s/\n' % addr)
815 ui.status('listening at http://%s/\n' % addr)
811 httpd.serve_forever()
816 httpd.serve_forever()
812
817
813 def status(ui, repo):
818 def status(ui, repo):
@@ -973,10 +978,12 b' table = {'
973 ("r", "rev", "", "revision")],
978 ("r", "rev", "", "revision")],
974 "hg revert [files|dirs]"),
979 "hg revert [files|dirs]"),
975 "root": (root, [], "hg root"),
980 "root": (root, [], "hg root"),
976 "^serve": (serve, [('p', 'port', 8000, 'listen port'),
981 "^serve": (serve, [('A', 'accesslog', '', 'access log file'),
977 ('a', 'address', '', 'interface address'),
982 ('E', 'errorlog', '', 'error log file'),
978 ('n', 'name', os.getcwd(), 'repository name'),
983 ('p', 'port', 8000, 'listen port'),
979 ('t', 'templates', "", 'template map')],
984 ('a', 'address', '', 'interface address'),
985 ('n', 'name', os.getcwd(), 'repository name'),
986 ('t', 'templates', "", 'template map')],
980 "hg serve [options]"),
987 "hg serve [options]"),
981 "^status": (status, [], 'hg status'),
988 "^status": (status, [], 'hg status'),
982 "tag": (tag, [('t', 'text', "", 'commit text'),
989 "tag": (tag, [('t', 'text', "", 'commit text'),
@@ -694,12 +694,22 b' class hgweb:'
694 else:
694 else:
695 write(self.t("error"))
695 write(self.t("error"))
696
696
697 def create_server(path, name, templates, address, port):
697 def create_server(path, name, templates, address, port,
698 accesslog = sys.stdout, errorlog = sys.stderr):
698
699
699 import BaseHTTPServer
700 import BaseHTTPServer
700 import sys, os
701
701
702 class hgwebhandler(BaseHTTPServer.BaseHTTPRequestHandler):
702 class hgwebhandler(BaseHTTPServer.BaseHTTPRequestHandler):
703 def log_error(self, format, *args):
704 errorlog.write("%s - - [%s] %s\n" % (self.address_string(),
705 self.log_date_time_string(),
706 format % args))
707
708 def log_message(self, format, *args):
709 accesslog.write("%s - - [%s] %s\n" % (self.address_string(),
710 self.log_date_time_string(),
711 format % args))
712
703 def do_POST(self):
713 def do_POST(self):
704 try:
714 try:
705 self.do_hgweb()
715 self.do_hgweb()
@@ -761,6 +771,8 b' def create_server(path, name, templates,'
761 hg = hgweb(path, name, templates)
771 hg = hgweb(path, name, templates)
762 return BaseHTTPServer.HTTPServer((address, port), hgwebhandler)
772 return BaseHTTPServer.HTTPServer((address, port), hgwebhandler)
763
773
764 def server(path, name, templates, address, port):
774 def server(path, name, templates, address, port,
765 httpd = create_server(path, name, templates, address, port)
775 accesslog = sys.stdout, errorlog = sys.stderr):
776 httpd = create_server(path, name, templates, address, port,
777 accesslog, errorlog)
766 httpd.serve_forever()
778 httpd.serve_forever()
General Comments 0
You need to be logged in to leave comments. Login now