Added result logging functionality #2

Merged
gballan merged 2 commits from log-to-file into master 2022-02-26 20:48:37 -05:00
3 changed files with 21 additions and 3 deletions
Showing only changes of commit 65c23c480d - Show all commits

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
"os/exec" "os/exec"
@ -30,7 +31,14 @@ var diskCmd = &cobra.Command{
outfile := fmt.Sprintf("of=%s", tempfile) outfile := fmt.Sprintf("of=%s", tempfile)
ddCmd := exec.Command("dd", "bs=1M", blocksCount, "if=/dev/zero", outfile) ddCmd := exec.Command("dd", "bs=1M", blocksCount, "if=/dev/zero", outfile)
if Log != "false" {
f, _ := os.Create(Log)
ddCmd.Stderr = io.MultiWriter(os.Stderr, f)
} else {
ddCmd.Stderr = os.Stdout ddCmd.Stderr = os.Stdout
}
err = ddCmd.Run() err = ddCmd.Run()
if err != nil { if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err) log.Fatalf("cmd.Run() failed with %s\n", err)

View File

@ -5,6 +5,8 @@ import (
"github.com/showwin/speedtest-go/speedtest" "github.com/showwin/speedtest-go/speedtest"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"git.metaunix.net/bitgoblin/hardware-tests/lib"
) )
var netCmd = &cobra.Command{ var netCmd = &cobra.Command{
@ -22,7 +24,14 @@ var netCmd = &cobra.Command{
s.DownloadTest(false) s.DownloadTest(false)
s.UploadTest(false) s.UploadTest(false)
fmt.Printf("Latency: %s, Download: %f, Upload: %f\n", s.Latency, s.DLSpeed, s.ULSpeed) // put together the result
result := fmt.Sprintf("Latency: %s, Download: %f, Upload: %f\n", s.Latency, s.DLSpeed, s.ULSpeed)
// log the result if it's configured, otherwise print it out
if Log != "false" {
lib.WriteToFile(Log, result)
} else {
fmt.Printf("%s", result)
}
} }
}, },
} }

View File

@ -31,7 +31,8 @@ var pingCmd = &cobra.Command{
result += fmt.Sprintf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n", result += fmt.Sprintf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt) stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
if Log != nil { // log the result if it's configured, otherwise print it out
if Log != "false" {
lib.WriteToFile(Log, result) lib.WriteToFile(Log, result)
} else { } else {
fmt.Printf("%s", result) fmt.Printf("%s", result)