蓝图新增logger功能

This commit is contained in:
boyce
2026-03-25 18:24:31 +08:00
parent e6550b6428
commit b371237dde
3 changed files with 64 additions and 18 deletions

View File

@@ -2,15 +2,27 @@ package blueprint
import ( import (
"fmt" "fmt"
"sync/atomic"
"github.com/duanhf2012/origin/v2/log" "github.com/duanhf2012/origin/v2/log"
"sync/atomic"
) )
// IBlueprintLogger 蓝图执行日志接口
type IBlueprintLogger interface {
// LogNodeExec 记录结点执行日志
// nodeName: 结点名称
// nodeID: 结点ID
// inPorts: 输入端口列表
// outPorts: 输出端口列表
// execResult: 执行结果(-1表示中断>=0表示后续执行的索引
// err: 执行错误
LogNodeExec(nodeName string, nodeID string, inPorts []IPort, outPorts []IPort, execResult int, err error)
}
type Blueprint struct { type Blueprint struct {
execNodes []IExecNode // 注册的定义执行结点 execNodes []IExecNode // 注册的定义执行结点
execNodeList []func() IExecNode execNodeList []func() IExecNode
execPool *ExecPool execPool *ExecPool
graphPool *GraphPool graphPool *GraphPool
blueprintModule IBlueprintModule blueprintModule IBlueprintModule
mapGraph map[int64]IGraph mapGraph map[int64]IGraph
@@ -18,7 +30,9 @@ type Blueprint struct {
cancelTimer func(*uint64) bool cancelTimer func(*uint64) bool
execDefFilePath string // 执行结点定义文件路径 execDefFilePath string // 执行结点定义文件路径
graphFilePath string // 蓝图文件路径 graphFilePath string // 蓝图文件路径
logger IBlueprintLogger // 蓝图执行日志记录器
} }
func (bm *Blueprint) RegisterExecNode(execNodeFunc func() IExecNode) { func (bm *Blueprint) RegisterExecNode(execNodeFunc func() IExecNode) {
@@ -31,7 +45,7 @@ type IExecNodeType[T any] interface {
} }
// 生成一个泛型函数返回func() IExecNode类型 // 生成一个泛型函数返回func() IExecNode类型
func NewExecNode[T any, P IExecNodeType[T]]() func() IExecNode { func NewExecNode[T any, P IExecNodeType[T]]() func() IExecNode {
return func() IExecNode { return func() IExecNode {
var t T var t T
return P(&t) return P(&t)
@@ -74,8 +88,7 @@ func (bm *Blueprint) regSysNodes() {
bm.RegisterExecNode(NewExecNode[IntInArray]()) bm.RegisterExecNode(NewExecNode[IntInArray]())
} }
func (bm *Blueprint) StartHotReload() (func(), error) {
func (bm *Blueprint) StartHotReload() (func(),error) {
var execPool ExecPool var execPool ExecPool
var graphPool GraphPool var graphPool GraphPool
@@ -89,7 +102,7 @@ func (bm *Blueprint) StartHotReload() (func(),error) {
for _, newExec := range bm.execNodeList { for _, newExec := range bm.execNodeList {
e := newExec() e := newExec()
if !execPool.Register(e) { if !execPool.Register(e) {
return nil,fmt.Errorf("register exec failed,exec:%s", e.GetName()) return nil, fmt.Errorf("register exec failed,exec:%s", e.GetName())
} }
} }
@@ -119,8 +132,8 @@ func (bm *Blueprint) StartHotReload() (func(),error) {
} }
func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule, cancelTimer func(*uint64) bool) error { func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule, cancelTimer func(*uint64) bool, logger ...IBlueprintLogger) error {
var execPool ExecPool var execPool ExecPool
var graphPool GraphPool var graphPool GraphPool
// 加载配置结点生成名字对应的innerExecNode // 加载配置结点生成名字对应的innerExecNode
@@ -154,6 +167,11 @@ func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprin
bm.execDefFilePath = execDefFilePath bm.execDefFilePath = execDefFilePath
bm.graphFilePath = graphFilePath bm.graphFilePath = graphFilePath
// 设置logger
if len(logger) > 0 {
bm.logger = logger[0]
}
return nil return nil
} }
@@ -189,11 +207,13 @@ func (bm *Blueprint) Do(graphID int64, entranceID int64, args ...any) (Port_Arra
} }
clone := graph.Clone() clone := graph.Clone()
// 设置logger
if g, ok := clone.(*Graph); ok {
g.logger = bm.logger
}
return clone.Do(entranceID, args...) return clone.Do(entranceID, args...)
} }
func (bm *Blueprint) ReleaseGraph(graphID int64) { func (bm *Blueprint) ReleaseGraph(graphID int64) {
if graphID == 0 { if graphID == 0 {
return return
@@ -225,3 +245,22 @@ func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool {
delete(gr.mapTimerID, tId) delete(gr.mapTimerID, tId)
return true return true
} }
// GetLogger 获取logger
func (bm *Blueprint) GetLogger() IBlueprintLogger {
return bm.logger
}
// GetGraphName 通过graphId获取蓝图名称
func (bm *Blueprint) GetGraphName(graphID int64) string {
if graphID == 0 {
return ""
}
graph := bm.mapGraph[graphID]
if graph == nil {
return ""
}
return graph.GetGraphFileName()
}

View File

@@ -38,6 +38,7 @@ type Graph struct {
graphContext graphContext
IBlueprintModule IBlueprintModule
mapTimerID map[uint64]struct{} mapTimerID map[uint64]struct{}
logger IBlueprintLogger
} }
type graphContext struct { type graphContext struct {

View File

@@ -143,7 +143,7 @@ func (en *execNode) doSetInPort(gr *Graph, index int, inPort IPort) error {
func (en *execNode) Do(gr *Graph, outPortArgs ...any) error { func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
if IsDebug { if IsDebug {
log.Debug("Start ExecNode", log.String("Name",en.execNode.GetName())) log.Debug("Start ExecNode", log.String("Name", en.execNode.GetName()))
} }
// 重新初始化上下文 // 重新初始化上下文
@@ -180,13 +180,19 @@ func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
// 设置执行器相关的上下文信息 // 设置执行器相关的上下文信息
// 如果是变量设置变量名 // 如果是变量设置变量名
// 执行本结点 // 执行本结点
nextIndex, err := en.exec(gr) nextIndex, execErr := en.exec(gr)
if err != nil {
return err // 调用logger记录执行日志
if gr.logger != nil {
gr.logger.LogNodeExec(en.execNode.GetName(), en.Id, inPorts, outPorts, nextIndex, execErr)
}
if execErr != nil {
return execErr
} }
if IsDebug { if IsDebug {
log.Debug("End ExecNode", log.String("Name",en.execNode.GetName()),log.Any("InPort",inPorts ),log.Any("OutPort",outPorts)) log.Debug("End ExecNode", log.String("Name", en.execNode.GetName()), log.Any("InPort", inPorts), log.Any("OutPort", outPorts))
} }
if nextIndex == -1 || en.nextNode == nil { if nextIndex == -1 || en.nextNode == nil {