diff options
-rw-r--r-- | util/autoport/log_maker.go | 97 |
1 files changed, 72 insertions, 25 deletions
diff --git a/util/autoport/log_maker.go b/util/autoport/log_maker.go index 5ca8284c29..ed157c70df 100644 --- a/util/autoport/log_maker.go +++ b/util/autoport/log_maker.go @@ -12,14 +12,20 @@ import ( "bytes" ) -func TryRunAndSave(output string, name string, arg []string) error { - cmd := exec.Command(name, arg...) +type LogMakingProgram struct { + name string + prefixes []string + args []string +} +func (prog LogMakingProgram) TryRunAndSave(output string, prefix string) error { f, err := os.Create(output) if err != nil { log.Fatal(err) } + defer f.Close() + cmd := exec.Command(prefix+prog.name, prog.args...) cmd.Stdout = f cmd.Stderr = f @@ -27,25 +33,35 @@ func TryRunAndSave(output string, name string, arg []string) error { if err != nil { return err } - cmd.Wait() - return nil + return cmd.Wait() } -func RunAndSave(output string, name string, arg ...string) { - err := TryRunAndSave(output, name, arg) - if err == nil { - return - } - idx := strings.LastIndex(name, "/") - relname := name - if idx >= 0 { - relname = name[idx+1:] - } - relname = "./" + relname - err = TryRunAndSave(output, relname, arg) - if err != nil { - log.Fatal(err) +func (prog LogMakingProgram) RunAndSave(outDir string) { + output := fmt.Sprintf("%s/%s.log", outDir, prog.name) + cmdline := strings.Join(append([]string{prog.name}, prog.args...), " ") + + fmt.Println("Running: "+cmdline) + + var sb strings.Builder + for _, prefix := range prog.prefixes { + err := prog.TryRunAndSave(output, prefix) + if err == nil { + return + } + sb.WriteString("\nError running '"+prefix+cmdline+"': "+err.Error()+"\n") + data, ferr := os.ReadFile(output) + if ferr != nil { + sb.WriteString("<failed to open log>\n") + } else { + if len(data) > 0 { + sb.WriteString("Program output:\n\n") + sb.WriteString(string(data)) + } + } } + + fmt.Println("\nCould not run program: '"+cmdline+"'") + log.Fatal(sb.String()) } const MAXPROMPTRETRY = 3 @@ -140,11 +156,8 @@ func MakeHDALogs(outDir string, cardName string) { func MakeLogs(outDir string) { os.MkdirAll(outDir, 0700) - RunAndSave(outDir+"/lspci.log", "lspci", "-nnvvvxxxx") - RunAndSave(outDir+"/dmidecode.log", "dmidecode") - RunAndSave(outDir+"/acpidump.log", "acpidump") - probeGFX := PromptUserBool("WARNING: The following tool MAY cause your system to hang when it attempts "+ + probeGFX := PromptUserBool("WARNING: Running inteltool MAY cause your system to hang when it attempts "+ "to probe for graphics registers. Having the graphics registers will help create a better port. "+ "Should autoport probe these registers?", true) @@ -154,9 +167,43 @@ func MakeLogs(outDir string) { inteltoolArgs += "f" } - RunAndSave(outDir+"/inteltool.log", "../inteltool/inteltool", inteltoolArgs) - RunAndSave(outDir+"/ectool.log", "../ectool/ectool", "-pd") - RunAndSave(outDir+"/superiotool.log", "../superiotool/superiotool", "-ade") + var programs = []LogMakingProgram { + LogMakingProgram { + name: "lspci", + prefixes: []string{""}, + args: []string{"-nnvvvxxxx"}, + }, + LogMakingProgram { + name: "dmidecode", + prefixes: []string{""}, + args: []string{}, + }, + LogMakingProgram { + name: "acpidump", + prefixes: []string{""}, + args: []string{}, + }, + LogMakingProgram { + name: "inteltool", + prefixes: []string{"../inteltool/", ""}, + args: []string{inteltoolArgs}, + }, + LogMakingProgram { + name: "ectool", + prefixes: []string{"../ectool/", ""}, + args: []string{"-pd"}, + }, + LogMakingProgram { + name: "superiotool", + prefixes: []string{"../superiotool/", ""}, + args: []string{"-ade"}, + }, + } + + fmt.Println("Making logs...") + for _, prog := range programs { + prog.RunAndSave(outDir) + } SysSound := "/sys/class/sound/" card := "" |