|
| 1 | +// Copyright 2025 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package medialogutils |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/livekit/protocol/logger" |
| 22 | +) |
| 23 | + |
| 24 | +// CmdLogger logs cmd outputs |
| 25 | +type CmdLogger struct { |
| 26 | + name string |
| 27 | +} |
| 28 | + |
| 29 | +func NewCmdLogger(name string) *CmdLogger { |
| 30 | + return &CmdLogger{ |
| 31 | + name: name, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (l *CmdLogger) Write(p []byte) (int, error) { |
| 36 | + logger.Infow(fmt.Sprintf("%s: %s", l.name, string(p))) |
| 37 | + return len(p), nil |
| 38 | +} |
| 39 | + |
| 40 | +// HandlerLogger catches stray outputs from egress handlers |
| 41 | +type HandlerLogger struct { |
| 42 | + logger logger.Logger |
| 43 | +} |
| 44 | + |
| 45 | +func NewHandlerLogger(keyAndValues ...any) *HandlerLogger { |
| 46 | + return &HandlerLogger{ |
| 47 | + logger: logger.GetLogger().WithValues(keyAndValues...), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (l *HandlerLogger) Write(p []byte) (n int, err error) { |
| 52 | + s := string(p) |
| 53 | + if strings.HasSuffix(s, "}\n") { |
| 54 | + // normal handler logs |
| 55 | + fmt.Print(s) |
| 56 | + } else if strings.HasPrefix(s, "0:00:") { |
| 57 | + // ignore cuda and template not mapped gstreamer warnings |
| 58 | + } else if strings.HasPrefix(s, "turnc") { |
| 59 | + // warn on turnc error |
| 60 | + l.logger.Infow(s, nil) |
| 61 | + } else { |
| 62 | + // panics and unexpected errors |
| 63 | + l.logger.Errorw(s, nil) |
| 64 | + } |
| 65 | + |
| 66 | + return len(p), nil |
| 67 | +} |
0 commit comments