<commit_before>package main

import (
	"errors"
	"fmt"
	"log"
	"os"
)

type Sample interface {
	String() string
}

type PhoutSample struct {
	ts             float64
	tag            string
	rt             int
	connect        int
	send           int
	latency        int
	receive        int
	interval_event int
	egress         int
	igress         int
	netCode        int
	protoCode      int
}

func (ps *PhoutSample) String() string {
	return fmt.Sprintf(
		"%.3f\t%s\t%d\t"+
			"%d\t%d\t"+
			"%d\t%d\t"+
			"%d\t"+
			"%d\t%d\t"+
			"%d\t%d",
		ps.ts, ps.tag, ps.rt,
		ps.connect, ps.send,
		ps.latency, ps.receive,
		ps.interval_event,
		ps.egress, ps.igress,
		ps.netCode, ps.protoCode,
	)
}

type PhantomCompatible interface {
	Sample
	PhoutSample() *PhoutSample
}

type ResultListener interface {
	Start()
	Sink() chan Sample
}

type resultListener struct {
	sink chan Sample
}

func (rl *resultListener) Sink() chan Sample {
	return rl.sink
}

type LoggingResultListener struct {
	resultListener
}

func (rl *LoggingResultListener) Start() {
	go func() {
		for r := range rl.sink {
			log.Println(r)
		}
	}()
}

func NewLoggingResultListener() (rl ResultListener, err error) {
	return &LoggingResultListener{
		resultListener: resultListener{
			sink: make(chan Sample, 32),
		},
	}, nil
}

type PhoutResultListener struct {
	resultListener
	phout *os.File
}

func (rl *PhoutResultListener) Start() {
	go func() {
		for r := range rl.sink {
			pc, ok := r.(PhantomCompatible)
			if ok {
				rl.phout.WriteString(fmt.Sprintf("%s\n", pc.PhoutSample()))
			} else {
				log.Panic("Not phantom compatible sample")
				return
			}
		}
	}()
}

func NewPhoutResultListener(filename string) (rl ResultListener, err error) {
	var phoutFile *os.File
	if filename == "" {
		phoutFile = os.Stdout
	} else {
		phoutFile, err = os.Create(filename)
	}
	return &PhoutResultListener{
		resultListener: resultListener{
			sink: make(chan Sample, 32),
		},
		phout: phoutFile,
	}, nil
}

type phoutResultListenerFactory struct {
	listeners map[string]ResultListener
}

func (prls *phoutResultListenerFactory) Create(c *ResultListenerConfig) (rl ResultListener, err error) {
	rl, ok := prls.listeners[c.Destination]
	if !ok {
		rl, err = NewPhoutResultListener(c.Destination)
		if err != nil {
			return nil, err
		} else {
			prls.listeners[c.Destination] = rl<commit_msg>sample log open file options<commit_after>package main

import (
	"errors"
	"fmt"
	"log"
	"os"
)

type Sample interface {
	String() string
}

type PhoutSample struct {
	ts             float64
	tag            string
	rt             int
	connect        int
	send           int
	latency        int
	receive        int
	interval_event int
	egress         int
	igress         int
	netCode        int
	protoCode      int
}

func (ps *PhoutSample) String() string {
	return fmt.Sprintf(
		"%.3f\t%s\t%d\t"+
			"%d\t%d\t"+
			"%d\t%d\t"+
			"%d\t"+
			"%d\t%d\t"+
			"%d\t%d",
		ps.ts, ps.tag, ps.rt,
		ps.connect, ps.send,
		ps.latency, ps.receive,
		ps.interval_event,
		ps.egress, ps.igress,
		ps.netCode, ps.protoCode,
	)
}

type PhantomCompatible interface {
	Sample
	PhoutSample() *PhoutSample
}

type ResultListener interface {
	Start()
	Sink() chan Sample
}

type resultListener struct {
	sink chan Sample
}

func (rl *resultListener) Sink() chan Sample {
	return rl.sink
}

type LoggingResultListener struct {
	resultListener
}

func (rl *LoggingResultListener) Start() {
	go func() {
		for r := range rl.sink {
			log.Println(r)
		}
	}()
}

func NewLoggingResultListener() (rl ResultListener, err error) {
	return &LoggingResultListener{
		resultListener: resultListener{
			sink: make(chan Sample, 32),
		},
	}, nil
}

type PhoutResultListener struct {
	resultListener
	phout *os.File
}

func (rl *PhoutResultListener) Start() {
	go func() {
		for r := range rl.sink {
			pc, ok := r.(PhantomCompatible)
			if ok {
				rl.phout.WriteString(fmt.Sprintf("%s\n", pc.PhoutSample()))
			} else {
				log.Panic("Not phantom compatible sample")
				return
			}
		}
	}()
}

func NewPhoutResultListener(filename string) (rl ResultListener, err error) {
	var phoutFile *os.File
	if filename == "" {
		phoutFile = os.Stdout
	} else {
		phoutFile, err = os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0666)
	}
	return &PhoutResultListener{
		resultListener: resultListener{
			sink: make(chan Sample, 32),
		},
		phout: phoutFile,
	}, nil
}

type phoutResultListenerFactory struct {
	listeners map[string]ResultListener
}

func (prls *phoutResultListenerFactory) Create(c *ResultListenerConfig) (rl ResultListener, err error) {
	rl, ok := prls.listeners[c.Destination]
	if !ok {
		rl, err = NewPhoutResultListener(c.Destination)
		if err != nil {
			return nil, err
		} else {
			prls.listeners[c.Destination] = rl
{'fraction_non_alphanumeric': 0.12270066889632107, 'fraction_numerical': 0.003762541806020067, 'mean_word_length': 3.157254561251086, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1487437', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1769, 'n_words': 425}