2014-10-27 23:31:02 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-21 18:24:17 -04:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2014-10-27 23:31:02 -04:00
|
|
|
"io"
|
|
|
|
"os"
|
2018-08-21 18:24:17 -04:00
|
|
|
"strings"
|
2014-10-27 23:31:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// These are the environmental variables that determine if we log, and if
|
|
|
|
// we log whether or not the log should go to a file.
|
|
|
|
const EnvLog = "PACKER_LOG" //Set to True
|
|
|
|
const EnvLogFile = "PACKER_LOG_PATH" //Set to a file
|
|
|
|
|
|
|
|
// logOutput determines where we should send logs (if anywhere).
|
|
|
|
func logOutput() (logOutput io.Writer, err error) {
|
|
|
|
logOutput = nil
|
2016-10-07 15:10:20 -04:00
|
|
|
if os.Getenv(EnvLog) != "" && os.Getenv(EnvLog) != "0" {
|
2014-10-27 23:31:02 -04:00
|
|
|
logOutput = os.Stderr
|
|
|
|
|
|
|
|
if logPath := os.Getenv(EnvLogFile); logPath != "" {
|
|
|
|
var err error
|
|
|
|
logOutput, err = os.Create(logPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-21 18:24:17 -04:00
|
|
|
} else {
|
|
|
|
// no path; do a little light filtering to avoid double-dipping UI
|
|
|
|
// calls.
|
|
|
|
r, w := io.Pipe()
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
go func(scanner *bufio.Scanner) {
|
|
|
|
for scanner.Scan() {
|
|
|
|
if strings.Contains(scanner.Text(), "ui:") {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-09 22:43:54 -04:00
|
|
|
os.Stderr.WriteString(fmt.Sprint(scanner.Text() + "\n"))
|
2018-08-21 18:24:17 -04:00
|
|
|
}
|
|
|
|
}(scanner)
|
|
|
|
logOutput = w
|
2014-10-27 23:31:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|