mirror of
https://github.com/go-gitea/gitea
synced 2024-11-05 13:49:10 +01:00
c53ad052d8
Some bugs caused by less unit tests in fundamental packages. This PR refactor `setting` package so that create a unit test will be easier than before. - All `LoadFromXXX` files has been splited as two functions, one is `InitProviderFromXXX` and `LoadCommonSettings`. The first functions will only include the code to create or new a ini file. The second function will load common settings. - It also renames all functions in setting from `newXXXService` to `loadXXXSetting` or `loadXXXFrom` to make the function name less confusing. - Move `XORMLog` to `SQLLog` because it's a better name for that. Maybe we should finally move these `loadXXXSetting` into the `XXXInit` function? Any idea? --------- Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: delvh <dev.lh@web.de>
164 lines
4.5 KiB
Go
164 lines
4.5 KiB
Go
// 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.Log.Level
|
|
}
|
|
|
|
if _, ok := opts.Config["stacktraceLevel"]; !ok {
|
|
opts.Config["stacktraceLevel"] = setting.Log.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.Log.BufferLength
|
|
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")
|
|
}
|