代码拉取完成,页面将自动刷新
package console
import (
"encoding/json"
"fmt"
"os"
"runtime"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/op"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
)
func init() {
outputs.RegisterOutputPlugin("console", New)
}
type console struct {
config config
out *os.File
}
func New(_ string, config *common.Config, _ int) (outputs.Outputer, error) {
c := &console{config: defaultConfig, out: os.Stdout}
err := config.Unpack(&c.config)
if err != nil {
return nil, err
}
// check stdout actually being available
if runtime.GOOS != "windows" {
if _, err = c.out.Stat(); err != nil {
return nil, fmt.Errorf("console output initialization failed with: %v", err)
}
}
return c, nil
}
func newConsole(pretty bool) *console {
return &console{config: config{pretty}, out: os.Stdout}
}
// Implement Outputer
func (c *console) Close() error {
return nil
}
func (c *console) PublishEvent(
s op.Signaler,
opts outputs.Options,
data outputs.Data,
) error {
var jsonEvent []byte
var err error
if c.config.Pretty {
jsonEvent, err = json.MarshalIndent(data.Event, "", " ")
} else {
jsonEvent, err = json.Marshal(data.Event)
}
if err != nil {
logp.Err("Fail to convert the event to JSON (%v): %#v", err, data.Event)
op.SigCompleted(s)
return err
}
if err = c.writeBuffer(jsonEvent); err != nil {
goto fail
}
if err = c.writeBuffer([]byte{'\n'}); err != nil {
goto fail
}
op.SigCompleted(s)
return nil
fail:
if opts.Guaranteed {
logp.Critical("Unable to publish events to console: %v", err)
}
op.SigFailed(s, err)
return err
}
func (c *console) writeBuffer(buf []byte) error {
written := 0
for written < len(buf) {
n, err := c.out.Write(buf[written:])
if err != nil {
return err
}
written += n
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。