File: msg.go

package info (click to toggle)
golang-github-anacrolix-log 0.13.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (153 lines) | stat: -rw-r--r-- 2,754 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package log

import (
	"fmt"
)

type Msg struct {
	MsgImpl
}

func (me Msg) String() string {
	return me.Text()
}

func newMsg(text func() string) Msg {
	return Msg{rootMsgImpl{text}}
}

func Fmsg(format string, a ...interface{}) Msg {
	return newMsg(func() string { return fmt.Sprintf(format, a...) })
}

var Fstr = Fmsg

func Str(s string) (m Msg) {
	return newMsg(func() string { return s })
}

type msgSkipCaller struct {
	MsgImpl
	skip int
}

func (me msgSkipCaller) Callers(skip int, pc []uintptr) int {
	return me.MsgImpl.Callers(skip+1+me.skip, pc)
}

func (m Msg) Skip(skip int) Msg {
	return Msg{msgSkipCaller{m.MsgImpl, skip}}
}

type item struct {
	key, value interface{}
}

// rename sink
func (m Msg) Log(l Logger) Msg {
	l.Log(m.Skip(1))
	return m
}

func (m Msg) LogLevel(level Level, l Logger) Msg {
	l.LogLevel(level, m.Skip(1))
	return m
}

type msgWithValues struct {
	MsgImpl
	values []interface{}
}

func (me msgWithValues) Values(cb valueIterCallback) {
	for _, v := range me.values {
		if !cb(v) {
			return
		}
	}
	me.MsgImpl.Values(cb)
}

// TODO: What ordering should be applied to the values here, per MsgImpl.Values. For now they're
// traversed in order of the slice.
func (m Msg) WithValues(v ...interface{}) Msg {
	return Msg{msgWithValues{m.MsgImpl, v}}
}

func (m Msg) AddValues(v ...interface{}) Msg {
	return m.WithValues(v...)
}

func (m Msg) With(key, value interface{}) Msg {
	return m.WithValues(item{key, value})
}

func (m Msg) Add(key, value interface{}) Msg {
	return m.With(key, value)
}

//func (m Msg) SetLevel(level Level) Msg {
//	return m.With(levelKey, level)
//}

//func (m Msg) GetByKey(key interface{}) (value interface{}, ok bool) {
//	m.Values(func(i interface{}) bool {
//		if keyValue, isKeyValue := i.(item); isKeyValue && keyValue.key == key {
//			value = keyValue.value
//			ok = true
//		}
//		return !ok
//	})
//	return
//}

//func (m Msg) GetLevel() (l Level, ok bool) {
//	v, ok := m.GetByKey(levelKey)
//	if ok {
//		l = v.(Level)
//	}
//	return
//}

func (m Msg) HasValue(v interface{}) (has bool) {
	m.Values(func(i interface{}) bool {
		if i == v {
			has = true
		}
		return !has
	})
	return
}

func (m Msg) AddValue(v interface{}) Msg {
	return m.AddValues(v)
}

//func (m Msg) GetValueByType(p interface{}) bool {
//	pve := reflect.ValueOf(p).Elem()
//	t := pve.Type()
//	return !iter.All(func(i interface{}) bool {
//		iv := reflect.ValueOf(i)
//		if iv.Type() == t {
//			pve.Set(iv)
//			return false
//		}
//		return true
//	}, m.Values)
//}

func (m Msg) WithText(f func(Msg) string) Msg {
	return Msg{msgWithText{
		m,
		func() string { return f(m) },
	}}
}

type msgWithText struct {
	MsgImpl
	text func() string
}

func (me msgWithText) Text() string {
	return me.text()
}