1 Star 0 Fork 0

sqos/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
console.go 1.97 KB
一键复制 编辑 原始数据 按行查看 历史
Nicolas Ruflin 提交于 2017-04-28 09:45 . Fix console output (#4045) (#4130)
package console
import (
"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"
"github.com/elastic/beats/libbeat/outputs/codecs/json"
)
func init() {
outputs.RegisterOutputPlugin("console", New)
}
type console struct {
out *os.File
codec outputs.Codec
}
func New(_ string, config *common.Config, _ int) (outputs.Outputer, error) {
var unpackedConfig Config
err := config.Unpack(&unpackedConfig)
if err != nil {
return nil, err
}
var codec outputs.Codec
if unpackedConfig.Codec.Namespace.IsSet() {
codec, err = outputs.CreateEncoder(unpackedConfig.Codec)
if err != nil {
return nil, err
}
} else {
codec = json.New(unpackedConfig.Pretty)
}
c, err := newConsole(codec)
if err != nil {
return nil, fmt.Errorf("console output initialization failed with: %v", 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(codec outputs.Codec) (*console, error) {
return &console{codec: codec, out: os.Stdout}, nil
}
// Implement Outputer
func (c *console) Close() error {
return nil
}
var nl = []byte{'\n'}
func (c *console) PublishEvent(
s op.Signaler,
opts outputs.Options,
data outputs.Data,
) error {
serializedEvent, err := c.codec.Encode(data.Event)
if err = c.writeBuffer(serializedEvent); err != nil {
goto fail
}
if err = c.writeBuffer(nl); 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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqos/beats.git
git@gitee.com:sqos/beats.git
sqos
beats
beats
v5.4.2

搜索帮助