forked from External/mediamtx
move log handling into dedicated file
This commit is contained in:
parent
f5936d67ed
commit
a8ec4a4cf6
2 changed files with 64 additions and 38 deletions
56
log.go
Normal file
56
log.go
Normal file
|
|
@ -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
46
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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue