From a8ec4a4cf6df42c91a2ecffc032184d799864479 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sat, 19 Sep 2020 00:19:55 +0200 Subject: [PATCH] move log handling into dedicated file --- log.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 46 ++++++++-------------------------------------- 2 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 log.go diff --git a/log.go b/log.go new file mode 100644 index 00000000..34eae71d --- /dev/null +++ b/log.go @@ -0,0 +1,56 @@ +package main + +import ( + "log" + "os" +) + +type logDestination int + +const ( + logDestinationStdout logDestination = iota + logDestinationFile +) + +type logHandler struct { + logDestinationsParsed map[logDestination]struct{} + logFile *os.File +} + +func newLogHandler(logDestinationsParsed map[logDestination]struct{}, logFilePath string) (*logHandler, error) { + lh := &logHandler{ + logDestinationsParsed: logDestinationsParsed, + } + + if _, ok := logDestinationsParsed[logDestinationFile]; ok { + var err error + lh.logFile, err = os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return nil, err + } + } + + log.SetOutput(lh) + + return lh, nil +} + +func (lh *logHandler) close() error { + if lh.logFile != nil { + lh.logFile.Close() + } + + return nil +} + +func (lh *logHandler) Write(p []byte) (int, error) { + if _, ok := lh.logDestinationsParsed[logDestinationStdout]; ok { + print(string(p)) + } + + if _, ok := lh.logDestinationsParsed[logDestinationFile]; ok { + lh.logFile.Write(p) + } + + return len(p), nil +} diff --git a/main.go b/main.go index 4c567673..d85d1a75 100644 --- a/main.go +++ b/main.go @@ -19,22 +19,9 @@ const ( checkPathPeriod = 5 * time.Second ) -type logDestination int - -const ( - logDestinationStdout logDestination = iota - 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 + logHandler *logHandler metrics *metrics pprof *pprof paths map[string]*path @@ -81,8 +68,14 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { return nil, err } + logHandler, err := newLogHandler(conf.logDestinationsParsed, conf.LogFile) + if err != nil { + return nil, err + } + p := &program{ conf: conf, + logHandler: logHandler, paths: make(map[string]*path), clients: make(map[*client]struct{}), udpPublishersMap: newUdpPublisherMap(), @@ -101,15 +94,6 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { done: make(chan struct{}), } - if _, ok := p.conf.logDestinationsParsed[logDestinationFile]; ok { - p.logFile, err = os.OpenFile(p.conf.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - return nil, err - } - } - - log.SetOutput(logWriter(p.logOutput)) - p.log("rtsp-simple-server %s", Version) if conf.Metrics { @@ -164,18 +148,6 @@ func (p *program) log(format string, args ...interface{}) { countPublisher, countReader}, args...)...)) } -func (p *program) logOutput(line []byte) (int, error) { - if _, ok := p.conf.logDestinationsParsed[logDestinationStdout]; ok { - print(string(line)) - } - - if _, ok := p.conf.logDestinationsParsed[logDestinationFile]; ok { - p.logFile.Write(line) - } - - return len(line), nil -} - func (p *program) run() { if p.metrics != nil { go p.metrics.run() @@ -369,9 +341,7 @@ outer: p.pprof.close() } - if p.logFile != nil { - p.logFile.Close() - } + p.logHandler.close() close(p.metricsGather) close(p.clientNew)