Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: support logger level #558

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/go-c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand All @@ -126,15 +126,14 @@ 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)
modConf.SetHex(globalConf.IsHex)
modConf.SetBTF(globalConf.BtfMode)
modConf.SetPerCpuMapSize(globalConf.PerCpuMapSize)
modConf.SetAddrType(loggerTypeStdout)
return nil
}

// initLogger init logger
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions pkg/event_processor/iworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pkg/util/kernel/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 13 additions & 4 deletions user/module/imodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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的名字
Expand All @@ -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.")
Expand Down Expand Up @@ -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:
Expand Down
Loading