From 2e2c6702de4abeb96698d49b30f5a5526408d2da Mon Sep 17 00:00:00 2001 From: CFC4N Date: Tue, 28 May 2024 21:43:57 +0800 Subject: [PATCH 1/2] cli: support logger level Signed-off-by: CFC4N --- cli/cmd/root.go | 14 +++++++------- pkg/event_processor/iworker.go | 4 ++-- user/module/imodule.go | 17 +++++++++++++---- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/cli/cmd/root.go b/cli/cmd/root.go index a58462a67..20966d7ac 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -115,7 +115,7 @@ func init() { cobra.EnablePrefixMatching = true // Cobra also supports local flags, which will only run // when this action is called directly. - rootCmd.PersistentFlags().BoolVarP(&globalConf.Debug, "debug", "d", false, "enable debug logging.(coming soon)") + rootCmd.PersistentFlags().BoolVarP(&globalConf.Debug, "debug", "d", false, "enable debug logging") rootCmd.PersistentFlags().Uint8VarP(&globalConf.BtfMode, "btf", "b", 0, "enable BTF mode.(0:auto; 1:core; 2:non-core)") rootCmd.PersistentFlags().BoolVar(&globalConf.IsHex, "hex", false, "print byte strings as hex encoded strings") rootCmd.PersistentFlags().IntVar(&globalConf.PerCpuMapSize, "mapsize", 1024, "eBPF map size per CPU,for events buffer. default:1024 * PAGESIZE. (KB)") @@ -126,7 +126,7 @@ func init() { } // setModConfig set module config -func setModConfig(globalConf config.BaseConfig, modConf config.IConfig) error { +func setModConfig(globalConf config.BaseConfig, modConf config.IConfig) { modConf.SetPid(globalConf.Pid) modConf.SetUid(globalConf.Uid) modConf.SetDebug(globalConf.Debug) @@ -134,7 +134,6 @@ func setModConfig(globalConf config.BaseConfig, modConf config.IConfig) error { modConf.SetBTF(globalConf.BtfMode) modConf.SetPerCpuMapSize(globalConf.PerCpuMapSize) modConf.SetAddrType(loggerTypeStdout) - return nil } // initLogger init logger @@ -143,6 +142,10 @@ func initLogger(addr string, modConfig config.IConfig) zerolog.Logger { var err error consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339} logger = zerolog.New(consoleWriter).With().Timestamp().Logger() + zerolog.SetGlobalLevel(zerolog.InfoLevel) + if modConfig.GetDebug() { + zerolog.SetGlobalLevel(zerolog.DebugLevel) + } if addr != "" { var writer io.Writer var address string @@ -173,6 +176,7 @@ func initLogger(addr string, modConfig config.IConfig) zerolog.Logger { // runModule run module func runModule(modName string, modConfig config.IConfig) { var err error + setModConfig(globalConf, modConfig) var logger = initLogger(globalConf.LoggerAddr, modConfig) // init eCapture logger.Info().Str("AppName", fmt.Sprintf("%s(%s)", CliName, CliNameZh)).Send() @@ -203,10 +207,6 @@ func runModule(modName string, modConfig config.IConfig) { // run module { // config check - err = setModConfig(globalConf, modConfig) - if err != nil { - logger.Fatal().Err(err).Send() - } err = modConfig.Check() if err != nil { logger.Fatal().Err(err).Msg("config check failed") diff --git a/pkg/event_processor/iworker.go b/pkg/event_processor/iworker.go index acd3df613..6d833e7b2 100644 --- a/pkg/event_processor/iworker.go +++ b/pkg/event_processor/iworker.go @@ -116,8 +116,8 @@ func (ew *eventWorker) Display() error { b = []byte(hex.Dump(b)) } - // TODO 应该外部传入一个chan,iWorker只负责写入,不应该打印。 - e := ew.writeToChan(fmt.Sprintf("UUID:%s, Name:%s, Type:%d, Length:%d\n:%s\n", ew.UUID, ew.parser.Name(), ew.parser.ParserType(), len(b), b)) + //iWorker只负责写入,不应该打印。 + e := ew.writeToChan(fmt.Sprintf("UUID:%s, Name:%s, Type:%d, Length:%d\n%s\n", ew.UUID, ew.parser.Name(), ew.parser.ParserType(), len(b), b)) //ew.parser.Reset() // 设定状态、重置包类型 ew.status = ProcessStateInit diff --git a/user/module/imodule.go b/user/module/imodule.go index 1dae6e65a..01609f421 100644 --- a/user/module/imodule.go +++ b/user/module/imodule.go @@ -68,11 +68,20 @@ const ( BtfModeSwitch = "If eCapture fails to run, try specifying the BTF mode. use `-b 2` to specify non-CORE mode." ) +// eventProcesser Logger +type epLogger struct { + logger *zerolog.Logger +} + +func (e epLogger) Write(p []byte) (n int, err error) { + e.logger.Info().Msg(string(p)) + return len(p), nil +} + type Module struct { opts *ebpf.CollectionOptions reader []IClose ctx context.Context - //logger *zerolog.Logger logger *zerolog.Logger child IModule // probe的名字 @@ -95,8 +104,8 @@ func (m *Module) Init(ctx context.Context, logger *zerolog.Logger, conf config.I m.logger = logger m.errChan = make(chan error) m.isKernelLess5_2 = false //set false default - - m.processor = event_processor.NewEventProcessor(logger, conf.GetHex()) + var epl = epLogger{logger: logger} + m.processor = event_processor.NewEventProcessor(epl, conf.GetHex()) kv, err := kernel.HostVersion() if err != nil { m.logger.Warn().Err(err).Msg("Unable to detect kernel version due to an error:%v.used non-Less5_2 bytecode.") @@ -379,7 +388,7 @@ func (m *Module) Dispatcher(e event.IEventStruct) { if s == "" { return } - m.logger.Println(s) + m.logger.Info().Msg(s) case event.EventTypeEventProcessor: m.processor.Write(e) case event.EventTypeModuleData: From 566ae8fe059233dc9ffa2463d4a7e625517ae745 Mon Sep 17 00:00:00 2001 From: CFC4N Date: Tue, 28 May 2024 22:01:04 +0800 Subject: [PATCH 2/2] cli: update golangci/golangci-lint-action Signed-off-by: CFC4N --- .github/workflows/go-c-cpp.yml | 3 ++- pkg/util/kernel/version.go | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go-c-cpp.yml b/.github/workflows/go-c-cpp.yml index 858293165..bb79f7cf2 100644 --- a/.github/workflows/go-c-cpp.yml +++ b/.github/workflows/go-c-cpp.yml @@ -36,12 +36,13 @@ jobs: cd ./lib/libpcap/ && sudo make install cd $GITHUB_WORKSPACE - name: golangci-lint - uses: golangci/golangci-lint-action@v4 + uses: golangci/golangci-lint-action@v6 with: args: --disable-all -E errcheck -E staticcheck skip-cache: true skip-pkg-cache: true skip-build-cache: true + problem-matchers: true - name: Build NOCORE run: | make clean diff --git a/pkg/util/kernel/version.go b/pkg/util/kernel/version.go index 14f9b1623..126344c86 100644 --- a/pkg/util/kernel/version.go +++ b/pkg/util/kernel/version.go @@ -34,7 +34,10 @@ func HostVersion() (Version, error) { // ParseVersion parses a string in the format of x.x.x to a Version func ParseVersion(s string) Version { var a, b, c byte - fmt.Sscanf(s, "%d.%d.%d", &a, &b, &c) + _, err := fmt.Sscanf(s, "%d.%d.%d", &a, &b, &c) + if err != nil { + return Version(0) + } return VersionCode(a, b, c) }