Files
siyuan/kernel/plugin/api_logger.go
Yingyi / 颖逸 16036ae701 🎨 Implement kernel plugin lifecycle management and client plugin API (#17761)
* 🎨 Implement kernel plugin lifecycle management and state updates

* 🎨 Refactor kernel plugin to use options object in constructor and enhance event bus integration

* 🎨 Move kernel initialization to after plugin onload for improved lifecycle management

* 🎨 Improve error handling and argument validation in kernel plugin methods

* 🎨 Enhance error handling in removePluginSourceWatch by returning error

* 🎨 Refactor kernel plugin state management by removing 'loaded' state and related lifecycle events

* Fix running state code in kernel plugin state management
2026-05-27 17:26:10 +08:00

101 lines
3.1 KiB
Go

// SiYuan - Refactor your thinking
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package plugin
import (
"fmt"
"strings"
"github.com/dop251/goja"
"github.com/samber/lo"
"github.com/siyuan-note/logging"
)
type Printer struct {
name string // plugin name for log prefix
}
func (p *Printer) Log(s string) {
go print(p.name, s, logging.LogInfof)
}
func (p *Printer) Warn(s string) {
go print(p.name, s, logging.LogWarnf)
}
func (p *Printer) Error(s string) {
go print(p.name, s, logging.LogErrorf)
}
// print logs the message line by line with the plugin name as prefix, using the provided log function.
func print(name, s string, logf func(format string, v ...interface{})) {
rows := strings.Split(s, "\n")
for _, row := range rows {
logf("[plugin:%s] %s", name, row)
}
}
// injectLogger adds siyuan.logger to the goja context.
func injectLogger(p *KernelPlugin, rt *goja.Runtime, siyuan *goja.Object) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("injectLogger: %v", r)
}
}()
logger := rt.NewObject()
lo.Must0(logger.Set("trace", rt.ToValue(loggerWrapper(p, logging.LogTracef))))
lo.Must0(logger.Set("debug", rt.ToValue(loggerWrapper(p, logging.LogDebugf))))
lo.Must0(logger.Set("info", rt.ToValue(loggerWrapper(p, logging.LogInfof))))
lo.Must0(logger.Set("warn", rt.ToValue(loggerWrapper(p, logging.LogWarnf))))
lo.Must0(logger.Set("error", rt.ToValue(loggerWrapper(p, logging.LogErrorf))))
lo.Must0(ObjectFreeze(rt, logger))
lo.Must0(siyuan.Set("logger", logger))
return
}
// loggerWrapper returns a JS-callable function that logs a message at the given level.
// Logging is synchronous (in-process, no I/O), so resolve is called inline without worker.Run.
func loggerWrapper(p *KernelPlugin, logf func(format string, args ...any)) func(goja.FunctionCall, *goja.Runtime) goja.Value {
return func(call goja.FunctionCall, rt *goja.Runtime) goja.Value {
parts := make([]string, 0, len(call.Arguments))
for _, arg := range call.Arguments {
if goja.IsString(arg) {
parts = append(parts, arg.String())
continue
}
if arg == nil || goja.IsUndefined(arg) || goja.IsNull(arg) {
parts = append(parts, arg.String())
continue
}
argObj := arg.ToObject(rt)
if argObj != nil {
if argJson, marshalErr := argObj.MarshalJSON(); marshalErr == nil {
parts = append(parts, string(argJson))
continue
}
}
parts = append(parts, arg.String())
}
go print(p.Name, strings.Join(parts, " "), logf)
return goja.Undefined()
}
}