Skip to content

Commit d3e6ded

Browse files
authored
Merge pull request #3 from zeripath/optimize-logger
A few more improvements
2 parents bce0875 + bf08538 commit d3e6ded

20 files changed

+691
-555
lines changed

modules/log/colors_router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ var (
7373
wayTooLong = ColorBytes(BgMagenta)
7474
)
7575

76-
// ColoredTime adds colors for time on log, it always formats the duration in Millisecond unit
76+
// ColoredTime converts the provided time to a ColoredValue for logging. The duration is always formatted in milliseconds.
7777
func ColoredTime(duration time.Duration) *ColoredValue {
7878
// the output of in Millisecond is more readable:
7979
// * before: "100.1ms" "100.1μs" "100.1s"

modules/log/level.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ func (l Level) String() string {
7474
return "info"
7575
}
7676

77-
// IsEnabledOn checks whether the log level is enabled on targetLevel.
78-
// eg: INFO logs are enabled (outputted) on DEBUG / INFO, but not WARN / ERROR
79-
func (l Level) IsEnabledOn(targetLevel Level) bool {
80-
return l >= targetLevel && l != NONE
81-
}
82-
8377
// Color returns the color string for this Level
8478
func (l Level) Color() *[]byte {
8579
color, ok := levelToColor[l]

modules/log/level_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,3 @@ func TestLevelMarshalUnmarshalJSON(t *testing.T) {
5454
func makeTestLevelBytes(level string) []byte {
5555
return []byte(fmt.Sprintf(`{"level":"%s"}`, level))
5656
}
57-
58-
func TestLevelIsEnabled(t *testing.T) {
59-
assert.True(t, INFO.IsEnabledOn(DEBUG))
60-
assert.True(t, INFO.IsEnabledOn(INFO))
61-
assert.False(t, INFO.IsEnabledOn(ERROR))
62-
63-
assert.False(t, ERROR.IsEnabledOn(NONE))
64-
assert.False(t, NONE.IsEnabledOn(ERROR))
65-
assert.False(t, NONE.IsEnabledOn(NONE))
66-
}

modules/log/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,6 @@ func init() {
290290
prefix = strings.TrimSuffix(filename, "modules/log/log.go")
291291
if prefix == filename {
292292
// in case the source code file is moved, we can not trim the suffix, the code above should also be updated.
293-
panic("can not detect correct package prefix, please update file: " + filename)
293+
panic("unable to detect correct package prefix, please update file: " + filename)
294294
}
295295
}

modules/web/route.go

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -13,141 +13,11 @@ import (
1313

1414
"code.gitea.io/gitea/modules/context"
1515
"code.gitea.io/gitea/modules/web/middleware"
16-
"code.gitea.io/gitea/routers/common"
1716

1817
"gitea.com/go-chi/binding"
1918
chi "github.com/go-chi/chi/v5"
2019
)
2120

22-
// Wrap converts all kinds of routes to standard library one
23-
func Wrap(handlers ...interface{}) http.HandlerFunc {
24-
if len(handlers) == 0 {
25-
panic("No handlers found")
26-
}
27-
28-
for _, handler := range handlers {
29-
switch t := handler.(type) {
30-
case http.HandlerFunc, func(http.ResponseWriter, *http.Request),
31-
func(ctx *context.Context),
32-
func(ctx *context.Context) goctx.CancelFunc,
33-
func(*context.APIContext),
34-
func(*context.PrivateContext),
35-
func(*context.PrivateContext) goctx.CancelFunc,
36-
func(http.Handler) http.Handler:
37-
default:
38-
panic(fmt.Sprintf("Unsupported handler type: %#v", t))
39-
}
40-
}
41-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
42-
for i := 0; i < len(handlers); i++ {
43-
handler := handlers[i]
44-
common.UpdateContextHandlerFuncInfo(req.Context(), handler)
45-
switch t := handler.(type) {
46-
case http.HandlerFunc:
47-
t(resp, req)
48-
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
49-
return
50-
}
51-
case func(http.ResponseWriter, *http.Request):
52-
t(resp, req)
53-
if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 {
54-
return
55-
}
56-
case func(ctx *context.Context) goctx.CancelFunc:
57-
ctx := context.GetContext(req)
58-
cancel := t(ctx)
59-
if cancel != nil {
60-
defer cancel()
61-
}
62-
if ctx.Written() {
63-
return
64-
}
65-
case func(*context.PrivateContext) goctx.CancelFunc:
66-
ctx := context.GetPrivateContext(req)
67-
cancel := t(ctx)
68-
if cancel != nil {
69-
defer cancel()
70-
}
71-
if ctx.Written() {
72-
return
73-
}
74-
case func(ctx *context.Context):
75-
ctx := context.GetContext(req)
76-
t(ctx)
77-
if ctx.Written() {
78-
return
79-
}
80-
case func(*context.APIContext):
81-
ctx := context.GetAPIContext(req)
82-
t(ctx)
83-
if ctx.Written() {
84-
return
85-
}
86-
case func(*context.PrivateContext):
87-
ctx := context.GetPrivateContext(req)
88-
t(ctx)
89-
if ctx.Written() {
90-
return
91-
}
92-
case func(http.Handler) http.Handler:
93-
var next = http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})
94-
if len(handlers) > i+1 {
95-
next = Wrap(handlers[i+1:]...)
96-
}
97-
t(next).ServeHTTP(resp, req)
98-
return
99-
default:
100-
panic(fmt.Sprintf("Unsupported handler type: %#v", t))
101-
}
102-
}
103-
})
104-
}
105-
106-
// Middle wrap a context function as a chi middleware
107-
func Middle(f func(ctx *context.Context)) func(netx http.Handler) http.Handler {
108-
return func(next http.Handler) http.Handler {
109-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
110-
ctx := context.GetContext(req)
111-
f(ctx)
112-
if ctx.Written() {
113-
return
114-
}
115-
next.ServeHTTP(ctx.Resp, ctx.Req)
116-
})
117-
}
118-
}
119-
120-
// MiddleCancel wrap a context function as a chi middleware
121-
func MiddleCancel(f func(ctx *context.Context) goctx.CancelFunc) func(netx http.Handler) http.Handler {
122-
return func(next http.Handler) http.Handler {
123-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
124-
ctx := context.GetContext(req)
125-
cancel := f(ctx)
126-
if cancel != nil {
127-
defer cancel()
128-
}
129-
if ctx.Written() {
130-
return
131-
}
132-
next.ServeHTTP(ctx.Resp, ctx.Req)
133-
})
134-
}
135-
}
136-
137-
// MiddleAPI wrap a context function as a chi middleware
138-
func MiddleAPI(f func(ctx *context.APIContext)) func(netx http.Handler) http.Handler {
139-
return func(next http.Handler) http.Handler {
140-
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
141-
ctx := context.GetAPIContext(req)
142-
f(ctx)
143-
if ctx.Written() {
144-
return
145-
}
146-
next.ServeHTTP(ctx.Resp, ctx.Req)
147-
})
148-
}
149-
}
150-
15121
// Bind binding an obj to a handler
15222
func Bind(obj interface{}) http.HandlerFunc {
15323
var tp = reflect.TypeOf(obj)

modules/web/routing/context.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package routing
6+
7+
import (
8+
"context"
9+
"net/http"
10+
)
11+
12+
type contextKeyStruct struct{}
13+
14+
var contextKey contextKeyStruct
15+
16+
//UpdateFuncInfo updates a context's func info
17+
func UpdateFuncInfo(ctx context.Context, funcInfo *FuncInfo) {
18+
record, ok := ctx.Value(contextKey).(*requestRecord)
19+
if !ok {
20+
return
21+
}
22+
23+
// update our current record
24+
record.lock.Lock()
25+
record.funcInfo = funcInfo
26+
record.lock.Unlock()
27+
28+
}
29+
30+
// MarkLongPolling marks the reuqest is a long-polling request, and the logger may output different message for it
31+
func MarkLongPolling(resp http.ResponseWriter, req *http.Request) {
32+
record, ok := req.Context().Value(contextKey).(*requestRecord)
33+
if !ok {
34+
return
35+
}
36+
37+
record.lock.Lock()
38+
record.isLongPolling = true
39+
record.lock.Unlock()
40+
}
41+
42+
//UpdatePanicError updates a context's error info, a panic may be recovered by other middlewares, but we still need to know that.
43+
func UpdatePanicError(ctx context.Context, err interface{}) {
44+
record, ok := ctx.Value(contextKey).(*requestRecord)
45+
if !ok {
46+
return
47+
}
48+
49+
record.lock.Lock()
50+
record.panicError = err
51+
record.lock.Unlock()
52+
}

0 commit comments

Comments
 (0)