[Patch] server: logs are reopenned when hg receives a HUP signal

Bertrand Petit elrond at phoe.frmug.org
Sun Dec 23 19:14:11 CST 2007


	Here is a set of two patches of the changes I made to
Mercurial to permit the use of system-profided log rotatting
facilities[1] on the logfiles produced by `hg serve' when in daemon
mode.

	As I'm a very new Mercurial user I refrained from using the
tip revision, instead I made my changes against 0.9.5 and merged them
later to tip. That explains the count of patches attached. Is it an
acceptable method?

[1] In my case it is newsyslog from FreeBSD.

-- 
%!PS
297.6 420.9 translate 90 rotate 0 setgray gsave 0 1 1{pop 0 180 moveto 100
180 170 100 170 -10 curveto 180 -9 180 -9 190 -10 curveto 190 100 100 180
0 180 curveto fill 180 rotate}for grestore/Bookman-LightItalic findfont
240 scalefont setfont -151.536392 -63.7998886 moveto (bp)show showpage
-------------- next part --------------
# HG changeset patch
# User Bertrand Petit <elrond at phoe.frmug.org>
# Date 1198456048 -3600
# Node ID 35cac4aa54a4ebf7d69fa0d5117811313be9e7ce
# Parent  9d6ad26fab10e06768858ed1a74eff80207cb0f5
# Parent  c34650e594cbe40b2805cbd8cb6ae05cc78bd07f
Merged to tip log re-openning on HUP signal reception.

diff -r 9d6ad26fab10 -r 35cac4aa54a4 mercurial/commands.py
--- a/mercurial/commands.py	Tue Dec 18 15:40:46 2007 -0600
+++ b/mercurial/commands.py	Mon Dec 24 01:27:28 2007 +0100
@@ -2287,6 +2287,9 @@ def serve(ui, repo, **opts):
 
     By default, the server logs accesses to stdout and errors to
     stderr.  Use the "-A" and "-E" options to log to files.
+
+    When run as a daemon, the logs are closed then re-opened when a
+    HUP signal is received.
     """
 
     if opts["stdio"]:
@@ -2298,7 +2301,7 @@ def serve(ui, repo, **opts):
 
     parentui = ui.parentui or ui
     optlist = ("name templates style address port ipv6"
-               " accesslog errorlog webdir_conf certificate")
+               " accesslog errorlog webdir_conf certificate daemon")
     for o in optlist.split():
         if opts[o]:
             parentui.setconfig("web", o, str(opts[o]))
diff -r 9d6ad26fab10 -r 35cac4aa54a4 mercurial/hgweb/server.py
--- a/mercurial/hgweb/server.py	Tue Dec 18 15:40:46 2007 -0600
+++ b/mercurial/hgweb/server.py	Mon Dec 24 01:27:28 2007 +0100
@@ -11,6 +11,7 @@ from hgweb_mod import hgweb
 from hgweb_mod import hgweb
 from hgwebdir_mod import hgwebdir
 from mercurial.i18n import gettext as _
+from signal import signal, SIGHUP
 
 def _splitURI(uri):
     """ Return path and query splited from uri
@@ -51,6 +52,7 @@ class _hgwebhandler(object, BaseHTTPServ
 
     def log_error(self, format, *args):
         self._log_any(self.server.errorlog, format, *args)
+        self.server.errorlog.flush()
 
     def log_message(self, format, *args):
         self._log_any(self.server.accesslog, format, *args)
@@ -197,8 +199,14 @@ def create_server(ui, repo):
 
     def openlog(opt, default):
         if opt and opt != '-':
-            return open(opt, 'w')
+            return open(opt, 'r+')
         return default
+
+    def open_access_log():
+        return openlog(myui.config("web", "accesslog", "-"), sys.stdout)
+
+    def open_error_log():
+        return openlog(myui.config("web", "errorlog", "-"), sys.stderr)
 
     if repo is None:
         myui = ui
@@ -209,8 +217,6 @@ def create_server(ui, repo):
     use_ipv6 = myui.configbool("web", "ipv6")
     webdir_conf = myui.config("web", "webdir_conf")
     ssl_cert = myui.config("web", "certificate")
-    accesslog = openlog(myui.config("web", "accesslog", "-"), sys.stdout)
-    errorlog = openlog(myui.config("web", "errorlog", "-"), sys.stderr)
 
     if use_threads:
         try:
@@ -235,8 +241,8 @@ def create_server(ui, repo):
 
         def __init__(self, *args, **kargs):
             BaseHTTPServer.HTTPServer.__init__(self, *args, **kargs)
-            self.accesslog = accesslog
-            self.errorlog = errorlog
+            self.accesslog = open_access_log()
+            self.errorlog = open_error_log()
             self.daemon_threads = True
             def make_handler():
                 if webdir_conf:
@@ -268,6 +274,15 @@ def create_server(ui, repo):
                 self.server_bind()
                 self.server_activate()
 
+            if ui.configbool("web","daemon"):
+                signal(SIGHUP,(lambda dummy1,dummy2: self._reopen_logs()))
+
+        def _reopen_logs(self):
+            self.accesslog.close()
+            self.accesslog = open_access_log()
+            self.errorlog.close()
+            self.errorlog = open_error_log()
+
     class IPv6HTTPServer(MercurialHTTPServer):
         address_family = getattr(socket, 'AF_INET6', None)
 
-------------- next part --------------
# HG changeset patch
# User Bertrand Petit <elrond at phoe.frmug.org>
# Date 1198453612 -3600
# Node ID c34650e594cbe40b2805cbd8cb6ae05cc78bd07f
# Parent  23889160905a1b09fffe1c07378e9fc1827606eb
server: logs are reopenned when hg receives a HUP signal.
It is convinient to rely on system-provided features to rotate log files.

diff -r 23889160905a -r c34650e594cb mercurial/commands.py
--- a/mercurial/commands.py	Fri Sep 07 16:48:42 2007 +0200
+++ b/mercurial/commands.py	Mon Dec 24 00:46:52 2007 +0100
@@ -2488,6 +2488,9 @@ def serve(ui, repo, **opts):
 
     By default, the server logs accesses to stdout and errors to
     stderr.  Use the "-A" and "-E" options to log to files.
+
+    When run as a daemon, the logs are closed then re-opened when a
+    HUP signal is received.
     """
 
     if opts["stdio"]:
@@ -2499,7 +2502,7 @@ def serve(ui, repo, **opts):
 
     parentui = ui.parentui or ui
     optlist = ("name templates style address port ipv6"
-               " accesslog errorlog webdir_conf certificate")
+               " accesslog errorlog webdir_conf certificate daemon")
     for o in optlist.split():
         if opts[o]:
             parentui.setconfig("web", o, str(opts[o]))
diff -r 23889160905a -r c34650e594cb mercurial/hgweb/server.py
--- a/mercurial/hgweb/server.py	Fri Sep 07 16:48:42 2007 +0200
+++ b/mercurial/hgweb/server.py	Mon Dec 24 00:46:52 2007 +0100
@@ -12,6 +12,7 @@ from hgwebdir_mod import hgwebdir
 from hgwebdir_mod import hgwebdir
 from request import wsgiapplication
 from mercurial.i18n import gettext as _
+from signal import signal, SIGHUP
 
 def _splitURI(uri):
     """ Return path and query splited from uri
@@ -49,6 +50,7 @@ class _hgwebhandler(object, BaseHTTPServ
         errorlog.write("%s - - [%s] %s\n" % (self.client_address[0],
                                              self.log_date_time_string(),
                                              format % args))
+        errorlog.flush()
 
     def log_message(self, format, *args):
         accesslog = self.server.accesslog
@@ -200,8 +202,14 @@ def create_server(ui, repo):
 
     def openlog(opt, default):
         if opt and opt != '-':
-            return open(opt, 'w')
+            return open(opt, 'r+')
         return default
+
+    def open_access_log():
+        return openlog(myui.config("web", "accesslog", "-"), sys.stdout)
+
+    def open_error_log():
+        return openlog(myui.config("web", "errorlog", "-"), sys.stderr)
 
     if repo is None:
         myui = ui
@@ -212,8 +220,6 @@ def create_server(ui, repo):
     use_ipv6 = myui.configbool("web", "ipv6")
     webdir_conf = myui.config("web", "webdir_conf")
     ssl_cert = myui.config("web", "certificate")
-    accesslog = openlog(myui.config("web", "accesslog", "-"), sys.stdout)
-    errorlog = openlog(myui.config("web", "errorlog", "-"), sys.stderr)
 
     if use_threads:
         try:
@@ -238,8 +244,8 @@ def create_server(ui, repo):
 
         def __init__(self, *args, **kargs):
             BaseHTTPServer.HTTPServer.__init__(self, *args, **kargs)
-            self.accesslog = accesslog
-            self.errorlog = errorlog
+            self.accesslog = open_access_log()
+            self.errorlog = open_error_log()
             self.daemon_threads = True
             def make_handler():
                 if webdir_conf:
@@ -271,6 +277,16 @@ def create_server(ui, repo):
                 self.server_bind()
                 self.server_activate()
 
+
+            if ui.configbool("web","daemon"):
+                signal(SIGHUP,(lambda dummy1,dummy2: self._reopen_logs()))
+
+        def _reopen_logs(self):
+            self.accesslog.close()
+            self.accesslog = open_access_log()
+            self.errorlog.close()
+            self.errorlog = open_error_log()
+
     class IPv6HTTPServer(MercurialHTTPServer):
         address_family = getattr(socket, 'AF_INET6', None)
 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20071224/378dcd6d/attachment.pgp 


More information about the Mercurial-devel mailing list