<commit_before>package rpc2

import (
	"encoding/json"
	"fmt"
	"io"
	"net"
	"os"
	"time"
)

type Profiler interface {
	Stop()
}

type LogInterface interface {
	TransportStart()
	TransportError(error)
	ServerCall(int, string, error, interface{})
	ServerReply(int, string, error, interface{})
	ClientCall(int, string, interface{})
	ClientReply(int, string, error, interface{})
	StartProfiler(format string, args ...interface{}) Profiler
	UnexpectedReply(int)
	Warning(format string, args ...interface{})
}

type LogFactory interface {
	NewLog(net.Addr) LogInterface
}

type LogOutput interface {
	Error(s string)
	Warning(s string)
	Info(s string)
	Debug(s string)
	Profile(s string)
}

type LogOptions interface {
	ShowAddress() bool
	ShowArg() bool
	ShowResult() bool
	DoProfile() bool
}

//-------------------------------------------------

type SimpleLogFactory struct {
	out  LogOutput
	opts LogOptions
}

type SimpleLog struct {
	addr net.Addr
	out  LogOutput
	opts LogOptions
}

type SimpleLogOutput struct{}
type SimpleLogOptions struct{}

func (so SimpleLogOutput) Info(s string)    { fmt.Fprintf(os.Stdout, "[I] %s\n", s) }
func (so SimpleLogOutput) Error(s string)   { fmt.Fprintf(os.Stdout, "[E] %s\n", s) }
func (so SimpleLogOutput) Debug(s string)   { fmt.Fprintf(os.Stdout, "[D] %s\n", s) }
func (so SimpleLogOutput) Warning(s string) { fmt.Fprintf(os.Stdout, "[W] %s\n", s) }
func (so SimpleLogOutput) Profile(s string) { fmt.Fprintf(os.Stdout, "[P] %s\n", s) }

func (so SimpleLogOptions) ShowAddress() bool { return true }
func (so SimpleLogOptions) ShowArg() bool     { return true }
func (so SimpleLogOptions) ShowResult() bool  { return true }
func (so SimpleLogOptions) DoProfile() bool   { return true }

func NewSimpleLogFactory(out LogOutput, opts LogOptions) SimpleLogFactory {
	if out == nil {
		out = SimpleLogOutput{}
	}
	if opts == nil {
		opts = SimpleLogOptions{}
	}
	ret := SimpleLogFactory{out, opts}
	return ret
}

func (s SimpleLogFactory) NewLog(a net.Addr) LogInterface {
	ret := SimpleLog{a, s.out, s.opts}
	ret.TransportStart()
	return ret
}

func AddrToString(addr net.Addr) string {
	if addr == nil {
		return "-"
	} else {
		c := addr.String()
		if len(c) == 0 {
			return addr.Network()
		} else {
			return addr.Network() + "://" + c
		}
	}
}

func (l SimpleLog) TransportStart() {
	l.out.Info(l.msg(true, "New connection"))
}

func (l SimpleLog) TransportError(e error) {
	if e != io.EOF {
		l.out.Error(l.msg(true, "Error in transport: %s", e.Error()))
	} else {
		l.out.Info(l.msg(true, "EOF"))
	}
	return
}

func (s SimpleLog) ServerReply(q int, meth string, err error, res interface{}) {
	s.trace("reply", "res", s.opts.ShowResult(), q, meth, err, res)
}
func (s SimpleLog) ServerCall(q int, meth string, err error, arg interface{}) {
	s.trace("serve", "arg", s.opts.ShowArg(), q, meth, err, arg)
}
func (s SimpleLog) ClientCall(q int, meth string, arg interface{}) {
	s.trace("call", "arg", s.opts.ShowArg(), q, meth, nil, arg)
}
func (s SimpleLog) ClientReply(q int, meth string, err error, res interface{}) {
	s.trace("reply", "res", s.opts.ShowResult(), q, meth, err, res)
}

func (s SimpleLog) trace(which string, objname string, verbose bool, q int, meth string, err error, obj interface{}) {
	args := []interface{}{which, q}
	fmts := "%s(%d):"
	if len(meth) > 0 {
		fmts += " method=%s;"
		args = append(args, meth)
	}
	fmts += " err=%s;"
	var es string
	if err == nil {
		es = "null"
	} else {
		es = err.Error()
	}
	args = append(args, es)
	if verbose {
		fmts += " %s=%s;"
		eb, err := json.Marshal(obj)
		var es string
		if err != nil {
			es = fmt.Sprintf(`{"error": "%s"}`, err.Error())
		} else {
			es = string(eb)
		}
		args = append(args, objname)
		args = append(args, es)
	}
	s.out.Debug(s.msg(false, fmts, args...))
}

func (s SimpleLog) StartProfiler(format string, args ...interface{}) Profiler {
	if s.opts.DoProfile() {
		return SimpleProfiler{
			start: time.Now(),
			msg:   fmt.Sprintf(format, args...),
			log:   s,
		}
	} else {
		return nil
	}
}

func (s SimpleLog) UnexpectedReply(seqno int) {<commit_msg>better options<commit_after>package rpc2

import (
	"encoding/json"
	"fmt"
	"io"
	"net"
	"os"
	"time"
)

type Profiler interface {
	Stop()
}

type LogInterface interface {
	TransportStart()
	TransportError(error)
	ServerCall(int, string, error, interface{})
	ServerReply(int, string, error, interface{})
	ClientCall(int, string, interface{})
	ClientReply(int, string, error, interface{})
	StartProfiler(format string, args ...interface{}) Profiler
	UnexpectedReply(int)
	Warning(format string, args ...interface{})
}

type LogFactory interface {
	NewLog(net.Addr) LogInterface
}

type LogOutput interface {
	Error(s string)
	Warning(s string)
	Info(s string)
	Debug(s string)
	Profile(s string)
}

type LogOptions interface {
	ShowAddress() bool
	ShowArg() bool
	ShowResult() bool
	Profile() bool
	ClientTrace() bool
	ServerTrace() bool
	TransportStart() bool
}

//-------------------------------------------------

type SimpleLogFactory struct {
	out  LogOutput
	opts LogOptions
}

type SimpleLog struct {
	addr net.Addr
	out  LogOutput
	opts LogOptions
}

type SimpleLogOutput struct{}
type SimpleLogOptions struct{}

func (so SimpleLogOutput) Info(s string)    { fmt.Fprintf(os.Stdout, "[I] %s\n", s) }
func (so SimpleLogOutput) Error(s string)   { fmt.Fprintf(os.Stdout, "[E] %s\n", s) }
func (so SimpleLogOutput) Debug(s string)   { fmt.Fprintf(os.Stdout, "[D] %s\n", s) }
func (so SimpleLogOutput) Warning(s string) { fmt.Fprintf(os.Stdout, "[W] %s\n", s) }
func (so SimpleLogOutput) Profile(s string) { fmt.Fprintf(os.Stdout, "[P] %s\n", s) }

func (so SimpleLogOptions) ShowAddress() bool    { return true }
func (so SimpleLogOptions) ShowArg() bool        { return true }
func (so SimpleLogOptions) ShowResult() bool     { return true }
func (so SimpleLogOptions) Profile() bool        { return true }
func (so SimpleLogOptions) ClientTrace() bool    { return true }
func (so SimpleLogOptions) ServerTrace() bool    { return true }
func (so SimpleLogOptions) TransportStart() bool { return true }

func NewSimpleLogFactory(out LogOutput, opts LogOptions) SimpleLogFactory {
	if out == nil {
		out = SimpleLogOutput{}
	}
	if opts == nil {
		opts = SimpleLogOptions{}
	}
	ret := SimpleLogFactory{out, opts}
	return ret
}

func (s SimpleLogFactory) NewLog(a net.Addr) LogInterface {
	ret := SimpleLog{a, s.out, s.opts}
	ret.TransportStart()
	return ret
}

func AddrToString(addr net.Addr) string {
	if addr == nil {
		return "-"
	} else {
		c := addr.String()
		if len(c) == 0 {
			return addr.Network()
		} else {
			return addr.Network() + "://" + c
		}
	}
}

func (l SimpleLog) TransportStart() {
	if l.opts.TransportStart() {
		l.out.Info(l.msg(true, "New connection"))
	}
}

func (l SimpleLog) TransportError(e error) {
	if e != io.EOF {
		l.out.Error(l.msg(true, "Error in transport: %s", e.Error()))
	} else if l.opts.TransportStart() {
		l.out.Info(l.msg(true, "EOF"))
	}
	return
}

func (s SimpleLog) ServerReply(q int, meth string, err error, res interface{}) {
	if s.opts.ServerTrace() {
		s.trace("reply", "res", s.opts.ShowResult(), q, meth, err, res)
	}
}
func (s SimpleLog) ServerCall(q int, meth string, err error, arg interface{}) {
	if s.opts.ServerTrace() {
		s.trace("serve", "arg", s.opts.ShowArg(), q, meth, err, arg)
	}
}
func (s SimpleLog) ClientCall(q int, meth string, arg interface{}) {
	if s.opts.ClientTrace() {
		s.trace("call", "arg", s.opts.ShowArg(), q, meth, nil, arg)
	}
}
func (s SimpleLog) ClientReply(q int, meth string, err error, res interface{}) {
	if s.opts.ClientTrace() {
		s.trace("reply", "res", s.opts.ShowResult(), q, meth, err, res)
	}
}

func (s SimpleLog) trace(which string, objname string, verbose bool, q int, meth string, err error, obj interface{}) {
	args := []interface{}{which, q}
	fmts := "%s(%d):"
	if len(meth) > 0 {
		fmts += " method=%s;"
		args = append(args, meth)
	}
	fmts += " err=%s;"
	var es string
	if err == nil {
		es = "null"
	} else {
		es = err.Error()
	}
	args = append(args, es)
	if verbose {
		fmts += " %s=%s;"
		eb, err := json.Marshal(obj)
		var es string
		if err != nil {
			es = fmt.Sprintf(`{"error": "%s"}`, err.Error())
		} else {
			es = string(eb)
		}
		args = append(args, objname)
		args = append(args, es)
	}
	s.out.Debug(s.msg(false, fmts, args...))
}

func (s SimpleLog) StartProfiler(format string, args ...interface{}) Profiler {
	if s.opts.Profile() {
		return SimpleProfiler{
			start: time.Now(),
			msg:   fmt.Sprintf(format, args...),
			log:   s,
		}
	} else {
		return nil
	}
}

func (s SimpleLog) UnexpectedReply(seqno int) {
{'fraction_non_alphanumeric': 0.1691415313225058, 'fraction_numerical': 0.0006960556844547563, 'mean_word_length': 3.7761772853185596, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '5057816', 'n_tokens_mistral': 3170, 'n_tokens_neox': 3085, 'n_words': 876}