Skip to content

Commit

Permalink
Improve internal logging infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Sep 15, 2021
1 parent cbd3b35 commit 188bae9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
33 changes: 33 additions & 0 deletions internal/chezmoi/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package chezmoi
import (
"io/fs"
"strings"

"github.com/rs/zerolog"
)

// A SourceFileTargetType is a the type of a target represented by a file in the
Expand All @@ -20,6 +22,15 @@ const (
SourceFileTypeSymlink
)

var sourceFileTypeStrs = map[SourceFileTargetType]string{
SourceFileTypeCreate: "create",
SourceFileTypeFile: "file",
SourceFileTypeModify: "modify",
SourceFileTypeRemove: "remove",
SourceFileTypeScript: "script",
SourceFileTypeSymlink: "symlink",
}

// DirAttr holds attributes parsed from a source directory name.
type DirAttr struct {
TargetName string
Expand Down Expand Up @@ -76,6 +87,14 @@ func parseDirAttr(sourceName string) DirAttr {
}
}

// MarshalZerologObject implements zerolog.Marshaler.
func (da DirAttr) MarshalZerologObject(e *zerolog.Event) {
e.Str("targetName", da.TargetName)
e.Bool("exact", da.Exact)
e.Bool("private", da.Private)
e.Bool("readOnly", da.ReadOnly)
}

// SourceName returns da's source name.
func (da DirAttr) SourceName() string {
sourceName := ""
Expand Down Expand Up @@ -236,6 +255,20 @@ func parseFileAttr(sourceName, encryptedSuffix string) FileAttr {
}
}

// MarshalZerologObject implements zerolog.ObjectMarshaler.
func (fa FileAttr) MarshalZerologObject(e *zerolog.Event) {
e.Str("TargetName", fa.TargetName)
e.Str("Type", sourceFileTypeStrs[fa.Type])
e.Bool("Empty", fa.Empty)
e.Bool("Encrypted", fa.Encrypted)
e.Bool("Executable", fa.Executable)
e.Bool("Once", fa.Once)
e.Int("Order", fa.Order)
e.Bool("Private", fa.Private)
e.Bool("ReadOnly", fa.ReadOnly)
e.Bool("Template", fa.Template)
}

// SourceName returns fa's source name.
func (fa FileAttr) SourceName(encryptedSuffix string) string {
sourceName := ""
Expand Down
3 changes: 3 additions & 0 deletions internal/chezmoi/entrystate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (s *EntryState) Overwrite() bool {
}

func (s *EntryState) MarshalZerologObject(e *zerolog.Event) {
if s == nil {
return
}
e.Str("Type", string(s.Type))
e.Int("Mode", int(s.Mode))
e.Stringer("ContentsSHA256", s.ContentsSHA256)
Expand Down
43 changes: 43 additions & 0 deletions internal/chezmoi/sourcestateentry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package chezmoi

import (
"encoding/hex"

"github.com/rs/zerolog"

"github.com/twpayne/chezmoi/v2/internal/chezmoilog"
)

// A SourceStateEntry represents the state of an entry in the source state.
type SourceStateEntry interface {
zerolog.LogObjectMarshaler
Evaluate() error
Order() int
SourceRelPath() SourceRelPath
Expand Down Expand Up @@ -42,6 +51,12 @@ func (s *SourceStateDir) Evaluate() error {
return nil
}

// MarshalZerologObject implements zerolog.LogObjectMarshaler.
func (s *SourceStateDir) MarshalZerologObject(e *zerolog.Event) {
e.Stringer("sourceRelPath", s.sourceRelPath)
e.Object("attr", s.Attr)
}

// Order returns s's order.
func (s *SourceStateDir) Order() int {
return 0
Expand All @@ -63,6 +78,23 @@ func (s *SourceStateFile) Evaluate() error {
return err
}

// MarshalZerologObject implements zerolog.LogObjectMarshaler.
func (s *SourceStateFile) MarshalZerologObject(e *zerolog.Event) {
e.Stringer("sourceRelPath", s.sourceRelPath)
e.Interface("attr", s.Attr)
contents, contentsErr := s.Contents()
e.Bytes("contents", chezmoilog.FirstFewBytes(contents))
if contentsErr != nil {
e.Str("contentsErr", contentsErr.Error())
}
e.Err(contentsErr)
contentsSHA256, contentsSHA256Err := s.ContentsSHA256()
e.Str("contentsSHA256", hex.EncodeToString(contentsSHA256))
if contentsSHA256Err != nil {
e.Str("contentsSHA256Err", contentsSHA256Err.Error())
}
}

// Order returns s's order.
func (s *SourceStateFile) Order() int {
return s.Attr.Order
Expand All @@ -87,6 +119,11 @@ func (s *SourceStateRemove) Evaluate() error {
return nil
}

// MarshalZerologObject implements zerolog.LogObjectMarshaler.
func (s *SourceStateRemove) MarshalZerologObject(e *zerolog.Event) {
e.Stringer("targetRelPath", s.targetRelPath)
}

// Order returns s's order.
func (s *SourceStateRemove) Order() int {
return 0
Expand All @@ -107,6 +144,12 @@ func (s *SourceStateRenameDir) Evaluate() error {
return nil
}

// MarashalLogObject implements zerolog.LogObjectMarshaler.
func (s *SourceStateRenameDir) MarshalZerologObject(e *zerolog.Event) {
e.Stringer("oldSourceRelPath", s.oldSourceRelPath)
e.Stringer("newSourceRelPath", s.newSourceRelPath)
}

// Order returns s's order.
func (s *SourceStateRenameDir) Order() int {
return -1
Expand Down

0 comments on commit 188bae9

Please sign in to comment.