This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
log.Logger api changes #1617
Merged
Merged
log.Logger api changes #1617
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ package gps | |
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
@@ -48,6 +47,22 @@ type solution struct { | |
solv Solver | ||
} | ||
|
||
// WriteProgress informs about the progress of WriteDepTree. | ||
type WriteProgress struct { | ||
Count int | ||
Total int | ||
LP LockedProject | ||
Failure bool | ||
} | ||
|
||
func (p WriteProgress) String() string { | ||
msg := "Wrote" | ||
if p.Failure { | ||
msg = "Failed to write" | ||
} | ||
return fmt.Sprintf("(%d/%d) %s %s@%s", p.Count, p.Total, msg, p.LP.Ident(), p.LP.Version()) | ||
} | ||
|
||
const concurrentWriters = 16 | ||
|
||
// WriteDepTree takes a basedir, a Lock and a RootPruneOptions and exports all | ||
|
@@ -58,7 +73,9 @@ const concurrentWriters = 16 | |
// | ||
// It requires a SourceManager to do the work. Prune options are read from the | ||
// passed manifest. | ||
func WriteDepTree(basedir string, l Lock, sm SourceManager, co CascadingPruneOptions, logger *log.Logger) error { | ||
// | ||
// If onWrite is not nil, it will be called after each project write. Calls are ordered and atomic. | ||
func WriteDepTree(basedir string, l Lock, sm SourceManager, co CascadingPruneOptions, onWrite func(WriteProgress)) error { | ||
if l == nil { | ||
return fmt.Errorf("must provide non-nil Lock to WriteDepTree") | ||
} | ||
|
@@ -95,7 +112,7 @@ func WriteDepTree(basedir string, l Lock, sm SourceManager, co CascadingPruneOpt | |
return errors.Wrapf(err, "failed to export %s", projectRoot) | ||
} | ||
|
||
err := PruneProject(to, p, co.PruneOptionsFor(ident.ProjectRoot), logger) | ||
err := PruneProject(to, p, co.PruneOptionsFor(ident.ProjectRoot)) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to prune %s", projectRoot) | ||
} | ||
|
@@ -105,18 +122,20 @@ func WriteDepTree(basedir string, l Lock, sm SourceManager, co CascadingPruneOpt | |
|
||
switch err { | ||
case context.Canceled, context.DeadlineExceeded: | ||
// Don't log "secondary" errors. | ||
// Don't report "secondary" errors. | ||
default: | ||
msg := "Wrote" | ||
if err != nil { | ||
msg = "Failed to write" | ||
if onWrite != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No more locking to serialize to |
||
// Increment and call atomically to prevent re-ordering. | ||
cnt.Lock() | ||
cnt.i++ | ||
onWrite(WriteProgress{ | ||
Count: cnt.i, | ||
Total: len(lps), | ||
LP: p, | ||
Failure: err != nil, | ||
}) | ||
cnt.Unlock() | ||
} | ||
|
||
// Log and increment atomically to prevent re-ordering. | ||
cnt.Lock() | ||
cnt.i++ | ||
logger.Printf("(%d/%d) %s %s@%s\n", cnt.i, len(lps), msg, p.Ident(), p.Version()) | ||
cnt.Unlock() | ||
} | ||
|
||
return err | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than log or nothing, callers get a rich type they can inspect and which also knows how to log itself.