Преглед изворни кода

Add Critical and Criticalf to Logger for capturing stacktraces

Signed-off-by: Robert Lin <robertlin1@gmail.com>
Robert Lin пре 6 година
родитељ
комит
c92aaacc8b

+ 12 - 4
internal/core/logger.go

@@ -18,6 +18,8 @@ type Logger interface {
 	Warnf(string, ...interface{})
 	Error(...interface{})
 	Errorf(string, ...interface{})
+	Critical(...interface{})
+	Criticalf(string, ...interface{})
 }
 
 // DefaultLogger is the default logger used by a pipeline, and wraps the standard
@@ -49,15 +51,21 @@ func (d *DefaultLogger) Warn(v ...interface{}) { d.W.Println(v...) }
 // Warnf writes to the "warning" logger with printf-style formatting.
 func (d *DefaultLogger) Warnf(f string, v ...interface{}) { d.W.Printf(f, v...) }
 
-// Error writes to the "error" logger and logs the current stacktrace.
-func (d *DefaultLogger) Error(v ...interface{}) {
+// Error writes to the "error" logger.
+func (d *DefaultLogger) Error(v ...interface{}) { d.E.Println(v...) }
+
+// Errorf writes to the "error" logger with printf-style formatting.
+func (d *DefaultLogger) Errorf(f string, v ...interface{}) { d.E.Printf(f, v...) }
+
+// Critical writes to the "error" logger and logs the current stacktrace.
+func (d *DefaultLogger) Critical(v ...interface{}) {
 	d.E.Println(v...)
 	d.logStacktraceToErr()
 }
 
-// Errorf writes to the "error" logger with printf-style formatting and logs the
+// Criticalf writes to the "error" logger with printf-style formatting and logs the
 // current stacktrace.
-func (d *DefaultLogger) Errorf(f string, v ...interface{}) {
+func (d *DefaultLogger) Criticalf(f string, v ...interface{}) {
 	d.E.Printf(f, v...)
 	d.logStacktraceToErr()
 }

+ 12 - 3
internal/core/logger_test.go

@@ -43,15 +43,24 @@ func TestLogger(t *testing.T) {
 
 	l.Error(v...)
 	assert.Contains(t, eBuf.String(), "[ERROR]")
-	assert.Contains(t, eBuf.String(), "internal/core.TestLogger")
-	assert.Contains(t, eBuf.String(), "internal/core/logger_test.go:44")
 	eBuf.Reset()
 
 	l.Errorf(f, v...)
 	assert.Contains(t, eBuf.String(), "[ERROR]")
 	assert.Contains(t, eBuf.String(), "-")
+	eBuf.Reset()
+
+	l.Critical(v...)
+	assert.Contains(t, eBuf.String(), "[ERROR]")
+	assert.Contains(t, eBuf.String(), "internal/core.TestLogger")
+	assert.Contains(t, eBuf.String(), "internal/core/logger_test.go:53")
+	eBuf.Reset()
+
+	l.Criticalf(f, v...)
+	assert.Contains(t, eBuf.String(), "[ERROR]")
+	assert.Contains(t, eBuf.String(), "-")
 	assert.Contains(t, eBuf.String(), "internal/core.TestLogger")
-	assert.Contains(t, eBuf.String(), "internal/core/logger_test.go:50")
+	assert.Contains(t, eBuf.String(), "internal/core/logger_test.go:59")
 	println(eBuf.String())
 	eBuf.Reset()
 }

+ 5 - 4
internal/core/pipeline.go

@@ -552,7 +552,8 @@ func (pipeline *Pipeline) resolve(dumpPath string) {
 						}
 						fmt.Fprintln(os.Stderr, "]")
 					}
-					panic("Failed to resolve pipeline dependencies: ambiguous graph.")
+					pipeline.l.Critical("Failed to resolve pipeline dependencies: ambiguous graph.")
+					return
 				}
 				ambiguousMap[key] = graph.FindParents(key)
 			}
@@ -569,7 +570,7 @@ func (pipeline *Pipeline) resolve(dumpPath string) {
 		for _, key := range item.Requires() {
 			key = "[" + key + "]"
 			if graph.AddEdge(key, name) == 0 {
-				pipeline.l.Errorf("Unsatisfied dependency: %s -> %s", key, item.Name())
+				pipeline.l.Criticalf("Unsatisfied dependency: %s -> %s", key, item.Name())
 				return
 			}
 		}
@@ -623,7 +624,7 @@ func (pipeline *Pipeline) resolve(dumpPath string) {
 	}
 	strplan, ok := graph.Toposort()
 	if !ok {
-		pipeline.l.Errorf("Failed to resolve pipeline dependencies: unable to topologically sort the items.")
+		pipeline.l.Critical("Failed to resolve pipeline dependencies: unable to topologically sort the items.")
 		return
 	}
 	pipeline.items = make([]PipelineItem, 0, len(pipeline.items))
@@ -810,7 +811,7 @@ func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]in
 					val, ok := update[key]
 					if !ok {
 						err := fmt.Errorf("%s: Consume() did not return %s", item.Name(), key)
-						pipeline.l.Error(err)
+						pipeline.l.Critical(err)
 						return nil, err
 					}
 					state[key] = val

+ 1 - 1
internal/plumbing/tree_diff.go

@@ -171,7 +171,7 @@ func (treediff *TreeDiff) Consume(deps map[string]interface{}) (map[string]inter
 	}
 	if !pass && treediff.previousCommit != plumbing.ZeroHash {
 		err := fmt.Errorf("%s > %s", treediff.previousCommit.String(), commit.Hash.String())
-		treediff.l.Error(err)
+		treediff.l.Critical(err)
 		return nil, err
 	}
 	tree, err := commit.Tree()

+ 3 - 3
leaves/couples.go

@@ -219,7 +219,7 @@ func (couples *CouplesAnalysis) Finalize() interface{} {
 		if err != nil {
 			err := fmt.Errorf("cannot find file %s in commit %s: %v",
 				name, couples.lastCommit.Hash.String(), err)
-			couples.l.Error(err)
+			couples.l.Critical(err)
 			return err
 		}
 		blob := items.CachedBlob{Blob: file.Blob}
@@ -227,7 +227,7 @@ func (couples *CouplesAnalysis) Finalize() interface{} {
 		if err != nil {
 			err := fmt.Errorf("cannot read blob %s of file %s: %v",
 				blob.Hash.String(), name, err)
-			couples.l.Error(err)
+			couples.l.Critical(err)
 			return err
 		}
 		filesLines[i], _ = blob.CountLines()
@@ -312,7 +312,7 @@ func (couples *CouplesAnalysis) Deserialize(pbmessage []byte) (interface{}, erro
 	if len(message.FileCouples.Index) != len(message.FilesLines) {
 		err := fmt.Errorf("Couples PB message integrity violation: file_couples (%d) != file_lines (%d)",
 			len(message.FileCouples.Index), len(message.FilesLines))
-		couples.l.Error(err)
+		couples.l.Critical(err)
 		return nil, err
 	}
 	for i, v := range message.FilesLines {