-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctx_handle.go
38 lines (30 loc) · 959 Bytes
/
ctx_handle.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
package olog
import (
"context"
"sync/atomic"
"unsafe"
)
// Pointer to the default context handle
var dch unsafe.Pointer
// Initializes the default context handle with an empty handle
func init() {
SetDefCtxHandle(emptyHandle)
}
// CtxHandle type that returns a slice of Fields based on the provided context
type CtxHandle func(ctx context.Context) []Field
// ctxHandler hold a context handle function
type ctxHandler struct {
handle CtxHandle
}
// SetDefCtxHandle Sets the default context handle to the given handle function
func SetDefCtxHandle(handle CtxHandle) {
atomic.StorePointer(&dch, unsafe.Pointer(&ctxHandler{handle: handle}))
}
// getDefCtxHandle returns the default context handle function
func getDefCtxHandle() CtxHandle {
return (*ctxHandler)(atomic.LoadPointer(&dch)).handle
}
// emptyHandle is default context handle function that returns an empty slice of fields
func emptyHandle(ctx context.Context) []Field {
return nil
}