changeset 8789:e0ed17984a48

cmdutil: service: logfile option to redirect stdout & stderr in a file
author Nicolas Dumazet <nicdumz.commits@gmail.com>
date Mon, 25 May 2009 18:14:32 +0900
parents 5d8021ac0e19
children 72af80052bd9
files mercurial/cmdutil.py
diffstat 1 files changed, 13 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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()