蓝图新增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 (
"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()
}

View File

@@ -38,6 +38,7 @@ type Graph struct {
graphContext
IBlueprintModule
mapTimerID map[uint64]struct{}
logger IBlueprintLogger
}
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 {
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 {