<commit_before>package weblog

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/websocket"
)

var upgrader = &websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}

func NewWebLogger() *WebLogger {
	ww := NewWebWriter()
	l := log.New(ww, "", log.Lshortfile)
	go ww.Run()

	return &WebLogger{l, ww}
}

type WebLogger struct {
	*log.Logger

	writer *WebWriter
}

func (wl *WebLogger) Handle(w http.ResponseWriter, r *http.Request) {
	wl.writer.Handle(w, r)
}

func NewWebWriter() *WebWriter {
	ww := &WebWriter{register: make(chan *connection),
		unregister:  make(chan *connection),
		connections: make(map[*connection]bool)}
	return ww
}

type WebWriter struct {
	register    chan *connection
	unregister  chan *connection
	connections map[*connection]bool
}

func (ww *WebWriter) Run() {
	for {
		select {
		case c := <-ww.unregister:
			ww.connections[c] = false
			delete(ww.connections, c)
			c.ws.Close()
		case c := <-ww.register:
			ww.connections[c] = true
			fmt.Println("Just registered a client")
		}
	}
}

func (ww *WebWriter) Write(p []byte) (int, error) {
	for conn := range ww.connections {
		conn.send <- p
	}

	return len(p), nil
}

func (ww *WebWriter) Handle(w http.ResponseWriter, r *http.Request) {
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		return
	}
	c := &connection{send: make(chan []byte, 256), ws: ws}
	ww.register <- c
	defer func() { ww.unregister <- c }()

	c.send <- []byte("Welcome to WebWriter!")
	c.writer()
}

type connection struct {
	send chan []byte
	ws   *websocket.Conn
}

func (c connection) writer() {
	for message := range c.send {
		err := c.ws.WriteMessage(websocket.TextMessage, message)
		if err != nil {
			break
		}<commit_msg>log: WebWriter -> webWriter, minimising public API<commit_after>package weblog

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/websocket"
)

var upgrader = &websocket.Upgrader{ReadBufferSize: 1024, WriteBufferSize: 1024}

func NewWebLogger() *WebLogger {
	ww := &webWriter{register: make(chan *connection),
		unregister:  make(chan *connection),
		connections: make(map[*connection]bool)}

	l := log.New(ww, "", log.Lshortfile)
	go ww.Run()

	return &WebLogger{l, ww}
}

type WebLogger struct {
	*log.Logger

	writer *webWriter
}

func (wl *WebLogger) Handle(w http.ResponseWriter, r *http.Request) {
	wl.writer.Handle(w, r)
}

type webWriter struct {
	register    chan *connection
	unregister  chan *connection
	connections map[*connection]bool
}

func (ww *webWriter) Run() {
	for {
		select {
		case c := <-ww.unregister:
			ww.connections[c] = false
			delete(ww.connections, c)
			c.ws.Close()
		case c := <-ww.register:
			ww.connections[c] = true
			fmt.Println("Just registered a client")
		}
	}
}

func (ww *webWriter) Write(p []byte) (int, error) {
	for conn := range ww.connections {
		conn.send <- p
	}

	return len(p), nil
}

func (ww *webWriter) Handle(w http.ResponseWriter, r *http.Request) {
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		return
	}
	c := &connection{send: make(chan []byte, 256), ws: ws}
	ww.register <- c
	defer func() { ww.unregister <- c }()

	c.send <- []byte("Welcome to the weblog!")
	c.writer()
}

type connection struct {
	send chan []byte
	ws   *websocket.Conn
}

func (c connection) writer() {
	for message := range c.send {
		err := c.ws.WriteMessage(websocket.TextMessage, message)
		if err != nil {
			break
		}
{'fraction_non_alphanumeric': 0.15191740412979352, 'fraction_numerical': 0.006489675516224189, 'mean_word_length': 3.8373751783166905, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2377884', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1239, 'n_words': 323}