Browse Source

Replace fmt.Fprintf(os.Stderr) with log.Printf()

Vadim Markovtsev 7 years ago
parent
commit
c723fb076d
8 changed files with 31 additions and 31 deletions
  1. 7 8
      blob_cache.go
  2. 9 9
      burndown.go
  3. 1 1
      changes_xpather.go
  4. 2 2
      changes_xpather_test.go
  5. 3 2
      cmd/hercules/root.go
  6. 3 2
      pipeline.go
  7. 2 3
      renames.go
  8. 4 4
      shotness.go

+ 7 - 8
blob_cache.go

@@ -1,8 +1,7 @@
 package hercules
 
 import (
-	"fmt"
-	"os"
+	"log"
 
 	"gopkg.in/src-d/go-git.v4"
 	"gopkg.in/src-d/go-git.v4/config"
@@ -94,7 +93,7 @@ func (blobCache *BlobCache) Consume(deps map[string]interface{}) (map[string]int
 	for _, change := range changes {
 		action, err := change.Action()
 		if err != nil {
-			fmt.Fprintf(os.Stderr, "no action in %s\n", change.To.TreeEntry.Hash)
+			log.Printf("no action in %s\n", change.To.TreeEntry.Hash)
 			return nil, err
 		}
 		var exists bool
@@ -103,7 +102,7 @@ func (blobCache *BlobCache) Consume(deps map[string]interface{}) (map[string]int
 		case merkletrie.Insert:
 			blob, err = blobCache.getBlob(&change.To, commit.File)
 			if err != nil {
-				fmt.Fprintf(os.Stderr, "file to %s %s\n", change.To.Name, change.To.TreeEntry.Hash)
+				log.Printf("file to %s %s\n", change.To.Name, change.To.TreeEntry.Hash)
 			} else {
 				cache[change.To.TreeEntry.Hash] = blob
 				newCache[change.To.TreeEntry.Hash] = blob
@@ -114,7 +113,7 @@ func (blobCache *BlobCache) Consume(deps map[string]interface{}) (map[string]int
 				cache[change.From.TreeEntry.Hash], err = blobCache.getBlob(&change.From, commit.File)
 				if err != nil {
 					if err.Error() != plumbing.ErrObjectNotFound.Error() {
-						fmt.Fprintf(os.Stderr, "file from %s %s\n", change.From.Name,
+						log.Printf("file from %s %s\n", change.From.Name,
 							change.From.TreeEntry.Hash)
 					} else {
 						cache[change.From.TreeEntry.Hash], err = createDummyBlob(
@@ -125,7 +124,7 @@ func (blobCache *BlobCache) Consume(deps map[string]interface{}) (map[string]int
 		case merkletrie.Modify:
 			blob, err = blobCache.getBlob(&change.To, commit.File)
 			if err != nil {
-				fmt.Fprintf(os.Stderr, "file to %s\n", change.To.Name)
+				log.Printf("file to %s\n", change.To.Name)
 			} else {
 				cache[change.To.TreeEntry.Hash] = blob
 				newCache[change.To.TreeEntry.Hash] = blob
@@ -134,7 +133,7 @@ func (blobCache *BlobCache) Consume(deps map[string]interface{}) (map[string]int
 			if !exists {
 				cache[change.From.TreeEntry.Hash], err = blobCache.getBlob(&change.From, commit.File)
 				if err != nil {
-					fmt.Fprintf(os.Stderr, "file from %s\n", change.From.Name)
+					log.Printf("file from %s\n", change.From.Name)
 				}
 			}
 		}
@@ -157,7 +156,7 @@ func (blobCache *BlobCache) getBlob(entry *object.ChangeEntry, fileGetter FileGe
 	blob, err := blobCache.repository.BlobObject(entry.TreeEntry.Hash)
 	if err != nil {
 		if err.Error() != plumbing.ErrObjectNotFound.Error() {
-			fmt.Fprintf(os.Stderr, "getBlob(%s)\n", entry.TreeEntry.Hash.String())
+			log.Printf("getBlob(%s)\n", entry.TreeEntry.Hash.String())
 			return nil, err
 		}
 		if entry.TreeEntry.Mode != 0160000 {

+ 9 - 9
burndown.go

@@ -4,7 +4,7 @@ import (
 	"errors"
 	"fmt"
 	"io"
-	"os"
+	"log"
 	"sort"
 	"sync"
 	"unicode/utf8"
@@ -205,17 +205,17 @@ func (analyser *BurndownAnalysis) Flag() string {
 // calls. The repository which is going to be analysed is supplied as an argument.
 func (analyser *BurndownAnalysis) Initialize(repository *git.Repository) {
 	if analyser.Granularity <= 0 {
-		fmt.Fprintf(os.Stderr, "Warning: adjusted the granularity to %d days\n",
+		log.Printf("Warning: adjusted the granularity to %d days\n",
 			DefaultBurndownGranularity)
 		analyser.Granularity = DefaultBurndownGranularity
 	}
 	if analyser.Sampling <= 0 {
-		fmt.Fprintf(os.Stderr, "Warning: adjusted the sampling to %d days\n",
+		log.Printf("Warning: adjusted the sampling to %d days\n",
 			DefaultBurndownGranularity)
 		analyser.Sampling = DefaultBurndownGranularity
 	}
 	if analyser.Sampling > analyser.Granularity {
-		fmt.Fprintf(os.Stderr, "Warning: granularity may not be less than sampling, adjusted to %d\n",
+		log.Printf("Warning: granularity may not be less than sampling, adjusted to %d\n",
 			analyser.Granularity)
 		analyser.Sampling = analyser.Granularity
 	}
@@ -929,7 +929,7 @@ func (analyser *BurndownAnalysis) handleModification(
 
 	thisDiffs := diffs[change.To.Name]
 	if file.Len() != thisDiffs.OldLinesOfCode {
-		fmt.Fprintf(os.Stderr, "====TREE====\n%s", file.Dump())
+		log.Printf("====TREE====\n%s", file.Dump())
 		return fmt.Errorf("%s: internal integrity error src %d != %d %s -> %s",
 			change.To.Name, thisDiffs.OldLinesOfCode, file.Len(),
 			change.From.TreeEntry.Hash.String(), change.To.TreeEntry.Hash.String())
@@ -960,13 +960,13 @@ func (analyser *BurndownAnalysis) handleModification(
 		}
 		length := utf8.RuneCountInString(edit.Text)
 		debugError := func() {
-			fmt.Fprintf(os.Stderr, "%s: internal diff error\n", change.To.Name)
-			fmt.Fprintf(os.Stderr, "Update(%d, %d, %d (0), %d (0))\n", analyser.day, position,
+			log.Printf("%s: internal diff error\n", change.To.Name)
+			log.Printf("Update(%d, %d, %d (0), %d (0))\n", analyser.day, position,
 				length, utf8.RuneCountInString(pending.Text))
 			if dumpBefore != "" {
-				fmt.Fprintf(os.Stderr, "====TREE BEFORE====\n%s====END====\n", dumpBefore)
+				log.Printf("====TREE BEFORE====\n%s====END====\n", dumpBefore)
 			}
-			fmt.Fprintf(os.Stderr, "====TREE AFTER====\n%s====END====\n", file.Dump())
+			log.Printf("====TREE AFTER====\n%s====END====\n", file.Dump())
 		}
 		switch edit.Type {
 		case diffmatchpatch.DiffEqual:

+ 1 - 1
changes_xpather.go

@@ -4,8 +4,8 @@ import (
 	"log"
 
 	"github.com/minio/highwayhash"
-	"gopkg.in/bblfsh/sdk.v1/uast"
 	"gopkg.in/bblfsh/client-go.v2/tools"
+	"gopkg.in/bblfsh/sdk.v1/uast"
 	"gopkg.in/src-d/go-git.v4/plumbing"
 )
 

+ 2 - 2
changes_xpather_test.go

@@ -5,11 +5,11 @@ import (
 	"log"
 	"testing"
 
+	"github.com/stretchr/testify/assert"
 	"gopkg.in/bblfsh/client-go.v2"
-	"gopkg.in/src-d/go-git.v4/plumbing"
 	"gopkg.in/bblfsh/sdk.v1/uast"
+	"gopkg.in/src-d/go-git.v4/plumbing"
 	"gopkg.in/src-d/go-git.v4/plumbing/object"
-	"github.com/stretchr/testify/assert"
 )
 
 func TestChangesXPatherExtractChanged(t *testing.T) {

+ 3 - 2
cmd/hercules/root.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"io"
 	"io/ioutil"
+	"log"
 	"net/http"
 	_ "net/http/pprof"
 	"os"
@@ -59,7 +60,7 @@ func loadRepository(uri string, cachePath string, disableStatus bool) *git.Repos
 			}
 			_, err = os.Stat(cachePath)
 			if !os.IsNotExist(err) {
-				fmt.Fprintf(os.Stderr, "warning: deleted %s\n", cachePath)
+				log.Printf("warning: deleted %s\n", cachePath)
 				os.RemoveAll(cachePath)
 			}
 		} else {
@@ -118,7 +119,7 @@ func loadPlugins() {
 	for path := range pluginFlags {
 		_, err := plugin.Open(path)
 		if err != nil {
-			fmt.Fprintf(os.Stderr, "Failed to load plugin from %s %s\n", path, err)
+			log.Printf("Failed to load plugin from %s %s\n", path, err)
 		}
 	}
 }

+ 3 - 2
pipeline.go

@@ -6,6 +6,7 @@ import (
 	"fmt"
 	"io"
 	"io/ioutil"
+	"log"
 	"os"
 	"path/filepath"
 	"sort"
@@ -494,7 +495,7 @@ func (pipeline *Pipeline) resolve(dumpPath string) {
 		// fmt.Fprint(os.Stderr, graphCopy.DebugDump())
 		ioutil.WriteFile(dumpPath, []byte(graphCopy.Serialize(strplan)), 0666)
 		absPath, _ := filepath.Abs(dumpPath)
-		fmt.Fprintf(os.Stderr, "Wrote the DAG to %s\n", absPath)
+		log.Printf("Wrote the DAG to %s\n", absPath)
 	}
 }
 
@@ -541,7 +542,7 @@ func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]in
 		for _, item := range pipeline.items {
 			update, err := item.Consume(state)
 			if err != nil {
-				fmt.Fprintf(os.Stderr, "%s failed on commit #%d %s\n",
+				log.Printf("%s failed on commit #%d %s\n",
 					item.Name(), index, commit.Hash.String())
 				return nil, err
 			}

+ 2 - 3
renames.go

@@ -1,8 +1,7 @@
 package hercules
 
 import (
-	"fmt"
-	"os"
+	"log"
 	"sort"
 	"unicode/utf8"
 
@@ -79,7 +78,7 @@ func (ra *RenameAnalysis) Configure(facts map[string]interface{}) {
 // calls. The repository which is going to be analysed is supplied as an argument.
 func (ra *RenameAnalysis) Initialize(repository *git.Repository) {
 	if ra.SimilarityThreshold < 0 || ra.SimilarityThreshold > 100 {
-		fmt.Fprintf(os.Stderr, "Warning: adjusted the similarity threshold to %d\n",
+		log.Printf("Warning: adjusted the similarity threshold to %d\n",
 			RenameAnalysisDefaultThreshold)
 		ra.SimilarityThreshold = RenameAnalysisDefaultThreshold
 	}

+ 4 - 4
shotness.go

@@ -3,7 +3,7 @@ package hercules
 import (
 	"fmt"
 	"io"
-	"os"
+	"log"
 	"sort"
 	"unicode/utf8"
 
@@ -193,7 +193,7 @@ func (shotness *ShotnessAnalysis) Consume(deps map[string]interface{}) (map[stri
 		if change.Before == nil {
 			nodes, err := shotness.extractNodes(change.After)
 			if err != nil {
-				fmt.Fprintf(os.Stderr, "Shotness: commit %s file %s failed to filter UAST: %s\n",
+				log.Printf("Shotness: commit %s file %s failed to filter UAST: %s\n",
 					commit.Hash.String(), toName, err.Error())
 				continue
 			}
@@ -229,14 +229,14 @@ func (shotness *ShotnessAnalysis) Consume(deps map[string]interface{}) (map[stri
 		// pass through new UAST
 		nodesBefore, err := shotness.extractNodes(change.Before)
 		if err != nil {
-			fmt.Fprintf(os.Stderr, "Shotness: commit ^%s file %s failed to filter UAST: %s\n",
+			log.Printf("Shotness: commit ^%s file %s failed to filter UAST: %s\n",
 				commit.Hash.String(), change.Change.From.Name, err.Error())
 			continue
 		}
 		reversedNodesBefore := reverseNodeMap(nodesBefore)
 		nodesAfter, err := shotness.extractNodes(change.After)
 		if err != nil {
-			fmt.Fprintf(os.Stderr, "Shotness: commit %s file %s failed to filter UAST: %s\n",
+			log.Printf("Shotness: commit %s file %s failed to filter UAST: %s\n",
 				commit.Hash.String(), toName, err.Error())
 			continue
 		}