Merge pull request #536 from Masterminds/better-vcs-output-msgs

Better vcs output msgs
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cd27a18..45e126d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,11 @@
+# Unreleased
+
+## Added
+- #533: Log VCS output with debug (`--debug` switch) when there was a VCS error (thanks @atombender)
+
+## Changed
+- #521: Sort subpackages for glide.yaml and glide.lock to avoid spurious diffs
+
 # Release 0.11.1 (2016-07-21)
 
 ## Fixed
diff --git a/msg/msg.go b/msg/msg.go
index b0961d3..8b0f077 100644
--- a/msg/msg.go
+++ b/msg/msg.go
@@ -177,7 +177,7 @@
 	// When operations in Glide are happening concurrently messaging needs to be
 	// locked to avoid displaying one message in the middle of another one.
 	m.Lock()
-
+	defer m.Unlock()
 	// Get rid of the annoying fact that messages need \n at the end, but do
 	// it in a backward compatible way.
 	if !strings.HasSuffix(msg, "\n") {
@@ -190,25 +190,22 @@
 		fmt.Fprintf(m.Stderr, msg, args...)
 	}
 
-	m.Unlock()
-
-	if len(args) != 0 {
+	// If an arg is a vcs error print the output if in debug mode. This is
+	// capured here rather than calling Debug because concurrent operations
+	// could cause other messages to appear between the initial error and the
+	// debug output by unlocking and calling Debug.
+	if len(args) != 0 && !m.Quiet && m.IsDebugging {
 		if err, ok := args[len(args)-1].(error); ok {
-			m.printErrorAsDebug(err)
+			switch t := err.(type) {
+			case *vcs.LocalError:
+				fmt.Fprintf(m.Stderr, "[DEBUG]\tOutput was: %s", strings.TrimSpace(t.Out()))
+			case *vcs.RemoteError:
+				fmt.Fprintf(m.Stderr, "[DEBUG]\tOutput was: %s", strings.TrimSpace(t.Out()))
+			}
 		}
 	}
 }
 
-func (m *Messenger) printErrorAsDebug(err error) {
-	// TODO: It'd be nice if the error was an interface, not a struct
-	switch t := err.(type) {
-	case *vcs.LocalError:
-		m.Debug("Output was: %s", strings.TrimSpace(t.Out()))
-	case *vcs.RemoteError:
-		m.Debug("Output was: %s", strings.TrimSpace(t.Out()))
-	}
-}
-
 // Msg prints a message with optional arguments, that can be printed, of
 // varying types using the Default Messenger.
 func Msg(msg string, args ...interface{}) {