From b371237ddea93594a81af07a6be5444b7dc6ad56 Mon Sep 17 00:00:00 2001 From: boyce <6549168@qq.com> Date: Wed, 25 Mar 2026 18:24:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=93=9D=E5=9B=BE=E6=96=B0=E5=A2=9Elogger?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/blueprint/blueprint.go | 65 +++++++++++++++++++++++++++++-------- util/blueprint/graph.go | 1 + util/blueprint/node.go | 16 ++++++--- 3 files changed, 64 insertions(+), 18 deletions(-) diff --git a/util/blueprint/blueprint.go b/util/blueprint/blueprint.go index 59257c5..e218c11 100644 --- a/util/blueprint/blueprint.go +++ b/util/blueprint/blueprint.go @@ -2,15 +2,27 @@ package blueprint import ( "fmt" - "sync/atomic" "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 { - execNodes []IExecNode // 注册的定义执行结点 + execNodes []IExecNode // 注册的定义执行结点 execNodeList []func() IExecNode - execPool *ExecPool - graphPool *GraphPool + execPool *ExecPool + graphPool *GraphPool blueprintModule IBlueprintModule mapGraph map[int64]IGraph @@ -18,7 +30,9 @@ type Blueprint struct { cancelTimer func(*uint64) bool execDefFilePath string // 执行结点定义文件路径 - graphFilePath string // 蓝图文件路径 + graphFilePath string // 蓝图文件路径 + + logger IBlueprintLogger // 蓝图执行日志记录器 } func (bm *Blueprint) RegisterExecNode(execNodeFunc func() IExecNode) { @@ -31,7 +45,7 @@ type IExecNodeType[T any] interface { } // 生成一个泛型函数,返回func() IExecNode类型 -func NewExecNode[T any, P IExecNodeType[T]]() func() IExecNode { +func NewExecNode[T any, P IExecNodeType[T]]() func() IExecNode { return func() IExecNode { var t T return P(&t) @@ -74,8 +88,7 @@ func (bm *Blueprint) regSysNodes() { bm.RegisterExecNode(NewExecNode[IntInArray]()) } - -func (bm *Blueprint) StartHotReload() (func(),error) { +func (bm *Blueprint) StartHotReload() (func(), error) { var execPool ExecPool var graphPool GraphPool @@ -89,7 +102,7 @@ func (bm *Blueprint) StartHotReload() (func(),error) { for _, newExec := range bm.execNodeList { e := newExec() 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 { - var execPool ExecPool +func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprintModule IBlueprintModule, cancelTimer func(*uint64) bool, logger ...IBlueprintLogger) error { + var execPool ExecPool var graphPool GraphPool // 加载配置结点生成名字对应的innerExecNode @@ -154,6 +167,11 @@ func (bm *Blueprint) Init(execDefFilePath string, graphFilePath string, blueprin bm.execDefFilePath = execDefFilePath bm.graphFilePath = graphFilePath + // 设置logger + if len(logger) > 0 { + bm.logger = logger[0] + } + return nil } @@ -189,11 +207,13 @@ func (bm *Blueprint) Do(graphID int64, entranceID int64, args ...any) (Port_Arra } clone := graph.Clone() + // 设置logger + if g, ok := clone.(*Graph); ok { + g.logger = bm.logger + } return clone.Do(entranceID, args...) } - - func (bm *Blueprint) ReleaseGraph(graphID int64) { if graphID == 0 { return @@ -225,3 +245,22 @@ func (bm *Blueprint) CancelTimerId(graphID int64, timerId *uint64) bool { delete(gr.mapTimerID, tId) 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() +} diff --git a/util/blueprint/graph.go b/util/blueprint/graph.go index aa71bea..0ef6666 100644 --- a/util/blueprint/graph.go +++ b/util/blueprint/graph.go @@ -38,6 +38,7 @@ type Graph struct { graphContext IBlueprintModule mapTimerID map[uint64]struct{} + logger IBlueprintLogger } type graphContext struct { diff --git a/util/blueprint/node.go b/util/blueprint/node.go index a292b08..551a966 100644 --- a/util/blueprint/node.go +++ b/util/blueprint/node.go @@ -143,7 +143,7 @@ func (en *execNode) doSetInPort(gr *Graph, index int, inPort IPort) error { func (en *execNode) Do(gr *Graph, outPortArgs ...any) error { 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) - if err != nil { - return err + nextIndex, execErr := en.exec(gr) + + // 调用logger记录执行日志 + if gr.logger != nil { + gr.logger.LogNodeExec(en.execNode.GetName(), en.Id, inPorts, outPorts, nextIndex, execErr) + } + + if execErr != nil { + return execErr } 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 {