Skip to content

Commit

Permalink
cli: log daemon output to file
Browse files Browse the repository at this point in the history
This stores the daemon output in os.UserCacheDir()+"/encore/daemon.log"
for easier post-hoc debugging of a daemon crash.
  • Loading branch information
eandre committed May 3, 2021
1 parent c2ce458 commit e98210f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The environment variables are:
`ENCORE_RUNTIME_PATH` can be set to location of the `compiler/runtime` package in this repository,
while `ENCORE_GOROOT` must be pointed to where `encore-go` was built.

For more information on this see [cli/internal/env/env.go](cli/internal/env/env.go).
For more information on this see [cli/daemon/internal/env/env.go](cli/daemon/internal/env/env.go).

## Architecture

Expand Down
22 changes: 22 additions & 0 deletions cli/cmd/encore/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import (

// Main runs the daemon.
func Main() {
if err := redirectLogOutput(); err != nil {
log.Error().Err(err).Msg("could not setup daemon log file, skipping")
}
if err := runMain(); err != nil {
log.Fatal().Err(err).Msg("daemon failed")
}
Expand Down Expand Up @@ -299,3 +302,22 @@ func wrap(ctx context.Context) context.Context {
"goarch", stdruntime.GOARCH,
)
}

// redirectLogOutput redirects the global logger to also write to a file.
func redirectLogOutput() error {
cache, err := os.UserCacheDir()
if err != nil {
return err
}
logPath := filepath.Join(cache, "encore", "daemon.log")
if err := os.MkdirAll(filepath.Dir(logPath), 0755); err != nil {
return err
}
f, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return err
}
log.Info().Msgf("writing output to %s", logPath)
log.Logger = log.Output(io.MultiWriter(zerolog.ConsoleWriter{Out: os.Stderr}, f))
return nil
}
2 changes: 1 addition & 1 deletion cli/daemon/dash/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"strings"
"time"

"encr.dev/cli/daemon/internal/env"
"encr.dev/cli/daemon/runtime/trace"
"encr.dev/cli/internal/dedent"
"encr.dev/cli/internal/env"
tracepb "encr.dev/proto/encore/engine/trace"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
Expand Down
9 changes: 4 additions & 5 deletions cli/internal/env/env.go → cli/daemon/internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
package env

import (
"fmt"
"os"
"path/filepath"

"github.com/rs/zerolog/log"
)

// EncoreRuntimePath reports the path to the Encore runtime.
Expand All @@ -15,9 +16,8 @@ func EncoreRuntimePath() string {
}
root, ok := determineRoot()
if !ok {
fmt.Fprintln(os.Stderr, "fatal: could not determine Encore install root.\n"+
log.Fatal().Msg("could not determine Encore install root. " +
"You can specify the path to the Encore runtime manually by setting the ENCORE_RUNTIME_PATH environment variable.")
os.Exit(1)
}
return filepath.Join(root, "runtime")
}
Expand All @@ -30,9 +30,8 @@ func EncoreGoRoot() string {
}
root, ok := determineRoot()
if !ok {
fmt.Fprintln(os.Stderr, "fatal: could not determine Encore install root.\n"+
log.Fatal().Msg("could not determine Encore install root. " +
"You can specify the path to the Encore GOROOT manually by setting the ENCORE_GOROOT environment variable.")
os.Exit(1)
}
return filepath.Join(root, "encore-go")
}
Expand Down
2 changes: 1 addition & 1 deletion cli/daemon/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"golang.org/x/mod/modfile"

"encr.dev/cli/daemon/internal/appfile"
"encr.dev/cli/daemon/internal/env"
"encr.dev/cli/daemon/internal/sym"
"encr.dev/cli/internal/env"
"encr.dev/cli/internal/xos"
"encr.dev/compiler"
"encr.dev/parser"
Expand Down
2 changes: 1 addition & 1 deletion cli/daemon/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
"testing"

"encr.dev/cli/internal/env"
"encr.dev/cli/daemon/internal/env"
"encr.dev/compiler"
qt "github.com/frankban/quicktest"
"go.uber.org/goleak"
Expand Down
2 changes: 1 addition & 1 deletion cli/daemon/run/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"

"encr.dev/cli/daemon/internal/appfile"
"encr.dev/cli/internal/env"
"encr.dev/cli/daemon/internal/env"
"encr.dev/compiler"
"encr.dev/parser"
)
Expand Down

0 comments on commit e98210f

Please sign in to comment.