mirror of
https://github.com/hengyoush/kyanos.git
synced 2025-12-20 01:03:46 +08:00
fix: output logs to file during loading
This commit is contained in:
@@ -78,6 +78,7 @@ func SetupAgent(options ac.AgentOptions) {
|
||||
{
|
||||
bf, err := loader.LoadBPF(&options)
|
||||
if err != nil {
|
||||
common.AgentLog.Error("Failed to load BPF programs: ", err)
|
||||
if bf != nil {
|
||||
bf.Close()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kyanos/agent/common"
|
||||
c "kyanos/common"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -108,6 +109,7 @@ func (m model) View() string {
|
||||
}
|
||||
|
||||
func Start(ctx context.Context, options common.AgentOptions) {
|
||||
c.SetLogToFile()
|
||||
const numLastResults = 5
|
||||
m := model{
|
||||
spinner: spinner.New(spinner.WithSpinner(spinner.Dot)),
|
||||
|
||||
@@ -63,9 +63,9 @@ func generateBTF(fileBytes []byte) (*btf.Spec, error) {
|
||||
return btfPath, nil
|
||||
}
|
||||
|
||||
func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
|
||||
func loadBTFSpec(options *ac.AgentOptions) (*btf.Spec, error) {
|
||||
if bpf.IsKernelSupportHasBTF() {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
options.LoadPorgressChannel <- "starting load BTF file"
|
||||
@@ -73,7 +73,8 @@ func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
|
||||
if options.BTFFilePath != "" {
|
||||
btfPath, err := btf.LoadSpec(options.BTFFilePath)
|
||||
if err != nil {
|
||||
common.AgentLog.Fatalf("can't load btf spec from file %s: %v", options.BTFFilePath, err)
|
||||
common.AgentLog.Warnf("can't load btf spec from file %s: %v\n", options.BTFFilePath, err)
|
||||
return nil, err
|
||||
}
|
||||
spec = btfPath
|
||||
options.LoadPorgressChannel <- "starting load BTF file: success!"
|
||||
@@ -84,11 +85,11 @@ func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
|
||||
if needGenerateBTF {
|
||||
spec, err = generateBTF(fileBytes)
|
||||
if err != nil {
|
||||
common.AgentLog.Warnf("failed to generate btf file: %+v", err)
|
||||
common.AgentLog.Warnf("failed to generate btf file: %+v\n", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
common.AgentLog.Warnf("failed to load embeded btf file: %+v", err)
|
||||
common.AgentLog.Warnf("failed to load embeded btf file: %+v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +98,7 @@ func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
|
||||
options.LoadPorgressChannel <- "starting load BTF from network..."
|
||||
btfSpec, _, err := loadBTFSpecFallback("")
|
||||
if err != nil {
|
||||
common.AgentLog.Warnf("failed to get btf file from network: %+v", err)
|
||||
common.AgentLog.Warnf("failed to get btf file from network: %+v\n", err)
|
||||
} else {
|
||||
spec = btfSpec
|
||||
}
|
||||
@@ -110,18 +111,18 @@ func loadBTFSpec(options *ac.AgentOptions) *btf.Spec {
|
||||
if needGenerateBTF {
|
||||
spec, err = generateBTF(fileBytes)
|
||||
if err != nil {
|
||||
common.AgentLog.Warnf("failed to generate btf file (best matched): %+v", err)
|
||||
common.AgentLog.Warnf("failed to generate btf file (best matched): %+v\n", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
common.AgentLog.Warnf("failed to load embedded btf file (best matched): %+v", err)
|
||||
common.AgentLog.Warnf("failed to load embedded btf file (best matched): %+v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
if spec == nil {
|
||||
common.AgentLog.Fatalf("can't find btf file to load!")
|
||||
return nil, fmt.Errorf("can't find btf file to load!")
|
||||
}
|
||||
return spec
|
||||
return spec, nil
|
||||
}
|
||||
|
||||
func loadBTFSpecFallback(path string) (*btf.Spec, string, error) {
|
||||
|
||||
@@ -57,12 +57,17 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) {
|
||||
var bf *BPF = &BPF{}
|
||||
|
||||
if err := features.HaveProgramType(ebpf.Kprobe); errors.Is(err, ebpf.ErrNotSupported) {
|
||||
common.AgentLog.Fatalf("Require oldest kernel version is 3.10.0-957, pls check your kernel version by `uname -r`")
|
||||
common.AgentLog.Errorf("Require oldest kernel version is 3.10.0-957, pls check your kernel version by `uname -r`\n")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
btfSpec, err := loadBTFSpec(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
collectionOptions = &ebpf.CollectionOptions{
|
||||
Programs: ebpf.ProgramOptions{
|
||||
KernelTypes: loadBTFSpec(options),
|
||||
KernelTypes: btfSpec,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -36,8 +36,13 @@ var ConntrackLog *Klogger = &Klogger{logrus.New()}
|
||||
var ProtocolParserLog *Klogger = &Klogger{logrus.New()}
|
||||
|
||||
var Loggers []*Klogger = []*Klogger{DefaultLog, AgentLog, BPFLog, BPFEventLog, UprobeLog, ConntrackLog, ProtocolParserLog}
|
||||
var SetLogToFileFlag = false
|
||||
|
||||
func SetLogToFile() {
|
||||
if SetLogToFileFlag {
|
||||
return
|
||||
}
|
||||
SetLogToFileFlag = true
|
||||
for _, l := range Loggers {
|
||||
l.SetOut(io.Discard)
|
||||
logdir := "/tmp"
|
||||
|
||||
4
go.sum
4
go.sum
@@ -317,8 +317,6 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
|
||||
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
|
||||
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
|
||||
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
|
||||
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0=
|
||||
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
@@ -343,8 +341,6 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
|
||||
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
|
||||
Reference in New Issue
Block a user