add an util method to convert []byte to string

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2024-05-08 19:01:58 +02:00
parent 65753fe23e
commit 5d24d665bd
23 changed files with 70 additions and 40 deletions

View File

@@ -29,6 +29,7 @@ import (
"os"
"path/filepath"
"time"
"unsafe"
ftpserverlog "github.com/fclairamb/go-log"
"github.com/rs/zerolog"
@@ -283,7 +284,7 @@ func (l *StdLoggerWrapper) Write(p []byte) (n int, err error) {
p = p[0 : n-1]
}
Log(LevelError, l.Sender, "", string(p))
Log(LevelError, l.Sender, "", bytesToString(p))
return
}
@@ -363,3 +364,12 @@ func (l *LeveledLogger) With(keysAndValues ...any) ftpserverlog.Logger {
additionalKeyVals: append(l.additionalKeyVals, keysAndValues...),
}
}
func bytesToString(b []byte) string {
// unsafe.SliceData relies on cap whereas we want to rely on len
if len(b) == 0 {
return ""
}
// https://github.com/golang/go/blob/4ed358b57efdad9ed710be7f4fc51495a7620ce2/src/strings/builder.go#L41
return unsafe.String(unsafe.SliceData(b), len(b))
}