Browse Source

move log handling into dedicated file

pull/97/head
aler9 6 years ago
parent
commit
a8ec4a4cf6
  1. 56
      log.go
  2. 46
      main.go

56
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
}

46
main.go

@ -19,22 +19,9 @@ const (
checkPathPeriod = 5 * time.Second 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 { type program struct {
conf *conf conf *conf
logFile *os.File logHandler *logHandler
metrics *metrics metrics *metrics
pprof *pprof pprof *pprof
paths map[string]*path paths map[string]*path
@ -81,8 +68,14 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
return nil, err return nil, err
} }
logHandler, err := newLogHandler(conf.logDestinationsParsed, conf.LogFile)
if err != nil {
return nil, err
}
p := &program{ p := &program{
conf: conf, conf: conf,
logHandler: logHandler,
paths: make(map[string]*path), paths: make(map[string]*path),
clients: make(map[*client]struct{}), clients: make(map[*client]struct{}),
udpPublishersMap: newUdpPublisherMap(), udpPublishersMap: newUdpPublisherMap(),
@ -101,15 +94,6 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
done: make(chan struct{}), 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) p.log("rtsp-simple-server %s", Version)
if conf.Metrics { if conf.Metrics {
@ -164,18 +148,6 @@ func (p *program) log(format string, args ...interface{}) {
countPublisher, countReader}, args...)...)) 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() { func (p *program) run() {
if p.metrics != nil { if p.metrics != nil {
go p.metrics.run() go p.metrics.run()
@ -369,9 +341,7 @@ outer:
p.pprof.close() p.pprof.close()
} }
if p.logFile != nil { p.logHandler.close()
p.logFile.Close()
}
close(p.metricsGather) close(p.metricsGather)
close(p.clientNew) close(p.clientNew)

Loading…
Cancel
Save