code.gitea.io/gitea@v1.22.3/modules/log/init.go (about)

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