X Tutup
Skip to content

Commit fd25762

Browse files
committed
Add vbox.log to crashreport
Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
1 parent 32795c9 commit fd25762

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

libmachine/crashreport/crash_report.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313

1414
"errors"
1515

16+
"io/ioutil"
17+
1618
"github.com/bugsnag/bugsnag-go"
1719
"github.com/docker/machine/commands/mcndirs"
1820
"github.com/docker/machine/libmachine/log"
@@ -35,8 +37,7 @@ func Configure(key string) {
3537
}
3638
}
3739

38-
// Send through http the crash report to bugsnag need a call to Configure(apiKey) before
39-
func Send(err error, context string, driverName string, command string) error {
40+
func SendWithFile(err error, context string, driverName string, command string, path string) error {
4041
if noReportFileExist() || apiKey == noreportAPIKey {
4142
log.Debug("Opting out of crash reporting.")
4243
return nil
@@ -67,6 +68,7 @@ func Send(err error, context string, driverName string, command string) error {
6768
detectRunningShell(&metaData)
6869
detectUname(&metaData)
6970
detectOSVersion(&metaData)
71+
addFile(path, &metaData)
7072

7173
var buffer bytes.Buffer
7274
for _, message := range log.History() {
@@ -76,6 +78,25 @@ func Send(err error, context string, driverName string, command string) error {
7678
return bugsnag.Notify(err, metaData, bugsnag.SeverityError, bugsnag.Context{String: context}, bugsnag.ErrorClass{Name: fmt.Sprintf("%s/%s", driverName, command)})
7779
}
7880

81+
// Send through http the crash report to bugsnag need a call to Configure(apiKey) before
82+
func Send(err error, context string, driverName string, command string) error {
83+
return SendWithFile(err, context, driverName, command, "")
84+
}
85+
86+
func addFile(path string, metaData *bugsnag.MetaData) {
87+
file, err := os.Open(path)
88+
if err != nil {
89+
log.Debug(err)
90+
return
91+
}
92+
data, err := ioutil.ReadAll(file)
93+
if err != nil {
94+
log.Debug(err)
95+
return
96+
}
97+
metaData.Add("logfile", filepath.Base(path), string(data))
98+
}
99+
79100
func noReportFileExist() bool {
80101
optOutFilePath := filepath.Join(mcndirs.GetBaseDir(), "no-error-report")
81102
if _, err := os.Stat(optOutFilePath); os.IsNotExist(err) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package crashreport
2+
3+
import (
4+
"testing"
5+
6+
"io/ioutil"
7+
8+
"os"
9+
"path/filepath"
10+
11+
"github.com/bugsnag/bugsnag-go"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestFileIsNotReadWhenNotExisting(t *testing.T) {
16+
metaData := bugsnag.MetaData{}
17+
addFile("not existing", &metaData)
18+
assert.Empty(t, metaData)
19+
}
20+
21+
func TestRead(t *testing.T) {
22+
metaData := bugsnag.MetaData{}
23+
content := "foo\nbar\nqix\n"
24+
fileName := createTempFile(t, content)
25+
defer os.Remove(fileName)
26+
addFile(fileName, &metaData)
27+
assert.Equal(t, "foo\nbar\nqix\n", metaData["logfile"][filepath.Base(fileName)])
28+
}
29+
30+
func createTempFile(t *testing.T, content string) string {
31+
file, err := ioutil.TempFile("", "")
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
if err := ioutil.WriteFile(file.Name(), []byte(content), 0644); err != nil {
36+
t.Fatal(err)
37+
}
38+
return file.Name()
39+
}

libmachine/libmachine.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (api *Client) Create(h *host.Host) error {
9898
log.Info("Creating machine...")
9999

100100
if err := api.performCreate(h); err != nil {
101-
crashreport.Send(err, "api.performCreate", h.DriverName, "Create")
101+
sendCrashReport(err, api, h)
102102
return err
103103
}
104104

@@ -152,3 +152,12 @@ func (api *Client) performCreate(h *host.Host) error {
152152
return nil
153153

154154
}
155+
156+
func sendCrashReport(err error, api *Client, host *host.Host) {
157+
if host.DriverName == "virtualbox" {
158+
vboxlogPath := filepath.Join(api.GetMachinesDir(), host.Name, host.Name, "Logs", "VBox.log")
159+
crashreport.SendWithFile(err, "api.performCreate", host.DriverName, "Create", vboxlogPath)
160+
} else {
161+
crashreport.Send(err, "api.performCreate", host.DriverName, "Create")
162+
}
163+
}

0 commit comments

Comments
 (0)
X Tutup