fix some logging bugs
authorCameron Patrick <[email protected]>
Sun, 1 Aug 2004 08:57:17 +0000 (08:57 +0000)
committerCameron Patrick <[email protected]>
Sun, 1 Aug 2004 08:57:17 +0000 (08:57 +0000)
sql-edition/servers/VendServer.py

index b272eba..02d5f9b 100755 (executable)
@@ -451,9 +451,7 @@ class VendConfigFile:
                                self.__dict__[option] = value
                
                except ConfigParser.Error, e:
-                       logging.critical("Error reading config file "+config_file+": " + str(e))
-                       logging.critical("Bailing out")
-                       sys.exit(1)
+                       raise SystemExit("Error reading config file "+config_file+": " + str(e))
 
 def create_pid_file(name):
        try:
@@ -472,9 +470,9 @@ def set_stuff_up():
        signal.signal(signal.SIGINT, stop_server)
 
        options = parse_args()
-       set_up_logging(options)
        config_opts = VendConfigFile(options.config_file, config_options)
        if options.daemon: become_daemon()
+       set_up_logging(options)
        if options.pid_file != '': create_pid_file(options.pid_file)
 
        return options, config_opts
@@ -489,9 +487,10 @@ def clean_up_nicely(options, config_opts):
 def set_up_logging(options):
        logger = logging.getLogger()
        
-       stderr_logger = logging.StreamHandler(sys.stderr)
-       stderr_logger.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
-       logger.addHandler(stderr_logger)
+       if not options.daemon:
+               stderr_logger = logging.StreamHandler(sys.stderr)
+               stderr_logger.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
+               logger.addHandler(stderr_logger)
        
        if options.log_file != '':
                try:
@@ -519,8 +518,15 @@ def become_daemon():
        os.dup2(fd, 0)
        os.dup2(fd, 1)
        os.dup2(fd, 2)
-       if os.fork() != 0:
-               sys.exit(0)
+       logging.debug('about to fork')
+       try:
+               if os.fork() != 0:
+                       logging.debug('in parent process: dying')
+                       sys.exit(0)
+               logging.debug('back from fork')
+               os.setsid()
+       except OSError, e:
+               logging.critical('failed to fork: '+e)
 
 def do_vend_server(options, config_opts):
        while True:

UCC git Repository :: git.ucc.asn.au