package logger

import (
	"os"

	"gogs.carduccidante.edu.it/karmen/core/orm"
)

type JobLogger struct {
	job *orm.Job
}

func NewJobLogger(job *orm.Job) *JobLogger {
	return &JobLogger{job}
}

func (l *JobLogger) Write(p []byte) (n int, err error) {
	os.Stdout.Write(p)
	origLen := len(p)
	if len(p) > 0 && p[len(p)-1] == '\n' {
		p = p[:len(p)-1] // Cut terminating newline
	}
	l.job.Logs = append(l.job.Logs, &orm.Log{Content: string(p)})

	return origLen, nil
}
