This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
95 lines (84 loc) · 2.54 KB
/
log.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package main
import (
"bufio"
"bytes"
"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)
scanner.Split(ScanLinesSmallerThanBuffer)
go func(scanner *bufio.Scanner) {
for scanner.Scan() {
if strings.Contains(scanner.Text(), "ui:") {
continue
}
if strings.Contains(scanner.Text(), "ui error:") {
continue
}
os.Stderr.WriteString(fmt.Sprint(scanner.Text() + "\n"))
}
if err := scanner.Err(); err != nil {
os.Stderr.WriteString(err.Error())
w.Close()
}
}(scanner)
logOutput = w
}
}
return
}
// The below functions come from bufio.Scanner with a small tweak, to fix an
// edgecase where the default ScanFunc fails: sometimes, if someone tries to
// log a line that is longer than 64*1024 bytes long before it contains a
// newline, the ScanLine will continue to return, requesting more data from the
// buffer, which can't increase in size anymore, causing a hang.
func dropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0 : len(data)-1]
}
return data
}
func ScanLinesSmallerThanBuffer(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, dropCR(data[0:i]), nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), dropCR(data), nil
}
// Our tweak:
// Buffer is full, so we can't get more data. Just return what we have as
// its own token so we can keep going, even though there's no newline.
if len(data)+1 >= bufio.MaxScanTokenSize {
return len(data), data[0 : len(data)-1], nil
}
// Request more data.
return 0, nil, nil
}