Skip to content

Commit d78919d

Browse files
committed
chore: merge latest codes
2 parents 270e1b9 + 4cbfdb3 commit d78919d

File tree

16 files changed

+342
-111
lines changed

16 files changed

+342
-111
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
# ignore
1414
**/.idea
15+
**/.vscode
1516
**/.DS_Store
1617
**/logs
1718
**/adhoc

Diff for: core/logx/logs.go

+104-46
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,30 @@ func Close() error {
6868

6969
// Debug writes v into access log.
7070
func Debug(v ...any) {
71-
writeDebug(fmt.Sprint(v...))
71+
if shallLog(DebugLevel) {
72+
writeDebug(fmt.Sprint(v...))
73+
}
7274
}
7375

7476
// Debugf writes v with format into access log.
7577
func Debugf(format string, v ...any) {
76-
writeDebug(fmt.Sprintf(format, v...))
78+
if shallLog(DebugLevel) {
79+
writeDebug(fmt.Sprintf(format, v...))
80+
}
7781
}
7882

7983
// Debugv writes v into access log with json content.
8084
func Debugv(v any) {
81-
writeDebug(v)
85+
if shallLog(DebugLevel) {
86+
writeDebug(v)
87+
}
8288
}
8389

8490
// Debugw writes msg along with fields into access log.
8591
func Debugw(msg string, fields ...LogField) {
86-
writeDebug(msg, fields...)
92+
if shallLog(DebugLevel) {
93+
writeDebug(msg, fields...)
94+
}
8795
}
8896

8997
// Disable disables the logging.
@@ -99,35 +107,47 @@ func DisableStat() {
99107

100108
// Error writes v into error log.
101109
func Error(v ...any) {
102-
writeError(fmt.Sprint(v...))
110+
if shallLog(ErrorLevel) {
111+
writeError(fmt.Sprint(v...))
112+
}
103113
}
104114

105115
// Errorf writes v with format into error log.
106116
func Errorf(format string, v ...any) {
107-
writeError(fmt.Errorf(format, v...).Error())
117+
if shallLog(ErrorLevel) {
118+
writeError(fmt.Errorf(format, v...).Error())
119+
}
108120
}
109121

110122
// ErrorStack writes v along with call stack into error log.
111123
func ErrorStack(v ...any) {
112-
// there is newline in stack string
113-
writeStack(fmt.Sprint(v...))
124+
if shallLog(ErrorLevel) {
125+
// there is newline in stack string
126+
writeStack(fmt.Sprint(v...))
127+
}
114128
}
115129

116130
// ErrorStackf writes v along with call stack in format into error log.
117131
func ErrorStackf(format string, v ...any) {
118-
// there is newline in stack string
119-
writeStack(fmt.Sprintf(format, v...))
132+
if shallLog(ErrorLevel) {
133+
// there is newline in stack string
134+
writeStack(fmt.Sprintf(format, v...))
135+
}
120136
}
121137

122138
// Errorv writes v into error log with json content.
123139
// No call stack attached, because not elegant to pack the messages.
124140
func Errorv(v any) {
125-
writeError(v)
141+
if shallLog(ErrorLevel) {
142+
writeError(v)
143+
}
126144
}
127145

128146
// Errorw writes msg along with fields into error log.
129147
func Errorw(msg string, fields ...LogField) {
130-
writeError(msg, fields...)
148+
if shallLog(ErrorLevel) {
149+
writeError(msg, fields...)
150+
}
131151
}
132152

133153
// Field returns a LogField for the given key and value.
@@ -170,22 +190,30 @@ func Field(key string, value any) LogField {
170190

171191
// Info writes v into access log.
172192
func Info(v ...any) {
173-
writeInfo(fmt.Sprint(v...))
193+
if shallLog(InfoLevel) {
194+
writeInfo(fmt.Sprint(v...))
195+
}
174196
}
175197

176198
// Infof writes v with format into access log.
177199
func Infof(format string, v ...any) {
178-
writeInfo(fmt.Sprintf(format, v...))
200+
if shallLog(InfoLevel) {
201+
writeInfo(fmt.Sprintf(format, v...))
202+
}
179203
}
180204

181205
// Infov writes v into access log with json content.
182206
func Infov(v any) {
183-
writeInfo(v)
207+
if shallLog(InfoLevel) {
208+
writeInfo(v)
209+
}
184210
}
185211

186212
// Infow writes msg along with fields into access log.
187213
func Infow(msg string, fields ...LogField) {
188-
writeInfo(msg, fields...)
214+
if shallLog(InfoLevel) {
215+
writeInfo(msg, fields...)
216+
}
189217
}
190218

191219
// Must checks if err is nil, otherwise logs the error and exits.
@@ -194,7 +222,7 @@ func Must(err error) {
194222
return
195223
}
196224

197-
msg := err.Error()
225+
msg := fmt.Sprintf("%+v\n\n%s", err.Error(), debug.Stack())
198226
log.Print(msg)
199227
getWriter().Severe(msg)
200228

@@ -269,42 +297,58 @@ func SetUp(c LogConf) (err error) {
269297

270298
// Severe writes v into severe log.
271299
func Severe(v ...any) {
272-
writeSevere(fmt.Sprint(v...))
300+
if shallLog(SevereLevel) {
301+
writeSevere(fmt.Sprint(v...))
302+
}
273303
}
274304

275305
// Severef writes v with format into severe log.
276306
func Severef(format string, v ...any) {
277-
writeSevere(fmt.Sprintf(format, v...))
307+
if shallLog(SevereLevel) {
308+
writeSevere(fmt.Sprintf(format, v...))
309+
}
278310
}
279311

280312
// Slow writes v into slow log.
281313
func Slow(v ...any) {
282-
writeSlow(fmt.Sprint(v...))
314+
if shallLog(ErrorLevel) {
315+
writeSlow(fmt.Sprint(v...))
316+
}
283317
}
284318

285319
// Slowf writes v with format into slow log.
286320
func Slowf(format string, v ...any) {
287-
writeSlow(fmt.Sprintf(format, v...))
321+
if shallLog(ErrorLevel) {
322+
writeSlow(fmt.Sprintf(format, v...))
323+
}
288324
}
289325

290326
// Slowv writes v into slow log with json content.
291327
func Slowv(v any) {
292-
writeSlow(v)
328+
if shallLog(ErrorLevel) {
329+
writeSlow(v)
330+
}
293331
}
294332

295333
// Sloww writes msg along with fields into slow log.
296334
func Sloww(msg string, fields ...LogField) {
297-
writeSlow(msg, fields...)
335+
if shallLog(ErrorLevel) {
336+
writeSlow(msg, fields...)
337+
}
298338
}
299339

300340
// Stat writes v into stat log.
301341
func Stat(v ...any) {
302-
writeStat(fmt.Sprint(v...))
342+
if shallLogStat() && shallLog(InfoLevel) {
343+
writeStat(fmt.Sprint(v...))
344+
}
303345
}
304346

305347
// Statf writes v with format into stat log.
306348
func Statf(format string, v ...any) {
307-
writeStat(fmt.Sprintf(format, v...))
349+
if shallLogStat() && shallLog(InfoLevel) {
350+
writeStat(fmt.Sprintf(format, v...))
351+
}
308352
}
309353

310354
// WithCooldownMillis customizes logging on writing call stack interval.
@@ -429,44 +473,58 @@ func shallLogStat() bool {
429473
return atomic.LoadUint32(&disableStat) == 0
430474
}
431475

476+
// writeDebug writes v into debug log.
477+
// Not checking shallLog here is for performance consideration.
478+
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
479+
// The caller should check shallLog before calling this function.
432480
func writeDebug(val any, fields ...LogField) {
433-
if shallLog(DebugLevel) {
434-
getWriter().Debug(val, addCaller(fields...)...)
435-
}
481+
getWriter().Debug(val, addCaller(fields...)...)
436482
}
437483

484+
// writeError writes v into error log.
485+
// Not checking shallLog here is for performance consideration.
486+
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
487+
// The caller should check shallLog before calling this function.
438488
func writeError(val any, fields ...LogField) {
439-
if shallLog(ErrorLevel) {
440-
getWriter().Error(val, addCaller(fields...)...)
441-
}
489+
getWriter().Error(val, addCaller(fields...)...)
442490
}
443491

492+
// writeInfo writes v into info log.
493+
// Not checking shallLog here is for performance consideration.
494+
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
495+
// The caller should check shallLog before calling this function.
444496
func writeInfo(val any, fields ...LogField) {
445-
if shallLog(InfoLevel) {
446-
getWriter().Info(val, addCaller(fields...)...)
447-
}
497+
getWriter().Info(val, addCaller(fields...)...)
448498
}
449499

500+
// writeSevere writes v into severe log.
501+
// Not checking shallLog here is for performance consideration.
502+
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
503+
// The caller should check shallLog before calling this function.
450504
func writeSevere(msg string) {
451-
if shallLog(SevereLevel) {
452-
getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
453-
}
505+
getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
454506
}
455507

508+
// writeSlow writes v into slow log.
509+
// Not checking shallLog here is for performance consideration.
510+
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
511+
// The caller should check shallLog before calling this function.
456512
func writeSlow(val any, fields ...LogField) {
457-
if shallLog(ErrorLevel) {
458-
getWriter().Slow(val, addCaller(fields...)...)
459-
}
513+
getWriter().Slow(val, addCaller(fields...)...)
460514
}
461515

516+
// writeStack writes v into stack log.
517+
// Not checking shallLog here is for performance consideration.
518+
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
519+
// The caller should check shallLog before calling this function.
462520
func writeStack(msg string) {
463-
if shallLog(ErrorLevel) {
464-
getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
465-
}
521+
getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
466522
}
467523

524+
// writeStat writes v into stat log.
525+
// Not checking shallLog here is for performance consideration.
526+
// If we check shallLog here, the fmt.Sprint might be called even if the log level is not enabled.
527+
// The caller should check shallLog before calling this function.
468528
func writeStat(msg string) {
469-
if shallLogStat() && shallLog(InfoLevel) {
470-
getWriter().Stat(msg, addCaller()...)
471-
}
529+
getWriter().Stat(msg, addCaller()...)
472530
}

Diff for: core/stat/internal/cgroup_linux.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ func parseUints(val string) ([]uint64, error) {
219219
return nil, nil
220220
}
221221

222+
var sets []uint64
222223
ints := make(map[uint64]lang.PlaceholderType)
223224
cols := strings.Split(val, ",")
224225
for _, r := range cols {
@@ -239,27 +240,28 @@ func parseUints(val string) ([]uint64, error) {
239240
}
240241

241242
for i := min; i <= max; i++ {
242-
ints[i] = lang.Placeholder
243+
if _, ok := ints[i]; !ok {
244+
ints[i] = lang.Placeholder
245+
sets = append(sets, i)
246+
}
243247
}
244248
} else {
245249
v, err := parseUint(r)
246250
if err != nil {
247251
return nil, err
248252
}
249253

250-
ints[v] = lang.Placeholder
254+
if _, ok := ints[v]; !ok {
255+
ints[v] = lang.Placeholder
256+
sets = append(sets, v)
257+
}
251258
}
252259
}
253260

254-
var sets []uint64
255-
for k := range ints {
256-
sets = append(sets, k)
257-
}
258-
259261
return sets, nil
260262
}
261263

262-
// runningInUserNS detects whether we are currently running in an user namespace.
264+
// runningInUserNS detects whether we are currently running in a user namespace.
263265
func runningInUserNS() bool {
264266
nsOnce.Do(func() {
265267
file, err := os.Open("/proc/self/uid_map")

Diff for: core/stat/internal/cgroup_linux_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -25,3 +26,46 @@ func TestCgroupV1(t *testing.T) {
2526
assert.Error(t, err)
2627
}
2728
}
29+
30+
func TestParseUint(t *testing.T) {
31+
tests := []struct {
32+
input string
33+
want uint64
34+
err error
35+
}{
36+
{"0", 0, nil},
37+
{"123", 123, nil},
38+
{"-1", 0, nil},
39+
{"-18446744073709551616", 0, nil},
40+
{"foo", 0, fmt.Errorf("cgroup: bad int format: foo")},
41+
}
42+
43+
for _, tt := range tests {
44+
got, err := parseUint(tt.input)
45+
assert.Equal(t, tt.err, err)
46+
assert.Equal(t, tt.want, got)
47+
}
48+
}
49+
50+
func TestParseUints(t *testing.T) {
51+
tests := []struct {
52+
input string
53+
want []uint64
54+
err error
55+
}{
56+
{"", nil, nil},
57+
{"1,2,3", []uint64{1, 2, 3}, nil},
58+
{"1-3", []uint64{1, 2, 3}, nil},
59+
{"1-3,5,7-9", []uint64{1, 2, 3, 5, 7, 8, 9}, nil},
60+
{"foo", nil, fmt.Errorf("cgroup: bad int format: foo")},
61+
{"1-bar", nil, fmt.Errorf("cgroup: bad int list format: 1-bar")},
62+
{"bar-3", nil, fmt.Errorf("cgroup: bad int list format: bar-3")},
63+
{"3-1", nil, fmt.Errorf("cgroup: bad int list format: 3-1")},
64+
}
65+
66+
for _, tt := range tests {
67+
got, err := parseUints(tt.input)
68+
assert.Equal(t, tt.err, err)
69+
assert.Equal(t, tt.want, got)
70+
}
71+
}

0 commit comments

Comments
 (0)