File: log.go

package info (click to toggle)
packer 1.3.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,324 kB
  • sloc: python: 619; sh: 557; makefile: 111
file content (46 lines) | stat: -rw-r--r-- 1,070 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strings"
)

// 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
	if os.Getenv(EnvLog) != "" && os.Getenv(EnvLog) != "0" {
		logOutput = os.Stderr

		if logPath := os.Getenv(EnvLogFile); logPath != "" {
			var err error
			logOutput, err = os.Create(logPath)
			if err != nil {
				return nil, err
			}
		} 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
					}
					os.Stderr.WriteString(fmt.Sprint(scanner.Text() + "\n"))
				}
			}(scanner)
			logOutput = w
		}
	}

	return
}