# HG changeset patch # User Nicolas Dumazet # Date 2009-05-25 09:14:32 # Node ID e0ed17984a481ed6de6e5c86d45e66d9292da94b # Parent 5d8021ac0e1917370de9ade65749540fe00c965c cmdutil: service: logfile option to redirect stdout & stderr in a file diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -537,7 +537,7 @@ def copy(ui, repo, pats, opts, rename=Fa 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 @@ def service(opts, parentfn=None, initfn= 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()