|
| 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 logging |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "sync" |
| 21 | + |
| 22 | + "github.com/aws/smithy-go/logging" |
| 23 | + |
| 24 | + "github.com/livekit/protocol/logger" |
| 25 | +) |
| 26 | + |
| 27 | +// DowngradeLogger converts errors to warnings |
| 28 | +type DowngradeLogger struct { |
| 29 | + logger.Logger |
| 30 | +} |
| 31 | + |
| 32 | +func NewDowngradeLogger() *DowngradeLogger { |
| 33 | + return &DowngradeLogger{ |
| 34 | + Logger: logger.GetLogger(), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (l *DowngradeLogger) Errorw(msg string, err error, keysAndValues ...interface{}) { |
| 39 | + l.Logger.Warnw(msg, err, keysAndValues...) |
| 40 | +} |
| 41 | + |
| 42 | +// DebugLogger logs command outputs |
| 43 | +type DebugLogger struct { |
| 44 | + cmd string |
| 45 | +} |
| 46 | + |
| 47 | +func NewDebugLogger(cmd string) *DebugLogger { |
| 48 | + return &DebugLogger{ |
| 49 | + cmd: cmd, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (l *DebugLogger) Write(p []byte) (int, error) { |
| 54 | + logger.Infow(fmt.Sprintf("%s: %s", l.cmd, string(p))) |
| 55 | + return len(p), nil |
| 56 | +} |
| 57 | + |
| 58 | +// ProcessLogger catches stray outputs from handlers |
| 59 | +type ProcessLogger struct { |
| 60 | + logger logger.Logger |
| 61 | +} |
| 62 | + |
| 63 | +func NewProcessLogger(handlerID, egressID string) *ProcessLogger { |
| 64 | + return &ProcessLogger{ |
| 65 | + logger: logger.GetLogger().WithValues("handlerID", handlerID, "egressID", egressID), |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (l *ProcessLogger) Write(p []byte) (n int, err error) { |
| 70 | + s := string(p) |
| 71 | + if strings.HasPrefix(s, "00:00:") { |
| 72 | + // ignore cuda and template not mapped gstreamer warnings |
| 73 | + } else if strings.HasPrefix(s, "turnc") { |
| 74 | + // warn on turnc error |
| 75 | + l.logger.Warnw(s, nil) |
| 76 | + } else { |
| 77 | + // panics and unexpected errors |
| 78 | + l.logger.Errorw(s, nil) |
| 79 | + } |
| 80 | + return len(p), nil |
| 81 | +} |
| 82 | + |
| 83 | +// S3Logger only logs aws messages on upload failure |
| 84 | +type S3Logger struct { |
| 85 | + mu sync.Mutex |
| 86 | + msgs []string |
| 87 | + idx int |
| 88 | +} |
| 89 | + |
| 90 | +func NewS3Logger() *S3Logger { |
| 91 | + return &S3Logger{ |
| 92 | + msgs: make([]string, 10), |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (l *S3Logger) Logf(classification logging.Classification, format string, v ...interface{}) { |
| 97 | + format = "aws %s: " + format |
| 98 | + v = append([]interface{}{strings.ToLower(string(classification))}, v...) |
| 99 | + |
| 100 | + l.mu.Lock() |
| 101 | + l.msgs[l.idx%len(l.msgs)] = fmt.Sprintf(format, v...) |
| 102 | + l.idx++ |
| 103 | + l.mu.Unlock() |
| 104 | +} |
| 105 | + |
| 106 | +func (l *S3Logger) WriteLogs() { |
| 107 | + l.mu.Lock() |
| 108 | + size := len(l.msgs) |
| 109 | + for range size { |
| 110 | + if msg := l.msgs[l.idx%size]; msg != "" { |
| 111 | + logger.Debugw(msg) |
| 112 | + } |
| 113 | + l.idx++ |
| 114 | + } |
| 115 | + l.mu.Unlock() |
| 116 | +} |
0 commit comments