Skip to content

Commit

Permalink
cli: Use a formatted logger rs/zerolog (#539)
Browse files Browse the repository at this point in the history
* cli: Use a formatted logger

从golang的基本log类库更换为rs/zerolog.
Zero Allocation JSON Logger:https://github.com/rs/zerolog

* pkg: check error return value.
* pkg: refactoring the message parser and decoupling the logger

---------

Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
  • Loading branch information
cfc4n committed May 11, 2024
1 parent 84cab57 commit 3c53dd3
Show file tree
Hide file tree
Showing 50 changed files with 724 additions and 1,195 deletions.
54 changes: 1 addition & 53 deletions cli/cmd/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@
package cmd

import (
"context"
"github.com/gojue/ecapture/user/config"
"github.com/gojue/ecapture/user/module"
"github.com/spf13/cobra"
"log"
"os"
"os/signal"
"syscall"
)

var bc = config.NewBashConfig()
Expand Down Expand Up @@ -55,52 +50,5 @@ func init() {

// bashCommandFunc executes the "bash" command.
func bashCommandFunc(command *cobra.Command, args []string) {
stopper := make(chan os.Signal, 1)
signal.Notify(stopper, os.Interrupt, syscall.SIGTERM)
ctx, cancelFun := context.WithCancel(context.TODO())

mod := module.GetModuleByName(module.ModuleNameBash)

logger := log.New(os.Stdout, "bash_", log.LstdFlags)
logger.Printf("ECAPTURE :: version :%s", GitVersion)
logger.Printf("ECAPTURE :: start to run %s module", mod.Name())

// save global config
gConf, e := getGlobalConf(command)
if e != nil {
logger.Fatal(e)
os.Exit(1)
}
logger.SetOutput(gConf.writer)
bc.Pid = gConf.Pid
bc.Uid = gConf.Uid
bc.Debug = gConf.Debug
bc.IsHex = gConf.IsHex
bc.SetBTF(gConf.BtfMode)
bc.SetPerCpuMapSize(gConf.mapSizeKB)

logger.Printf("ECAPTURE :: pid info :%d", os.Getpid())
//bc.Pid = globalFlags.Pid
if e := bc.Check(); e != nil {
logger.Fatal(e)
os.Exit(1)
}

//初始化
err := mod.Init(ctx, logger, bc)
if err != nil {
logger.Fatal(err)
os.Exit(1)
}

// 加载ebpf,挂载到hook点上,开始监听
go func(module module.IModule) {
err := module.Run()
if err != nil {
logger.Fatalf("%v", err)
}
}(mod)
<-stopper
cancelFun()
os.Exit(0)
runModule(module.ModuleNameBash, bc)
}
103 changes: 0 additions & 103 deletions cli/cmd/global.go

This file was deleted.

101 changes: 1 addition & 100 deletions cli/cmd/gnutls.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@
package cmd

import (
"context"
"github.com/gojue/ecapture/pkg/util/kernel"
"github.com/gojue/ecapture/user/config"
"github.com/gojue/ecapture/user/module"
"log"
"os"
"os/signal"
"sync"
"syscall"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -55,96 +47,5 @@ func init() {

// gnuTlsCommandFunc executes the "bash" command.
func gnuTlsCommandFunc(command *cobra.Command, args []string) {
stopper := make(chan os.Signal, 1)
signal.Notify(stopper, os.Interrupt, syscall.SIGTERM)
ctx, cancelFun := context.WithCancel(context.TODO())

logger := log.New(os.Stdout, "tls_", log.LstdFlags)

// save global config
gConf, err := getGlobalConf(command)
if err != nil {
logger.Fatal(err)
}
logger.SetOutput(gConf.writer)

logger.Printf("ECAPTURE :: %s Version : %s", cliName, GitVersion)
logger.Printf("ECAPTURE :: Pid Info : %d", os.Getpid())
var version kernel.Version
version, _ = kernel.HostVersion() // it's safe to ignore error because we have checked it in func main
logger.Printf("ECAPTURE :: Kernel Info : %s", version.String())
modNames := []string{module.ModuleNameGnutls}

var runMods uint8
var runModules = make(map[string]module.IModule)
var wg sync.WaitGroup

for _, modName := range modNames {
mod := module.GetModuleByName(modName)
if mod == nil {
logger.Printf("ECAPTURE :: \tcant found module: %s", modName)
break
}
if gc == nil {
logger.Printf("ECAPTURE :: \tcant found module %s config info.", mod.Name())
break
}
var conf config.IConfig
conf = gc

conf.SetPid(gConf.Pid)
conf.SetUid(gConf.Uid)
conf.SetDebug(gConf.Debug)
conf.SetHex(gConf.IsHex)
conf.SetBTF(gConf.BtfMode)
conf.SetPerCpuMapSize(gConf.mapSizeKB)

err = conf.Check()

if err != nil {
logger.Printf("%s\tmodule initialization failed. [skip it]. error:%+v", mod.Name(), err)
continue
}

logger.Printf("%s\tmodule initialization", mod.Name())

//初始化
err = mod.Init(ctx, logger, conf)
if err != nil {
logger.Printf("%s\tmodule initialization failed, [skip it]. error:%+v", mod.Name(), err)
continue
}

err = mod.Run()
if err != nil {
logger.Printf("%s\tmodule run failed, [skip it]. error:%+v", mod.Name(), err)
continue
}
runModules[mod.Name()] = mod
logger.Printf("%s\tmodule started successfully.", mod.Name())
wg.Add(1)
runMods++
}

// needs runmods > 0
if runMods > 0 {
logger.Printf("ECAPTURE :: \tstart %d modules", runMods)
<-stopper
} else {
logger.Println("ECAPTURE :: \tNo runnable modules, Exit(1)")
os.Exit(1)
}
cancelFun()

// clean up
for _, mod := range runModules {
err = mod.Close()
wg.Done()
if err != nil {
logger.Fatalf("%s\tmodule close failed. error:%+v", mod.Name(), err)
}
}

wg.Wait()
os.Exit(0)
runModule(module.ModuleNameGnutls, gc)
}
86 changes: 2 additions & 84 deletions cli/cmd/gotls.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@
package cmd

import (
"context"
"errors"
"log"
"os"
"os/signal"
"strings"
"syscall"

"github.com/gojue/ecapture/pkg/util/kernel"
"github.com/gojue/ecapture/user/config"
"github.com/gojue/ecapture/user/module"
"strings"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -61,79 +53,5 @@ func goTLSCommandFunc(command *cobra.Command, args []string) {
goc.PcapFilter = strings.Join(args, " ")
}

stopper := make(chan os.Signal, 1)
signal.Notify(stopper, os.Interrupt, syscall.SIGTERM)

logger := log.New(os.Stdout, "tls_", log.LstdFlags)

// save global config
gConf, err := getGlobalConf(command)
if err != nil {
logger.Fatal(err)
}
logger.SetOutput(gConf.writer)
logger.Printf("ECAPTURE :: %s Version : %s", cliName, GitVersion)
logger.Printf("ECAPTURE :: Pid Info : %d", os.Getpid())
var version kernel.Version
version, _ = kernel.HostVersion() // it's safe to ignore error because we have checked it in func main
logger.Printf("ECAPTURE :: Kernel Info : %s", version.String())

mod := module.GetModuleByName(module.ModuleNameGotls)
if mod == nil {
logger.Printf("ECAPTURE :: \tcant found module: %s", module.ModuleNameGotls)
return
}
if goc == nil {
logger.Printf("ECAPTURE :: \tcant found module %s config info.", mod.Name())
return
}
var conf config.IConfig
conf = goc

conf.SetPid(gConf.Pid)
conf.SetUid(gConf.Uid)
conf.SetDebug(gConf.Debug)
conf.SetHex(gConf.IsHex)
conf.SetBTF(gConf.BtfMode)
conf.SetPerCpuMapSize(gConf.mapSizeKB)

err = conf.Check()

if err != nil {
// ErrorGoBINNotFound is a special error, we should not print it.
if errors.Is(err, config.ErrorGoBINNotFound) {
logger.Printf("%s\t%s, exec \"ecapture gotls --help\" for more detail.", mod.Name(), config.ErrorGoBINNotFound.Error())
logger.Printf("%s\tmodule [disabled].", mod.Name())
return
}

logger.Printf("%s\tmodule initialization failed. [skip it]. error:%+v", mod.Name(), err)
return
}

logger.Printf("%s\tmodule initialization", mod.Name())

// 初始化
err = mod.Init(context.TODO(), logger, conf)
if err != nil {
logger.Printf("%s\tmodule initialization failed, [skip it]. error:%+v", mod.Name(), err)
return
}

err = mod.Run()
if err != nil {
logger.Printf("%s\tmodule run failed, [skip it]. error:%+v", mod.Name(), err)
return
}

logger.Printf("%s\tmodule started successfully.", mod.Name())

<-stopper

// clean up
err = mod.Close()
if err != nil {
logger.Fatalf("%s\tmodule close failed. error:%+v", mod.Name(), err)
}
os.Exit(0)
runModule(module.ModuleNameGotls, goc)
}

0 comments on commit 3c53dd3

Please sign in to comment.