forked from lysShub/wintun-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
68 lines (60 loc) · 1.48 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package wintun
import (
"context"
"log/slog"
"runtime"
"syscall"
"github.com/pkg/errors"
"golang.org/x/sys/windows"
)
type LoggerLevel int
const (
Info LoggerLevel = iota
Warn
Error
)
type LoggerCallback func(level LoggerLevel, timestamp uint64, msg *uint16) uintptr
func DefaultCallback(log *slog.Logger) LoggerCallback {
return func(level LoggerLevel, timestamp uint64, msg *uint16) uintptr {
var sl slog.Level
switch level {
case Info:
sl = slog.LevelInfo
case Warn:
sl = slog.LevelWarn
case Error:
sl = slog.LevelError
default:
sl = slog.LevelDebug
}
log.LogAttrs(
context.Background(), sl,
windows.UTF16PtrToString(msg),
)
return 0
}
}
func SetLogger(logger LoggerCallback) error {
var callback uintptr
if logger != nil {
switch runtime.GOARCH {
case "386":
callback = windows.NewCallback(func(level LoggerLevel, timestampLow, timestampHigh uint32, msg *uint16) {
logger(level, uint64(timestampHigh)<<32|uint64(timestampLow), msg)
})
case "arm":
callback = windows.NewCallback(func(level LoggerLevel, _, timestampLow, timestampHigh uint32, msg *uint16) {
logger(level, uint64(timestampHigh)<<32|uint64(timestampLow), msg)
})
case "amd64", "arm64":
callback = windows.NewCallback(logger)
default:
return errors.Errorf("not support windows arch %s", runtime.GOARCH)
}
}
_, _, err := syscall.SyscallN(procSetLogger.Addr(), callback)
if err != windows.ERROR_SUCCESS {
return errors.WithStack(err)
}
return nil
}