<commit_before>import os
import socket
import os.path
import logging
import threading

from mirte.core import Module

class SocketServer(Module):
        def __init__(self, *args, **kwargs):
                super(SocketServer, self).__init__(*args, **kwargs)
                self.lock = threading.Lock()
                self.running = False
                self.handlers = set()
                self.n_conn = 0
        def create_listening_socket(self):
                raise NotImplementedError
        def create_handler(self, con, addr, logger):
                raise NotImplementedError
        def run(self):
                with self.lock:
                        assert not self.running
                        self.running = True
                        self.socket = self.create_listening_socket()
                        self.selectPool.register(self._on_socket_activity,
                                        [self.socket])
        def _on_socket_activity(self, rs, ws, xs):
                with self.lock:
                        con, addr = self.socket.accept()
                        self.n_conn += 1
                        self.threadPool.execute_named(self._handle_connection,
                                        '%s _handle_connection %s' % (
                                                self.l.name, self.n_conn),
                                                con, addr, self.n_conn)
                return True
        def _handle_connection(self, con, addr, n_conn):
                l = logging.getLogger("%s.%s" % (self.l.name, n_conn))
                l.info("Accepted connection from %s" % repr(addr))
                handler = self.create_handler(con, addr, l)
                with self.lock:
                        if not self.running:
                                return
                        self.handlers.add(handler)
                try:
                        handler.handle()
                except Exception:
                        self.l.exception("Uncaught exception")
                finally:
                        with self.lock:
                                self.handlers.remove(handler)
                        handler.cleanup()
        def stop(self):<commit_msg>Use LoggerAdapter instead of separate logger for each connection<commit_after>import os
import socket
import os.path
import logging
import threading

from mirte.core import Module

class SocketServer(Module):
        def __init__(self, *args, **kwargs):
                super(SocketServer, self).__init__(*args, **kwargs)
                self.lock = threading.Lock()
                self.running = False
                self.handlers = set()
                self.n_conn = 0
        def create_listening_socket(self):
                raise NotImplementedError
        def create_handler(self, con, addr, logger):
                raise NotImplementedError
        def run(self):
                with self.lock:
                        assert not self.running
                        self.running = True
                        self.socket = self.create_listening_socket()
                        self.selectPool.register(self._on_socket_activity,
                                        [self.socket])
        def _on_socket_activity(self, rs, ws, xs):
                with self.lock:
                        con, addr = self.socket.accept()
                        self.n_conn += 1
                        self.threadPool.execute_named(self._handle_connection,
                                        '%s _handle_connection %s' % (
                                                self.l.name, self.n_conn),
                                                con, addr, self.n_conn)
                return True
        def _handle_connection(self, con, addr, n_conn):
                l = logging.LoggerAdapter(self.l, {'sid': n_conn})
                l.info("Accepted connection from %s" % repr(addr))
                handler = self.create_handler(con, addr, l)
                with self.lock:
                        if not self.running:
                                return
                        self.handlers.add(handler)
                try:
                        handler.handle()
                except Exception:
                        self.l.exception("Uncaught exception")
                finally:
                        with self.lock:
                                self.handlers.remove(handler)
                        handler.cleanup()
        def stop(self):
{'fraction_non_alphanumeric': 0.07728181212524983, 'fraction_numerical': 0.0008882966910948257, 'mean_word_length': 1.1265344664778092, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4893071', 'n_tokens_mistral': 1064, 'n_tokens_neox': 990, 'n_words': 239}