|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +// Package slogoslog provides a slog.Handler that writes to Apple's Unified Logging system (os_log). |
| 4 | +package slogoslog |
| 5 | + |
| 6 | +/* |
| 7 | +#cgo LDFLAGS: -framework Foundation |
| 8 | +#include <os/log.h> |
| 9 | +#include <stdlib.h> |
| 10 | +
|
| 11 | +static os_log_t epithet_log = NULL; |
| 12 | +
|
| 13 | +void epithet_log_init(const char* subsystem, const char* category) { |
| 14 | + epithet_log = os_log_create(subsystem, category); |
| 15 | +} |
| 16 | +
|
| 17 | +void epithet_log_debug(const char* msg) { |
| 18 | + os_log_debug(epithet_log, "%{public}s", msg); |
| 19 | +} |
| 20 | +
|
| 21 | +void epithet_log_info(const char* msg) { |
| 22 | + os_log_info(epithet_log, "%{public}s", msg); |
| 23 | +} |
| 24 | +
|
| 25 | +void epithet_log_default(const char* msg) { |
| 26 | + os_log(epithet_log, "%{public}s", msg); |
| 27 | +} |
| 28 | +
|
| 29 | +void epithet_log_error(const char* msg) { |
| 30 | + os_log_error(epithet_log, "%{public}s", msg); |
| 31 | +} |
| 32 | +*/ |
| 33 | +import "C" |
| 34 | + |
| 35 | +import ( |
| 36 | + "context" |
| 37 | + "fmt" |
| 38 | + "log/slog" |
| 39 | + "strings" |
| 40 | + "sync" |
| 41 | + "unsafe" |
| 42 | +) |
| 43 | + |
| 44 | +const ( |
| 45 | + // Subsystem is the os_log subsystem identifier for epithet. |
| 46 | + Subsystem = "dev.epithet" |
| 47 | + // Category is the default os_log category. |
| 48 | + Category = "default" |
| 49 | +) |
| 50 | + |
| 51 | +var initOnce sync.Once |
| 52 | + |
| 53 | +// Handler is a slog.Handler that writes to Apple's Unified Logging system. |
| 54 | +type Handler struct { |
| 55 | + level slog.Leveler |
| 56 | + attrs []slog.Attr |
| 57 | + groups []string |
| 58 | +} |
| 59 | + |
| 60 | +// NewHandler creates a new os_log handler with the given minimum log level. |
| 61 | +// Returns a handler that writes to Apple's Unified Logging system with |
| 62 | +// subsystem "dev.epithet". |
| 63 | +func NewHandler(level slog.Leveler) slog.Handler { |
| 64 | + initOnce.Do(func() { |
| 65 | + cs := C.CString(Subsystem) |
| 66 | + cc := C.CString(Category) |
| 67 | + defer C.free(unsafe.Pointer(cs)) |
| 68 | + defer C.free(unsafe.Pointer(cc)) |
| 69 | + C.epithet_log_init(cs, cc) |
| 70 | + }) |
| 71 | + return &Handler{level: level} |
| 72 | +} |
| 73 | + |
| 74 | +// Enabled reports whether the handler handles records at the given level. |
| 75 | +func (h *Handler) Enabled(_ context.Context, level slog.Level) bool { |
| 76 | + return level >= h.level.Level() |
| 77 | +} |
| 78 | + |
| 79 | +// Handle writes the record to os_log. |
| 80 | +func (h *Handler) Handle(_ context.Context, r slog.Record) error { |
| 81 | + msg := h.formatRecord(r) |
| 82 | + cmsg := C.CString(msg) |
| 83 | + defer C.free(unsafe.Pointer(cmsg)) |
| 84 | + |
| 85 | + switch { |
| 86 | + case r.Level >= slog.LevelError: |
| 87 | + C.epithet_log_error(cmsg) |
| 88 | + case r.Level >= slog.LevelWarn: |
| 89 | + C.epithet_log_default(cmsg) |
| 90 | + case r.Level >= slog.LevelInfo: |
| 91 | + C.epithet_log_info(cmsg) |
| 92 | + default: |
| 93 | + C.epithet_log_debug(cmsg) |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// WithAttrs returns a new handler with the given attributes added. |
| 99 | +func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler { |
| 100 | + newAttrs := make([]slog.Attr, len(h.attrs), len(h.attrs)+len(attrs)) |
| 101 | + copy(newAttrs, h.attrs) |
| 102 | + newAttrs = append(newAttrs, attrs...) |
| 103 | + return &Handler{ |
| 104 | + level: h.level, |
| 105 | + attrs: newAttrs, |
| 106 | + groups: h.groups, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// WithGroup returns a new handler with the given group name added. |
| 111 | +func (h *Handler) WithGroup(name string) slog.Handler { |
| 112 | + if name == "" { |
| 113 | + return h |
| 114 | + } |
| 115 | + newGroups := make([]string, len(h.groups), len(h.groups)+1) |
| 116 | + copy(newGroups, h.groups) |
| 117 | + newGroups = append(newGroups, name) |
| 118 | + return &Handler{ |
| 119 | + level: h.level, |
| 120 | + attrs: h.attrs, |
| 121 | + groups: newGroups, |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// formatRecord formats a log record as "message key=value key=value ...". |
| 126 | +func (h *Handler) formatRecord(r slog.Record) string { |
| 127 | + var b strings.Builder |
| 128 | + b.WriteString(r.Message) |
| 129 | + |
| 130 | + // Write pre-accumulated attrs. |
| 131 | + for _, a := range h.attrs { |
| 132 | + h.writeAttr(&b, a, h.groups) |
| 133 | + } |
| 134 | + |
| 135 | + // Write record attrs. |
| 136 | + r.Attrs(func(a slog.Attr) bool { |
| 137 | + h.writeAttr(&b, a, h.groups) |
| 138 | + return true |
| 139 | + }) |
| 140 | + |
| 141 | + return b.String() |
| 142 | +} |
| 143 | + |
| 144 | +// writeAttr writes a single attribute to the builder. |
| 145 | +func (h *Handler) writeAttr(b *strings.Builder, a slog.Attr, groups []string) { |
| 146 | + // Resolve the attribute value. |
| 147 | + a.Value = a.Value.Resolve() |
| 148 | + |
| 149 | + // Skip empty attributes. |
| 150 | + if a.Equal(slog.Attr{}) { |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + b.WriteByte(' ') |
| 155 | + |
| 156 | + // Write group prefix. |
| 157 | + for _, g := range groups { |
| 158 | + b.WriteString(g) |
| 159 | + b.WriteByte('.') |
| 160 | + } |
| 161 | + |
| 162 | + // Handle groups specially. |
| 163 | + if a.Value.Kind() == slog.KindGroup { |
| 164 | + attrs := a.Value.Group() |
| 165 | + newGroups := groups |
| 166 | + if a.Key != "" { |
| 167 | + newGroups = append(groups, a.Key) |
| 168 | + } |
| 169 | + for _, ga := range attrs { |
| 170 | + h.writeAttr(b, ga, newGroups) |
| 171 | + } |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + b.WriteString(a.Key) |
| 176 | + b.WriteByte('=') |
| 177 | + b.WriteString(formatValue(a.Value)) |
| 178 | +} |
| 179 | + |
| 180 | +// formatValue formats a slog.Value for display. |
| 181 | +func formatValue(v slog.Value) string { |
| 182 | + switch v.Kind() { |
| 183 | + case slog.KindString: |
| 184 | + s := v.String() |
| 185 | + if strings.ContainsAny(s, " \t\n\"") { |
| 186 | + return fmt.Sprintf("%q", s) |
| 187 | + } |
| 188 | + return s |
| 189 | + case slog.KindTime: |
| 190 | + return v.Time().Format("15:04:05.000") |
| 191 | + case slog.KindDuration: |
| 192 | + return v.Duration().String() |
| 193 | + default: |
| 194 | + return fmt.Sprintf("%v", v.Any()) |
| 195 | + } |
| 196 | +} |
0 commit comments