forked from microsoft/hcsshim
-
Notifications
You must be signed in to change notification settings - Fork 0
User/maksiman/add binary logging support #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| *.exe | ||
| .idea |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| Example logger, which writes to 2 different files | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "os" | ||
| "sync" | ||
|
|
||
| "github.com/Microsoft/hcsshim/cmd/logging" | ||
| ) | ||
|
|
||
| func _main() { | ||
| logging.Run(logger) | ||
| } | ||
|
|
||
| func logger(_ context.Context, config *logging.Config, ready func() error) error { | ||
| var wg sync.WaitGroup | ||
| wg.Add(2) | ||
|
|
||
| fileOut, err := os.Create("C:/Users/Administrator/LCOW/container-stdout.txt") | ||
| defer fileOut.Close() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fileErr, err := os.Create("C:/Users/Administrator/LCOW/container-stderr.txt") | ||
| defer fileErr.Close() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| go func() { | ||
| defer wg.Done() | ||
| io.Copy(fileOut, config.Stdout) | ||
| }() | ||
|
|
||
| go func() { | ||
| defer wg.Done() | ||
| io.Copy(fileErr, config.Stderr) | ||
| }() | ||
|
|
||
| if err := ready(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| wg.Wait() | ||
| return nil | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package logging | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/Microsoft/go-winio" | ||
| ) | ||
|
|
||
| // Config is passed to the binary logging function | ||
| type Config struct { | ||
| ID string | ||
| Namespace string | ||
| Stdout io.Reader | ||
| Stderr io.Reader | ||
| } | ||
|
|
||
| // LoggerFunc is a binary logging function signature | ||
| type LoggerFunc func(context.Context, *Config, func() error) error | ||
|
|
||
| // Run runs LoggerFunc | ||
| func Run(fn LoggerFunc) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| var errCh = make(chan error, 0) | ||
|
|
||
| sout, _ := winio.DialPipeContext(ctx, os.Getenv("CONTAINER_STDOUT")) | ||
| serr, _ := winio.DialPipeContext(ctx, os.Getenv("CONTAINER_STDERR")) | ||
| wait, _ := winio.DialPipeContext(ctx, os.Getenv("CONTAINER_WAIT")) | ||
|
|
||
| config := &Config{ | ||
| ID: os.Getenv("CONTAINER_ID"), | ||
| Namespace: os.Getenv("CONTAINER_NAMESPACE"), | ||
| Stdout: sout, | ||
| Stderr: serr, | ||
| } | ||
|
|
||
| // Write to wait pipe | ||
| ready := func() error { | ||
| wait.Write([]byte("#")) | ||
| return wait.Close() | ||
| } | ||
|
|
||
| f, _ := os.Create("C:/Users/Administrator/LCOW/binary-results.txt") | ||
| defer f.Close() | ||
|
anmaxvl marked this conversation as resolved.
|
||
|
|
||
| w := bufio.NewWriter(f) | ||
| w.WriteString("Starting logging goroutine\n") | ||
| w.Flush() | ||
|
|
||
| go func() { | ||
| if err := fn(ctx, config, ready); err != nil { | ||
| w.WriteString("Binary exited with error. sending error via channel\n") | ||
| w.Flush() | ||
| errCh <- err | ||
| return | ||
| } | ||
| w.WriteString("Binary exited normally.\n") | ||
| w.Flush() | ||
| errCh <- nil | ||
| }() | ||
|
|
||
| w.WriteString("Started logging goroutine\n") | ||
| w.Flush() | ||
|
|
||
| for { | ||
| select { | ||
| case err := <-errCh: | ||
| w.WriteString(fmt.Sprintf("Received from error channel: %s\n", err)) | ||
| w.Flush() | ||
| if err != nil { | ||
| fmt.Fprintln(os.Stderr, err) | ||
| os.Exit(1) | ||
| } | ||
| os.Exit(0) | ||
| default: | ||
| w.WriteString("Nothing received, sleeping for 500ms\n") | ||
| w.Flush() | ||
| time.Sleep(500 * time.Millisecond) | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.