-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsyslog.go
109 lines (86 loc) · 2.05 KB
/
xsyslog.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//go:build darwin
package xsyslog
import (
"fmt"
"log"
"log/syslog"
"os"
"strings"
"time"
"github.com/ebitengine/purego"
)
type Writer struct {
priority syslog.Priority
tag string
lib uintptr
syslogFunc func(priority syslog.Priority, message string, a ...any)
}
func New(priority syslog.Priority, tag string) (*Writer, error) {
writer := &Writer{
priority: priority,
tag: tag,
}
lib, err := purego.Dlopen("/usr/lib/libSystem.B.dylib", purego.RTLD_LAZY|purego.RTLD_GLOBAL)
if err != nil {
return nil, err
}
writer.lib = lib
purego.RegisterLibFunc(&writer.syslogFunc, writer.lib, "syslog")
return writer, nil
}
func (w *Writer) Write(b []byte) (int, error) {
return w.write(w.priority, string(b))
}
func (w *Writer) Close() error {
return purego.Dlclose(w.lib)
}
func (w *Writer) Emerg(m string) error {
_, err := w.write(syslog.LOG_EMERG, m)
return err
}
func (w *Writer) Alert(m string) error {
_, err := w.write(syslog.LOG_ALERT, m)
return err
}
func (w *Writer) Crit(m string) error {
_, err := w.write(syslog.LOG_CRIT, m)
return err
}
func (w *Writer) Err(m string) error {
_, err := w.write(syslog.LOG_ERR, m)
return err
}
func (w *Writer) Warning(m string) error {
_, err := w.write(syslog.LOG_WARNING, m)
return err
}
func (w *Writer) Notice(m string) error {
_, err := w.write(syslog.LOG_NOTICE, m)
return err
}
func (w *Writer) Info(m string) error {
_, err := w.write(syslog.LOG_INFO, m)
return err
}
func (w *Writer) Debug(m string) error {
_, err := w.write(syslog.LOG_DEBUG, m)
return err
}
func (w *Writer) write(priority syslog.Priority, msg string) (int, error) {
// ensure it ends in a \n
nl := ""
if !strings.HasSuffix(msg, "\n") {
nl = "\n"
}
timestamp := time.Now().Format(time.Stamp)
w.syslogFunc(priority, fmt.Sprintf("<%d>%s %s[%d]: %s%s",
priority, timestamp, w.tag, os.Getpid(), msg, nl))
return len(msg), nil
}
func NewLogger(p syslog.Priority, logFlag int) (*log.Logger, error) {
s, err := New(p, "")
if err != nil {
return nil, err
}
return log.New(s, "", logFlag), nil
}