Browse Source

add syslog log destination (#83)

pull/97/head
aler9 6 years ago
parent
commit
588878bcae
  1. 3
      conf.go
  2. 30
      log-syslog_unix.go
  3. 21
      log-syslog_win.go
  4. 36
      log.go
  5. 2
      rtsp-simple-server.yml

3
conf.go

@ -160,6 +160,9 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) {
case "file": case "file":
conf.logDestinationsParsed[logDestinationFile] = struct{}{} conf.logDestinationsParsed[logDestinationFile] = struct{}{}
case "syslog":
conf.logDestinationsParsed[logDestinationSyslog] = struct{}{}
default: default:
return nil, fmt.Errorf("unsupported log destination: %s", dest) return nil, fmt.Errorf("unsupported log destination: %s", dest)
} }

30
log-syslog_unix.go

@ -0,0 +1,30 @@
// +build !windows
package main
import (
"log/syslog"
)
type logSyslog struct {
inner *syslog.Writer
}
func newLogSyslog() (*logSyslog, error) {
inner, err := syslog.New(syslog.LOG_INFO|syslog.LOG_DAEMON, "rtsp-simple-server")
if err != nil {
return nil, err
}
return &logSyslog{
inner: inner,
}, nil
}
func (ls *logSyslog) close() {
ls.inner.Close()
}
func (ls *logSyslog) write(p []byte) (int, error) {
return ls.inner.Write(p)
}

21
log-syslog_win.go

@ -0,0 +1,21 @@
// +build windows
package main
import (
"fmt"
)
type logSyslog struct {
}
func newLogSyslog() (*logSyslog, error) {
return nil, fmt.Errorf("not implemented on windows")
}
func (ls *logSyslog) close() {
}
func (ls *logSyslog) write(p []byte) (int, error) {
return 0, nil
}

36
log.go

@ -10,22 +10,34 @@ type logDestination int
const ( const (
logDestinationStdout logDestination = iota logDestinationStdout logDestination = iota
logDestinationFile logDestinationFile
logDestinationSyslog
) )
type logHandler struct { type logHandler struct {
logDestinationsParsed map[logDestination]struct{} logDestinations map[logDestination]struct{}
logFile *os.File logFile *os.File
logSyslog *logSyslog
} }
func newLogHandler(logDestinationsParsed map[logDestination]struct{}, logFilePath string) (*logHandler, error) { func newLogHandler(logDestinations map[logDestination]struct{}, logFilePath string) (*logHandler, error) {
lh := &logHandler{ lh := &logHandler{
logDestinationsParsed: logDestinationsParsed, logDestinations: logDestinations,
} }
if _, ok := logDestinationsParsed[logDestinationFile]; ok { if _, ok := logDestinations[logDestinationFile]; ok {
var err error var err error
lh.logFile, err = os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) lh.logFile, err = os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil { if err != nil {
lh.close()
return nil, err
}
}
if _, ok := logDestinations[logDestinationSyslog]; ok {
var err error
lh.logSyslog, err = newLogSyslog()
if err != nil {
lh.close()
return nil, err return nil, err
} }
} }
@ -35,22 +47,28 @@ func newLogHandler(logDestinationsParsed map[logDestination]struct{}, logFilePat
return lh, nil return lh, nil
} }
func (lh *logHandler) close() error { func (lh *logHandler) close() {
if lh.logFile != nil { if lh.logFile != nil {
lh.logFile.Close() lh.logFile.Close()
} }
return nil if lh.logSyslog != nil {
lh.logSyslog.close()
}
} }
func (lh *logHandler) Write(p []byte) (int, error) { func (lh *logHandler) Write(p []byte) (int, error) {
if _, ok := lh.logDestinationsParsed[logDestinationStdout]; ok { if _, ok := lh.logDestinations[logDestinationStdout]; ok {
print(string(p)) print(string(p))
} }
if _, ok := lh.logDestinationsParsed[logDestinationFile]; ok { if _, ok := lh.logDestinations[logDestinationFile]; ok {
lh.logFile.Write(p) lh.logFile.Write(p)
} }
if _, ok := lh.logDestinations[logDestinationSyslog]; ok {
lh.logSyslog.write(p)
}
return len(p), nil return len(p), nil
} }

2
rtsp-simple-server.yml

@ -24,7 +24,7 @@ runOnConnect:
metrics: no metrics: no
# enable pprof on port 9999 to monitor performances # enable pprof on port 9999 to monitor performances
pprof: no pprof: no
# destinations of log messages; available options are 'stdout' and 'file' # destinations of log messages; available options are 'stdout', 'file' and 'syslog'
logDestinations: [stdout] logDestinations: [stdout]
# if 'file' is in logDestinations, this is the file that will receive the logs # if 'file' is in logDestinations, this is the file that will receive the logs
logFile: rtsp-simple-server.log logFile: rtsp-simple-server.log

Loading…
Cancel
Save