wings/loggers/cli/cli.go
Ethan Alicea 4390bad36b
Please enter the commit message for your changes. Lines starting
with '#' will be ignored, and an empty message aborts the commit.

 Author:    Ethan Alicea <64653625+Tech-Gamer@users.noreply.github.com>

 On branch develop
 Your branch is up to date with 'origin/develop'.

 Changes to be committed:
	modified:   .github/workflows/push.yaml
	modified:   .github/workflows/release.yaml
	modified:   CHANGELOG.md
	modified:   Dockerfile
	modified:   Makefile
	modified:   README.md
	modified:   cmd/configure.go
	modified:   cmd/diagnostics.go
	modified:   cmd/root.go
	modified:   config/config.go
	modified:   environment/allocations.go
	modified:   environment/docker.go
	modified:   environment/docker/api.go
	modified:   environment/docker/container.go
	modified:   environment/docker/environment.go
	modified:   environment/docker/power.go
	modified:   environment/docker/stats.go
	modified:   environment/environment.go
	modified:   environment/settings.go
	modified:   events/events.go
	modified:   go.mod
	modified:   internal/cron/activity_cron.go
	modified:   internal/cron/cron.go
	modified:   internal/cron/sftp_cron.go
	modified:   internal/database/database.go
	modified:   internal/progress/progress.go
	modified:   internal/progress/progress_test.go
	modified:   loggers/cli/cli.go
	new file:   oryxBuildBinary
	modified:   parser/parser.go
	modified:   remote/http.go
	modified:   remote/servers.go
	modified:   remote/types.go
	modified:   router/downloader/downloader.go
	modified:   router/middleware.go
	modified:   router/middleware/middleware.go
	modified:   router/middleware/request_error.go
	modified:   router/router.go
	modified:   router/router_download.go
	modified:   router/router_server.go
	modified:   router/router_server_backup.go
	modified:   router/router_server_files.go
	modified:   router/router_server_transfer.go
	modified:   router/router_server_ws.go
	modified:   router/router_system.go
	modified:   router/router_transfer.go
	modified:   router/tokens/parser.go
	modified:   router/websocket/listeners.go
	modified:   router/websocket/websocket.go
	modified:   server/activity.go
	modified:   server/backup.go
	modified:   server/backup/backup.go
	modified:   server/backup/backup_local.go
	modified:   server/backup/backup_s3.go
	modified:   server/configuration.go
	modified:   server/console.go
	modified:   server/crash.go
	modified:   server/events.go
	modified:   server/filesystem/archive.go
	modified:   server/filesystem/filesystem.go
	modified:   server/filesystem/filesystem_test.go
	modified:   server/install.go
	modified:   server/installer/installer.go
	modified:   server/listeners.go
	modified:   server/manager.go
	modified:   server/mounts.go
	modified:   server/power.go
	modified:   server/power_test.go
	modified:   server/resources.go
	modified:   server/server.go
	modified:   server/transfer/archive.go
	modified:   server/transfer/source.go
	modified:   server/transfer/transfer.go
	modified:   server/update.go
	modified:   sftp/event.go
	modified:   sftp/handler.go
	modified:   sftp/server.go
	modified:   wings.go
2023-09-11 17:22:09 +00:00

133 lines
3.2 KiB
Go

package cli
import (
"fmt"
"io"
"os"
"strings"
"sync"
"time"
"emperror.dev/errors"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
color2 "github.com/fatih/color"
"github.com/mattn/go-colorable"
)
var (
Default = New(os.Stderr, true)
bold = color2.New(color2.Bold)
boldred = color2.New(color2.Bold, color2.FgRed)
)
var Strings = [...]string{
log.DebugLevel: "DEBUG",
log.InfoLevel: " INFO",
log.WarnLevel: " WARN",
log.ErrorLevel: "ERROR",
log.FatalLevel: "FATAL",
}
type Handler struct {
mu sync.Mutex
Writer io.Writer
Padding int
}
func New(w io.Writer, useColors bool) *Handler {
if f, ok := w.(*os.File); ok {
if useColors {
return &Handler{Writer: colorable.NewColorable(f), Padding: 2}
}
}
return &Handler{Writer: colorable.NewNonColorable(w), Padding: 2}
}
// HandleLog implements log.Handler.
func (h *Handler) HandleLog(e *log.Entry) error {
color := cli.Colors[e.Level]
level := Strings[e.Level]
names := e.Fields.Names()
h.mu.Lock()
defer h.mu.Unlock()
color.Fprintf(h.Writer, "%s: [%s] %-25s", bold.Sprintf("%*s", h.Padding+1, level), time.Now().Format(time.StampMilli), e.Message)
for _, name := range names {
if name == "source" {
continue
}
fmt.Fprintf(h.Writer, " %s=%v", color.Sprint(name), e.Fields.Get(name))
}
fmt.Fprintln(h.Writer)
for _, name := range names {
if name != "error" {
continue
}
if err, ok := e.Fields.Get("error").(error); ok {
// Attach the stacktrace if it is missing at this point, but don't point
// it specifically to this line since that is irrelevant.
err = errors.WithStackDepthIf(err, 4)
formatted := fmt.Sprintf("\n%s\n%+v\n\n", boldred.Sprintf("Stacktrace:"), err)
if !strings.Contains(formatted, "runtime.goexit") {
_, _ = fmt.Fprint(h.Writer, formatted)
break
}
// Inserts a new-line between sections of a stack.
// When wrapping errors, you get multiple separate stacks that start with their message,
// this allows us to separate them with a new-line and view them more easily.
//
// For example:
//
// Stacktrace:
// readlink test: no such file or directory
// failed to read symlink target for 'test'
// github.com/Tech-Gamer/nwy-wings/server/filesystem.(*Archive).addToArchive
// github.com/Tech-Gamer/nwy-wings/server/filesystem/archive.go:166
// ... (Truncated the stack for easier reading)
// runtime.goexit
// runtime/asm_amd64.s:1374
// **NEW LINE INSERTED HERE**
// backup: error while generating server backup
// github.com/Tech-Gamer/nwy-wings/server.(*Server).Backup
// github.com/Tech-Gamer/nwy-wings/server/backup.go:84
// ... (Truncated the stack for easier reading)
// runtime.goexit
// runtime/asm_amd64.s:1374
//
var b strings.Builder
var endOfStack bool
for _, s := range strings.Split(formatted, "\n") {
b.WriteString(s + "\n")
if s == "runtime.goexit" {
endOfStack = true
continue
}
if !endOfStack {
continue
}
b.WriteString("\n")
endOfStack = false
}
_, _ = fmt.Fprint(h.Writer, b.String())
}
// Only one key with the name "error" can be in the map.
break
}
return nil
}