[PATCH] Get "hg serve" to optionally log accesses and errors to files
authormpm@selenic.com
Sun, 03 Jul 2005 20:51:05 -0800
changeset 605 8e82fd763be2
parent 604 40a66d464ac2
child 606 ea4526f9b1a5
[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 <bos@serpentine.com> 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-----
doc/hg.1.txt
mercurial/commands.py
mercurial/hgweb.py
--- a/doc/hg.1.txt	Sun Jul 03 20:49:48 2005 -0800
+++ b/doc/hg.1.txt	Sun Jul 03 20:51:05 2005 -0800
@@ -274,13 +274,18 @@
 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 <addr> address to use
-    -p, --port <n>       port to use (default: 8000)
-    -n, --name <name>    name to show in web pages (default: working dir)
+    -A, --accesslog <file>   name of access log file to write to
+    -E, --errorlog <file>    name of error log file to write to
+    -a, --address <addr>     address to use
+    -p, --port <n>           port to use (default: 8000)
+    -n, --name <name>        name to show in web pages (default: working dir)
     -t, --templatedir <path> web templates to use
 
 status::
--- a/mercurial/commands.py	Sun Jul 03 20:49:48 2005 -0800
+++ b/mercurial/commands.py	Sun Jul 03 20:51:05 2005 -0800
@@ -794,8 +794,13 @@
 
 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 @@
                 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 @@
                 ("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'),
--- a/mercurial/hgweb.py	Sun Jul 03 20:49:48 2005 -0800
+++ b/mercurial/hgweb.py	Sun Jul 03 20:51:05 2005 -0800
@@ -694,12 +694,22 @@
         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 @@
     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()