Add vbox.log to crashreport

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-17 13:42:09 +01:00
parent 32795c9d1f
commit fd25762c05
3 changed files with 72 additions and 3 deletions

View File

@ -13,6 +13,8 @@ import (
"errors"
"io/ioutil"
"github.com/bugsnag/bugsnag-go"
"github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/libmachine/log"
@ -35,8 +37,7 @@ func Configure(key string) {
}
}
// Send through http the crash report to bugsnag need a call to Configure(apiKey) before
func Send(err error, context string, driverName string, command string) error {
func SendWithFile(err error, context string, driverName string, command string, path string) error {
if noReportFileExist() || apiKey == noreportAPIKey {
log.Debug("Opting out of crash reporting.")
return nil
@ -67,6 +68,7 @@ func Send(err error, context string, driverName string, command string) error {
detectRunningShell(&metaData)
detectUname(&metaData)
detectOSVersion(&metaData)
addFile(path, &metaData)
var buffer bytes.Buffer
for _, message := range log.History() {
@ -76,6 +78,25 @@ func Send(err error, context string, driverName string, command string) error {
return bugsnag.Notify(err, metaData, bugsnag.SeverityError, bugsnag.Context{String: context}, bugsnag.ErrorClass{Name: fmt.Sprintf("%s/%s", driverName, command)})
}
// Send through http the crash report to bugsnag need a call to Configure(apiKey) before
func Send(err error, context string, driverName string, command string) error {
return SendWithFile(err, context, driverName, command, "")
}
func addFile(path string, metaData *bugsnag.MetaData) {
file, err := os.Open(path)
if err != nil {
log.Debug(err)
return
}
data, err := ioutil.ReadAll(file)
if err != nil {
log.Debug(err)
return
}
metaData.Add("logfile", filepath.Base(path), string(data))
}
func noReportFileExist() bool {
optOutFilePath := filepath.Join(mcndirs.GetBaseDir(), "no-error-report")
if _, err := os.Stat(optOutFilePath); os.IsNotExist(err) {

View File

@ -0,0 +1,39 @@
package crashreport
import (
"testing"
"io/ioutil"
"os"
"path/filepath"
"github.com/bugsnag/bugsnag-go"
"github.com/stretchr/testify/assert"
)
func TestFileIsNotReadWhenNotExisting(t *testing.T) {
metaData := bugsnag.MetaData{}
addFile("not existing", &metaData)
assert.Empty(t, metaData)
}
func TestRead(t *testing.T) {
metaData := bugsnag.MetaData{}
content := "foo\nbar\nqix\n"
fileName := createTempFile(t, content)
defer os.Remove(fileName)
addFile(fileName, &metaData)
assert.Equal(t, "foo\nbar\nqix\n", metaData["logfile"][filepath.Base(fileName)])
}
func createTempFile(t *testing.T, content string) string {
file, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(file.Name(), []byte(content), 0644); err != nil {
t.Fatal(err)
}
return file.Name()
}

View File

@ -98,7 +98,7 @@ func (api *Client) Create(h *host.Host) error {
log.Info("Creating machine...")
if err := api.performCreate(h); err != nil {
crashreport.Send(err, "api.performCreate", h.DriverName, "Create")
sendCrashReport(err, api, h)
return err
}
@ -152,3 +152,12 @@ func (api *Client) performCreate(h *host.Host) error {
return nil
}
func sendCrashReport(err error, api *Client, host *host.Host) {
if host.DriverName == "virtualbox" {
vboxlogPath := filepath.Join(api.GetMachinesDir(), host.Name, host.Name, "Logs", "VBox.log")
crashreport.SendWithFile(err, "api.performCreate", host.DriverName, "Create", vboxlogPath)
} else {
crashreport.Send(err, "api.performCreate", host.DriverName, "Create")
}
}