You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

init.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package log
  4. import (
  5. "context"
  6. "runtime"
  7. "strings"
  8. "code.gitea.io/gitea/modules/process"
  9. "code.gitea.io/gitea/modules/util/rotatingfilewriter"
  10. )
  11. var projectPackagePrefix string
  12. func init() {
  13. _, filename, _, _ := runtime.Caller(0)
  14. projectPackagePrefix = strings.TrimSuffix(filename, "modules/log/init.go")
  15. if projectPackagePrefix == filename {
  16. // in case the source code file is moved, we can not trim the suffix, the code above should also be updated.
  17. panic("unable to detect correct package prefix, please update file: " + filename)
  18. }
  19. rotatingfilewriter.ErrorPrintf = FallbackErrorf
  20. process.TraceCallback = func(skip int, start bool, pid process.IDType, description string, parentPID process.IDType, typ string) {
  21. if start && parentPID != "" {
  22. Log(skip+1, TRACE, "Start %s: %s (from %s) (%s)", NewColoredValue(pid, FgHiYellow), description, NewColoredValue(parentPID, FgYellow), NewColoredValue(typ, Reset))
  23. } else if start {
  24. Log(skip+1, TRACE, "Start %s: %s (%s)", NewColoredValue(pid, FgHiYellow), description, NewColoredValue(typ, Reset))
  25. } else {
  26. Log(skip+1, TRACE, "Done %s: %s", NewColoredValue(pid, FgHiYellow), NewColoredValue(description, Reset))
  27. }
  28. }
  29. }
  30. func newProcessTypedContext(parent context.Context, desc string) (ctx context.Context, cancel context.CancelFunc) {
  31. // the "process manager" also calls "log.Trace()" to output logs, so if we want to create new contexts by the manager, we need to disable the trace temporarily
  32. process.TraceLogDisable(true)
  33. defer process.TraceLogDisable(false)
  34. ctx, _, cancel = process.GetManager().AddTypedContext(parent, desc, process.SystemProcessType, false)
  35. return ctx, cancel
  36. }