comparison mercurial/cmdutil.py @ 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 c5f36402daad
children 92fc57c9f9d1
comparison
equal deleted inserted replaced
8788:5d8021ac0e19 8789:e0ed17984a48
535 if errors: 535 if errors:
536 ui.warn(_('(consider using --after)\n')) 536 ui.warn(_('(consider using --after)\n'))
537 537
538 return errors 538 return errors
539 539
540 def service(opts, parentfn=None, initfn=None, runfn=None): 540 def service(opts, parentfn=None, initfn=None, runfn=None, logfile=None):
541 '''Run a command as a service.''' 541 '''Run a command as a service.'''
542 542
543 if opts['daemon'] and not opts['daemon_pipefds']: 543 if opts['daemon'] and not opts['daemon_pipefds']:
544 rfd, wfd = os.pipe() 544 rfd, wfd = os.pipe()
545 args = sys.argv[:] 545 args = sys.argv[:]
579 pass 579 pass
580 os.write(wfd, 'y') 580 os.write(wfd, 'y')
581 os.close(wfd) 581 os.close(wfd)
582 sys.stdout.flush() 582 sys.stdout.flush()
583 sys.stderr.flush() 583 sys.stderr.flush()
584 fd = os.open(util.nulldev, os.O_RDWR) 584
585 if fd != 0: os.dup2(fd, 0) 585 nullfd = os.open(util.nulldev, os.O_RDWR)
586 if fd != 1: os.dup2(fd, 1) 586 logfilefd = nullfd
587 if fd != 2: os.dup2(fd, 2) 587 if logfile:
588 if fd not in (0, 1, 2): os.close(fd) 588 logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND)
589 os.dup2(nullfd, 0)
590 os.dup2(logfilefd, 1)
591 os.dup2(logfilefd, 2)
592 if nullfd not in (0, 1, 2):
593 os.close(nullfd)
594 if logfile and logfilefd not in (0, 1, 2):
595 os.close(logfilefd)
589 596
590 if runfn: 597 if runfn:
591 return runfn() 598 return runfn()
592 599
593 class changeset_printer(object): 600 class changeset_printer(object):