mercurial/cmdutil.py
changeset 8789 e0ed17984a48
parent 8778 c5f36402daad
child 8798 92fc57c9f9d1
--- a/mercurial/cmdutil.py	Sat Jun 13 17:39:01 2009 +0200
+++ b/mercurial/cmdutil.py	Mon May 25 18:14:32 2009 +0900
@@ -537,7 +537,7 @@
 
     return errors
 
-def service(opts, parentfn=None, initfn=None, runfn=None):
+def service(opts, parentfn=None, initfn=None, runfn=None, logfile=None):
     '''Run a command as a service.'''
 
     if opts['daemon'] and not opts['daemon_pipefds']:
@@ -581,11 +581,18 @@
         os.close(wfd)
         sys.stdout.flush()
         sys.stderr.flush()
-        fd = os.open(util.nulldev, os.O_RDWR)
-        if fd != 0: os.dup2(fd, 0)
-        if fd != 1: os.dup2(fd, 1)
-        if fd != 2: os.dup2(fd, 2)
-        if fd not in (0, 1, 2): os.close(fd)
+
+        nullfd = os.open(util.nulldev, os.O_RDWR)
+        logfilefd = nullfd
+        if logfile:
+            logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND)
+        os.dup2(nullfd, 0)
+        os.dup2(logfilefd, 1)
+        os.dup2(logfilefd, 2)
+        if nullfd not in (0, 1, 2):
+            os.close(nullfd)
+        if logfile and logfilefd not in (0, 1, 2):
+            os.close(logfilefd)
 
     if runfn:
         return runfn()