From 175a93b1f143f0416669197c59164425535bea21 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Mon, 14 Sep 2020 13:41:10 +0200 Subject: [PATCH] re-add time to log entries saved to file (#67) --- main.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 66efa809..d36a778e 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,12 @@ const ( logDestinationFile ) +type logWriter func([]byte) (int, error) + +func (f logWriter) Write(p []byte) (int, error) { + return f(p) +} + type program struct { conf *conf logFile *os.File @@ -106,6 +112,8 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { } } + log.SetOutput(logWriter(p.logOutput)) + p.log("rtsp-simple-server %s", Version) if conf.Metrics { @@ -156,16 +164,20 @@ func (p *program) log(format string, args ...interface{}) { countPublisher := atomic.LoadInt64(&p.countPublisher) countReader := atomic.LoadInt64(&p.countReader) - line := fmt.Sprintf("[%d/%d/%d] "+format, append([]interface{}{countClient, - countPublisher, countReader}, args...)...) + log.Printf(fmt.Sprintf("[%d/%d/%d] "+format, append([]interface{}{countClient, + countPublisher, countReader}, args...)...)) +} +func (p *program) logOutput(line []byte) (int, error) { if _, ok := p.conf.logDestinationsParsed[logDestinationStdout]; ok { - log.Println(line) + print(string(line)) } if _, ok := p.conf.logDestinationsParsed[logDestinationFile]; ok { - p.logFile.WriteString(line + "\n") + p.logFile.Write(line) } + + return len(line), nil } func (p *program) run() {