aboutsummaryrefslogtreecommitdiffstats
path: root/routers/private/manager.go
blob: f15da298d6a043a053128f4e1e39d8eb271f50f2 (plain)
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
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package private

import (
	"fmt"
	"net/http"

	"code.gitea.io/gitea/models/db"
	"code.gitea.io/gitea/modules/context"
	"code.gitea.io/gitea/modules/graceful"
	"code.gitea.io/gitea/modules/json"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/private"
	"code.gitea.io/gitea/modules/queue"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/modules/web"
)

// FlushQueues flushes all the Queues
func FlushQueues(ctx *context.PrivateContext) {
	opts := web.GetForm(ctx).(*private.FlushOptions)
	if opts.NonBlocking {
		// Save the hammer ctx here - as a new one is created each time you call this.
		baseCtx := graceful.GetManager().HammerContext()
		go func() {
			err := queue.GetManager().FlushAll(baseCtx, opts.Timeout)
			if err != nil {
				log.Error("Flushing request timed-out with error: %v", err)
			}
		}()
		ctx.JSON(http.StatusAccepted, private.Response{
			Err: "Flushing",
		})
		return
	}
	err := queue.GetManager().FlushAll(ctx, opts.Timeout)
	if err != nil {
		ctx.JSON(http.StatusRequestTimeout, private.Response{
			Err: fmt.Sprintf("%v", err),
		})
	}
	ctx.PlainText(http.StatusOK, "success")
}

// PauseLogging pauses logging
func PauseLogging(ctx *context.PrivateContext) {
	log.Pause()
	ctx.PlainText(http.StatusOK, "success")
}

// ResumeLogging resumes logging
func ResumeLogging(ctx *context.PrivateContext) {
	log.Resume()
	ctx.PlainText(http.StatusOK, "success")
}

// ReleaseReopenLogging releases and reopens logging files
func ReleaseReopenLogging(ctx *context.PrivateContext) {
	if err := log.ReleaseReopen(); err != nil {
		ctx.JSON(http.StatusInternalServerError, private.Response{
			Err: fmt.Sprintf("Error during release and reopen: %v", err),
		})
		return
	}
	ctx.PlainText(http.StatusOK, "success")
}

// SetLogSQL re-sets database SQL logging
func SetLogSQL(ctx *context.PrivateContext) {
	db.SetLogSQL(ctx, ctx.FormBool("on"))
	ctx.PlainText(http.StatusOK, "success")
}

// RemoveLogger removes a logger
func RemoveLogger(ctx *context.PrivateContext) {
	group := ctx.Params("group")
	name := ctx.Params("name")
	ok, err := log.GetLogger(group).DelLogger(name)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, private.Response{
			Err: fmt.Sprintf("Failed to remove logger: %s %s %v", group, name, err),
		})
		return
	}
	if ok {
		setting.RemoveSubLogDescription(group, name)
	}
	ctx.PlainText(http.StatusOK, fmt.Sprintf("Removed %s %s", group, name))
}

// AddLogger adds a logger
func AddLogger(ctx *context.PrivateContext) {
	opts := web.GetForm(ctx).(*private.LoggerOptions)
	if len(opts.Group) == 0 {
		opts.Group = log.DEFAULT
	}
	if _, ok := opts.Config["flags"]; !ok {
		switch opts.Group {
		case "access":
			opts.Config["flags"] = log.FlagsFromString("")
		case "router":
			opts.Config["flags"] = log.FlagsFromString("date,time")
		default:
			opts.Config["flags"] = log.FlagsFromString("stdflags")
		}
	}

	if _, ok := opts.Config["colorize"]; !ok && opts.Mode == "console" {
		if _, ok := opts.Config["stderr"]; ok {
			opts.Config["colorize"] = log.CanColorStderr
		} else {
			opts.Config["colorize"] = log.CanColorStdout
		}
	}

	if _, ok := opts.Config["level"]; !ok {
		opts.Config["level"] = setting.LogLevel
	}

	if _, ok := opts.Config["stacktraceLevel"]; !ok {
		opts.Config["stacktraceLevel"] = setting.StacktraceLogLevel
	}

	if opts.Mode == "file" {
		if _, ok := opts.Config["maxsize"]; !ok {
			opts.Config["maxsize"] = 1 << 28
		}
		if _, ok := opts.Config["maxdays"]; !ok {
			opts.Config["maxdays"] = 7
		}
		if _, ok := opts.Config["compressionLevel"]; !ok {
			opts.Config["compressionLevel"] = -1
		}
	}

	bufferLen := setting.Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000)
	byteConfig, err := json.Marshal(opts.Config)
	if err != nil {
		log.Error("Failed to marshal log configuration: %v %v", opts.Config, err)
		ctx.JSON(http.StatusInternalServerError, private.Response{
			Err: fmt.Sprintf("Failed to marshal log configuration: %v %v", opts.Config, err),
		})
		return
	}
	config := string(byteConfig)

	if err := log.NewNamedLogger(opts.Group, bufferLen, opts.Name, opts.Mode, config); err != nil {
		log.Error("Failed to create new named logger: %s %v", config, err)
		ctx.JSON(http.StatusInternalServerError, private.Response{
			Err: fmt.Sprintf("Failed to create new named logger: %s %v", config, err),
		})
		return
	}

	setting.AddSubLogDescription(opts.Group, setting.SubLogDescription{
		Name:     opts.Name,
		Provider: opts.Mode,
		Config:   config,
	})

	ctx.PlainText(http.StatusOK, "success")
}