Ejemplo n.º 1
0
 def log_record(self, record):
     logger = get_console_logger(record.name)
     logger.handle(record)
Ejemplo n.º 2
0
############################################################
# System handlers used internally by the Cynic server      #
############################################################

[handler:unixlog]
# a logging server that accepts connections over Unix socket
# from multiple local processes to output passed log records
class = cynic.handlers.log.LogRecordHandler
host = %(socketpath)s
port = 0
family = unix
""" % dict(socketpath=LOG_UNIX_SOCKET)

# Setup our console logger
logger = get_console_logger('server')


# taken from logging.config
def _resolve(name):
    """Resolve a dotted name to a global object."""
    name = name.split('.')
    used = name.pop(0)
    found = __import__(used)
    for n in name:
        used = used + '.' + n
        try:
            found = getattr(found, n)
        except AttributeError:
            __import__(used)
            found = getattr(found, n)
Ejemplo n.º 3
0
    def run(self):
        try:
            child_pids = []
            while True:
                try:
                    events = self.poller.poll(POLL_TIMEOUT)
                except select.error as e:
                    code, msg = e.args
                    if code == errno.EINTR:
                        continue
                    else:
                        raise

                for fd, flag in events:
                    # we're interested only in READ events
                    if not flag & READ_ONLY:
                        continue

                    # retrieve the actual socket and handler class
                    # from its file descriptor
                    handler_config = self.fd2config[fd]

                    # socket is ready to accept a connection
                    socket = handler_config.socket
                    try:
                        conn, client_address = socket.accept()
                    except IOError as e:
                        code, msg = e.args
                        if code == errno.EINTR:
                            continue
                        else:
                            raise

                    # spawn a child that will handle the request (connection)
                    pid = os.fork()
                    if pid == 0: # child
                        for config in self.fd2config.values():
                            config.socket.close()
                        # run a handler
                        klass = handler_config.klass
                        handler = klass(
                            conn, client_address, *handler_config.args)
                        try:
                            handler.handle()
                        except KeyboardInterrupt:
                            pass
                        except:
                            log = get_console_logger(klass.__name__)
                            log.exception('Exception when handling a request')
                        # off we go
                        os._exit(0)
                    else:
                        # this is parent
                        child_pids.append(pid)
                        # close unused connected socket
                        conn.close()
        except KeyboardInterrupt:
            for pid in child_pids:
                try:
                    os.kill(pid, signal.SIGTERM)
                except:
                    pass
Ejemplo n.º 4
0
 def log_record(self, record):
     logger = get_console_logger(record.name)
     logger.handle(record)