0

Not realtime

After executable finishes

I have the following code to get the logs of an executable inspired by this post. The fmt.Println(outb.String()) works fine. It prints all the logs after the command-line executable finishes.


import (
    "bytes"
    "fmt"
    "os"
    "os/exec"
)

func Run() error {
    pthExe := "." + string(os.PathSeparator) + "command_line.exe"
    cmd := exec.Command(pthExe)
    var outb, errb bytes.Buffer
    cmd.Stdout = &outb
    cmd.Stderr = &errb
    err := cmd.Run()
    if err != nil {
        return err
    }
    fmt.Println(outb.String()) // Required to have the logs.
    fmt.Println(errb.String()) // Required.
    return nil
}

Question

Is there a way that I can print the logs in realtime? I mean not when the executable finishes, but while it is logging.

Megidd
  • 7,089
  • 6
  • 65
  • 142

0 Answers0