Browse Source

add options logDestinations, logFile (#37)

pull/52/head v0.9.6
aler9 6 years ago
parent
commit
a5bd05dcfc
  1. 3
      Makefile
  2. 23
      conf.go
  3. 32
      main.go
  4. 4
      rtsp-simple-server.yml

3
Makefile

@ -68,7 +68,8 @@ define CONFIG_RUN
#rtspPort: 8555 #rtspPort: 8555
#rtpPort: 8002 #rtpPort: 8002
#rtcpPort: 8003 #rtcpPort: 8003
metrics: yes #metrics: yes
logDestinations: [stdout, file]
paths: paths:
all: all:

23
conf.go

@ -44,6 +44,9 @@ type conf struct {
authMethodsParsed []gortsplib.AuthMethod `` authMethodsParsed []gortsplib.AuthMethod ``
Metrics bool `yaml:"metrics"` Metrics bool `yaml:"metrics"`
Pprof bool `yaml:"pprof"` Pprof bool `yaml:"pprof"`
LogDestinations []string `yaml:"logDestinations"`
logDestinationsParsed map[logDestination]struct{} ``
LogFile string `yaml:"logFile"`
Paths map[string]*confPath `yaml:"paths"` Paths map[string]*confPath `yaml:"paths"`
} }
@ -144,6 +147,26 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) {
} }
} }
if len(conf.LogDestinations) == 0 {
conf.LogDestinations = []string{"stdout"}
}
conf.logDestinationsParsed = make(map[logDestination]struct{})
for _, dest := range conf.LogDestinations {
switch dest {
case "stdout":
conf.logDestinationsParsed[logDestinationStdout] = struct{}{}
case "file":
conf.logDestinationsParsed[logDestinationFile] = struct{}{}
default:
return nil, fmt.Errorf("unsupported log destination: %s", dest)
}
}
if conf.LogFile == "" {
conf.LogFile = "rtsp-simple-server.log"
}
if len(conf.Paths) == 0 { if len(conf.Paths) == 0 {
conf.Paths = map[string]*confPath{ conf.Paths = map[string]*confPath{
"all": {}, "all": {},

32
main.go

@ -21,6 +21,13 @@ const (
pprofAddress = ":9999" pprofAddress = ":9999"
) )
type logDestination int
const (
logDestinationStdout logDestination = iota
logDestinationFile
)
type programEvent interface { type programEvent interface {
isProgramEvent() isProgramEvent()
} }
@ -161,6 +168,7 @@ func (programEventTerminate) isProgramEvent() {}
type program struct { type program struct {
conf *conf conf *conf
logFile *os.File
metrics *metrics metrics *metrics
serverRtsp *serverTcp serverRtsp *serverTcp
serverRtp *serverUdp serverRtp *serverUdp
@ -202,6 +210,15 @@ 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 {
log.Fatal("ERR:", err)
}
}
p.log("rtsp-simple-server %s", Version)
for path, confp := range conf.Paths { for path, confp := range conf.Paths {
if path == "all" { if path == "all" {
continue continue
@ -216,8 +233,6 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
} }
} }
p.log("rtsp-simple-server %s", Version)
if conf.Metrics { if conf.Metrics {
p.metrics = newMetrics(p) p.metrics = newMetrics(p)
} }
@ -263,8 +278,15 @@ func newProgram(args []string, stdin io.Reader) (*program, error) {
} }
func (p *program) log(format string, args ...interface{}) { func (p *program) log(format string, args ...interface{}) {
log.Printf("[%d/%d/%d] "+format, append([]interface{}{len(p.clients), line := fmt.Sprintf("[%d/%d/%d] "+format, append([]interface{}{len(p.clients),
p.publisherCount, p.readerCount}, args...)...) p.publisherCount, p.readerCount}, args...)...)
if _, ok := p.conf.logDestinationsParsed[logDestinationStdout]; ok {
log.Println(line)
}
if _, ok := p.conf.logDestinationsParsed[logDestinationFile]; ok {
p.logFile.WriteString(line + "\n")
}
} }
func (p *program) run() { func (p *program) run() {
@ -491,6 +513,10 @@ outer:
p.metrics.close() p.metrics.close()
} }
if p.logFile != nil {
p.logFile.Close()
}
close(p.events) close(p.events)
close(p.done) close(p.done)
} }

4
rtsp-simple-server.yml

@ -21,6 +21,10 @@ authMethods: [basic, digest]
metrics: false metrics: false
# enable pprof on port 9999 to monitor performances # enable pprof on port 9999 to monitor performances
pprof: false pprof: false
# destinations of log messages; available options are 'stdout' and 'file'
logDestinations: [stdout]
# if 'file' is in logDestinations, this is the file that will receive the logs
logFile: rtsp-simple-server.log
# these settings are path-dependent. The settings under the path 'all' are # these settings are path-dependent. The settings under the path 'all' are
# applied to all paths that do not match a specific entry. # applied to all paths that do not match a specific entry.

Loading…
Cancel
Save