11 changed files with 166 additions and 154 deletions
@ -1,74 +0,0 @@
@@ -1,74 +0,0 @@
|
||||
package main |
||||
|
||||
import ( |
||||
"log" |
||||
"os" |
||||
) |
||||
|
||||
type logDestination int |
||||
|
||||
const ( |
||||
logDestinationStdout logDestination = iota |
||||
logDestinationFile |
||||
logDestinationSyslog |
||||
) |
||||
|
||||
type logHandler struct { |
||||
logDestinations map[logDestination]struct{} |
||||
logFile *os.File |
||||
logSyslog *logSyslog |
||||
} |
||||
|
||||
func newLogHandler(logDestinations map[logDestination]struct{}, logFilePath string) (*logHandler, error) { |
||||
lh := &logHandler{ |
||||
logDestinations: logDestinations, |
||||
} |
||||
|
||||
if _, ok := logDestinations[logDestinationFile]; ok { |
||||
var err error |
||||
lh.logFile, err = os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
||||
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 |
||||
} |
||||
} |
||||
|
||||
log.SetOutput(lh) |
||||
|
||||
return lh, nil |
||||
} |
||||
|
||||
func (lh *logHandler) close() { |
||||
if lh.logFile != nil { |
||||
lh.logFile.Close() |
||||
} |
||||
|
||||
if lh.logSyslog != nil { |
||||
lh.logSyslog.close() |
||||
} |
||||
} |
||||
|
||||
func (lh *logHandler) Write(p []byte) (int, error) { |
||||
if _, ok := lh.logDestinations[logDestinationStdout]; ok { |
||||
print(string(p)) |
||||
} |
||||
|
||||
if _, ok := lh.logDestinations[logDestinationFile]; ok { |
||||
lh.logFile.Write(p) |
||||
} |
||||
|
||||
if _, ok := lh.logDestinations[logDestinationSyslog]; ok { |
||||
lh.logSyslog.write(p) |
||||
} |
||||
|
||||
return len(p), nil |
||||
} |
||||
@ -0,0 +1,77 @@
@@ -0,0 +1,77 @@
|
||||
package loghandler |
||||
|
||||
import ( |
||||
"io" |
||||
"log" |
||||
"os" |
||||
|
||||
"github.com/aler9/rtsp-simple-server/syslog" |
||||
) |
||||
|
||||
type Destination int |
||||
|
||||
const ( |
||||
DestinationStdout Destination = iota |
||||
DestinationFile |
||||
DestinationSyslog |
||||
) |
||||
|
||||
type LogHandler struct { |
||||
destinations map[Destination]struct{} |
||||
file *os.File |
||||
syslog io.WriteCloser |
||||
} |
||||
|
||||
func New(destinations map[Destination]struct{}, filePath string) (*LogHandler, error) { |
||||
lh := &LogHandler{ |
||||
destinations: destinations, |
||||
} |
||||
|
||||
if _, ok := destinations[DestinationFile]; ok { |
||||
var err error |
||||
lh.file, err = os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
||||
if err != nil { |
||||
lh.Close() |
||||
return nil, err |
||||
} |
||||
} |
||||
|
||||
if _, ok := destinations[DestinationSyslog]; ok { |
||||
var err error |
||||
lh.syslog, err = syslog.New("rtsp-simple-server") |
||||
if err != nil { |
||||
lh.Close() |
||||
return nil, err |
||||
} |
||||
} |
||||
|
||||
log.SetOutput(lh) |
||||
|
||||
return lh, nil |
||||
} |
||||
|
||||
func (lh *LogHandler) Close() { |
||||
if lh.file != nil { |
||||
lh.file.Close() |
||||
} |
||||
|
||||
if lh.syslog != nil { |
||||
lh.syslog.Close() |
||||
} |
||||
} |
||||
|
||||
func (lh *LogHandler) Write(p []byte) (int, error) { |
||||
if _, ok := lh.destinations[DestinationStdout]; ok { |
||||
print(string(p)) |
||||
} |
||||
|
||||
if _, ok := lh.destinations[DestinationFile]; ok { |
||||
lh.file.Write(p) |
||||
} |
||||
|
||||
if _, ok := lh.destinations[DestinationSyslog]; ok { |
||||
lh.syslog.Write(p) |
||||
} |
||||
|
||||
return len(p), nil |
||||
} |
||||
@ -1,30 +0,0 @@
@@ -1,30 +0,0 @@
|
||||
// +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) |
||||
} |
||||
@ -1,21 +0,0 @@
@@ -1,21 +0,0 @@
|
||||
// +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 |
||||
} |
||||
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
// +build !windows
|
||||
|
||||
package syslog |
||||
|
||||
import ( |
||||
"io" |
||||
native "log/syslog" |
||||
) |
||||
|
||||
type syslog struct { |
||||
inner *native.Writer |
||||
} |
||||
|
||||
func New(prefix string) (io.WriteCloser, error) { |
||||
inner, err := native.New(native.LOG_INFO|native.LOG_DAEMON, prefix) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &syslog{ |
||||
inner: inner, |
||||
}, nil |
||||
} |
||||
|
||||
func (ls *syslog) Close() error { |
||||
return ls.inner.Close() |
||||
} |
||||
|
||||
func (ls *syslog) Write(p []byte) (int, error) { |
||||
return ls.inner.Write(p) |
||||
} |
||||
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
// +build windows
|
||||
|
||||
package syslog |
||||
|
||||
import ( |
||||
"fmt" |
||||
) |
||||
|
||||
type syslog struct { |
||||
} |
||||
|
||||
func New(prefix string) (io.WriteCloser, error) { |
||||
return nil, fmt.Errorf("not implemented on windows") |
||||
} |
||||
|
||||
func (ls *syslog) Close() error { |
||||
return nil |
||||
} |
||||
|
||||
func (ls *syslog) Write(p []byte) (int, error) { |
||||
return 0, nil |
||||
} |
||||
Loading…
Reference in new issue