diff options
Diffstat (limited to 'ringlogger/global.go')
-rw-r--r-- | ringlogger/global.go | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/ringlogger/global.go b/ringlogger/global.go index 60e750a5..5c522a70 100644 --- a/ringlogger/global.go +++ b/ringlogger/global.go @@ -1,32 +1,63 @@ /* SPDX-License-Identifier: MIT * - * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved. */ package ringlogger import ( "log" - "path/filepath" - - "golang.zx2c4.com/wireguard/windows/conf" + "unsafe" ) var Global *Ringlogger -func InitGlobalLogger(tag string) error { +func InitGlobalLogger(file, tag string) error { if Global != nil { return nil } - root, err := conf.RootDirectory() - if err != nil { - return err - } - Global, err = NewRinglogger(filepath.Join(root, "log.bin"), tag) + var err error + Global, err = NewRinglogger(file, tag) if err != nil { return err } log.SetOutput(Global) log.SetFlags(0) + overrideWrite = globalWrite return nil } + +//go:linkname overrideWrite runtime.overrideWrite +var overrideWrite func(fd uintptr, p unsafe.Pointer, n int32) int32 + +var ( + globalBuffer [maxLogLineLength - 1 - maxTagLength - 3]byte + globalBufferLocation int +) + +//go:nosplit +func globalWrite(fd uintptr, p unsafe.Pointer, n int32) int32 { + b := (*[1 << 30]byte)(p)[:n] + for len(b) > 0 { + amountAvailable := len(globalBuffer) - globalBufferLocation + amountToCopy := len(b) + if amountToCopy > amountAvailable { + amountToCopy = amountAvailable + } + copy(globalBuffer[globalBufferLocation:], b[:amountToCopy]) + b = b[amountToCopy:] + globalBufferLocation += amountToCopy + foundNl := false + for i := globalBufferLocation - amountToCopy; i < globalBufferLocation; i++ { + if globalBuffer[i] == '\n' { + foundNl = true + break + } + } + if foundNl || len(b) > 0 { + Global.Write(globalBuffer[:globalBufferLocation]) + globalBufferLocation = 0 + } + } + return n +} |