Log config when debug-level

This commit is contained in:
Torjus Håkestad 2021-09-19 05:49:23 +02:00
parent bc02653473
commit 66aea2f30b
4 changed files with 31 additions and 19 deletions

View File

@ -66,24 +66,25 @@ func ActionServe(c *cli.Context) error {
} }
// Setup proper logger // Setup proper logger
logger = setupServerLogger(cfg.LogLevel) logger = setupServerLogger(cfg.LogLevel)
cfg.DebugLog(logger)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
// Setup RTMP-server // Setup RTMP-server
s := server.NewRTMPServer(ctx, cfg.RTMPListenAddr) rtmpServer := server.NewRTMPServer(ctx, cfg.RTMPListenAddr)
s.Logger = logger rtmpServer.Logger = logger
s.Hostname = cfg.Hostname rtmpServer.Hostname = cfg.Hostname
// Setup web-server // Setup web-server
webDone := make(chan struct{}) webDone := make(chan struct{})
if cfg.HTTPServerEnable { if cfg.HTTPServerEnable {
ws := server.NewWebServer(ctx, s) ws := server.NewWebServer(ctx, rtmpServer)
ws.ListenAddr = cfg.HTTPListenAddr ws.ListenAddr = cfg.HTTPListenAddr
ws.Logger = logger ws.Logger = logger
go func() { go func() {
s.Logger.Infow("Starting HTTP server.", "listen_addr", ws.ListenAddr) rtmpServer.Logger.Infow("Starting HTTP server.", "listen_addr", ws.ListenAddr)
err := ws.Serve() err := ws.Serve()
if err != nil && err != http.ErrServerClosed { if err != nil && err != http.ErrServerClosed {
s.Logger.Infow("HTTP server shut down with error.", "err", err) rtmpServer.Logger.Infow("HTTP server shut down with error.", "err", err)
} }
close(webDone) close(webDone)
}() }()
@ -95,19 +96,19 @@ func ActionServe(c *cli.Context) error {
signal.Notify(sigChan, os.Interrupt) signal.Notify(sigChan, os.Interrupt)
go func() { go func() {
<-sigChan <-sigChan
s.Logger.Debug("Got shutdown signal.") rtmpServer.Logger.Debug("Got shutdown signal.")
cancel() cancel()
}() }()
// Start RTMP-server // Start RTMP-server
s.Logger.Infow("Starting RTMP server.", "listen_addr", s.ListenAddr) rtmpServer.Logger.Infow("Starting RTMP server.", "listen_addr", s.ListenAddr)
if err := s.Listen(); err != nil { if err := rtmpServer.Listen(); err != nil {
return cli.Exit(err, ExitGenericError) return cli.Exit(err, ExitGenericError)
} }
// Wait for webserver to exit, if started // Wait for webserver to exit, if started
<-webDone <-webDone
s.Logger.Info("Server shut down.") rtmpServer.Logger.Info("Server shut down.")
return nil return nil
} }

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
"go.uber.org/zap"
) )
var ErrNotFound = errors.New("no config file found") var ErrNotFound = errors.New("no config file found")
@ -131,3 +132,12 @@ func FromDefaultLocations() (*Config, error) {
} }
return nil, ErrNotFound return nil, ErrNotFound
} }
func (c *Config) DebugLog(logger *zap.SugaredLogger) {
logger.Debugw("Config",
"hostname", c.Hostname,
"rtmp_addr", c.RTMPListenAddr,
"http_enable", c.HTTPListenAddr,
"http_accesslog", c.HTTPAccessLogEnable,
"log_level", c.LogLevel)
}

View File

@ -122,7 +122,8 @@ func TestConfig(t *testing.T) {
sr := strings.NewReader(emptyCfg) sr := strings.NewReader(emptyCfg)
c, err := config.FromReader(sr) c, err := config.FromReader(sr)
if err != nil { if err == nil {
// TODO: Check that it actually returns the correct error
t.Fatalf("Error parsing config string: %s", err) t.Fatalf("Error parsing config string: %s", err)
} }
if err := c.UpdateFromEnv(); err == nil { if err := c.UpdateFromEnv(); err == nil {

View File

@ -369,25 +369,25 @@ type StreamInfo struct {
Path string Path string
} }
func (s *RTMPServer) List() []*StreamInfo { func (rs *RTMPServer) List() []*StreamInfo {
var results []*StreamInfo var results []*StreamInfo
_, port, _ := net.SplitHostPort(s.ListenAddr) _, port, _ := net.SplitHostPort(rs.ListenAddr)
for _, stream := range s.streams { for _, stream := range rs.streams {
results = append(results, &StreamInfo{Name: stream.Name, Path: fmt.Sprintf("rtmp://%s:%s/view/%s", s.Hostname, port, stream.Name)}) results = append(results, &StreamInfo{Name: stream.Name, Path: fmt.Sprintf("rtmp://%s:%s/view/%s", rs.Hostname, port, stream.Name)})
} }
return results return results
} }
func (s *RTMPServer) GetInfo(name string) (*StreamInfo, error) { func (rs *RTMPServer) GetInfo(name string) (*StreamInfo, error) {
stream, ok := s.streams[name] stream, ok := rs.streams[name]
if !ok { if !ok {
return nil, ErrNoSuchItem return nil, ErrNoSuchItem
} }
_, port, _ := net.SplitHostPort(s.ListenAddr) _, port, _ := net.SplitHostPort(rs.ListenAddr)
return &StreamInfo{ return &StreamInfo{
Name: stream.Name, Name: stream.Name,
Path: fmt.Sprintf("rtmp://%s:%s/view/%s", s.Hostname, port, stream.Name), Path: fmt.Sprintf("rtmp://%s:%s/view/%s", rs.Hostname, port, stream.Name),
}, nil }, nil
} }