Skip to content

Commit

Permalink
feat(log): initialize debug logger
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 7f3e304 commit f819896
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 30 deletions.
1 change: 1 addition & 0 deletions cmd/soft/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"runtime/debug"

"github.com/charmbracelet/log"
_ "github.com/charmbracelet/soft-serve/log"
"github.com/spf13/cobra"
)

Expand Down
5 changes: 0 additions & 5 deletions cmd/soft/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"syscall"
"time"

"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/server"
"github.com/charmbracelet/soft-serve/server/config"
"github.com/spf13/cobra"
Expand All @@ -26,10 +25,6 @@ var (
return err
}

if cfg.Debug {
log.SetLevel(log.DebugLevel)
}

done := make(chan os.Signal, 1)
lch := make(chan error, 1)
go func() {
Expand Down
6 changes: 0 additions & 6 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ type Config struct {
// DataPath is the path to the directory where Soft Serve will store its data.
DataPath string `env:"DATA_PATH" envDefault:"data"`

// Debug enables debug logging.
Debug bool `env:"DEBUG" envDefault:"false"`

// Backend is the Git backend to use.
Backend backend.Backend

Expand All @@ -70,9 +67,6 @@ func DefaultConfig() *Config {
}); err != nil {
log.Fatal(err)
}
if cfg.Debug {
log.SetLevel(log.DebugLevel)
}
fb, err := file.NewFileBackend(cfg.DataPath)
if err != nil {
log.Fatal(err)
Expand Down
5 changes: 2 additions & 3 deletions server/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func NewSSHServer(cfg *config.Config) (*SSHServer, error) {
cm.Middleware(cfg),
// Git middleware.
s.Middleware(cfg),
// Logging middleware.
lm.MiddlewareWithLogger(logger),
),
}
Expand All @@ -66,9 +67,7 @@ func NewSSHServer(cfg *config.Config) (*SSHServer, error) {

// PublicKeyAuthHandler handles public key authentication.
func (s *SSHServer) PublicKeyHandler(ctx ssh.Context, pk ssh.PublicKey) bool {
al := s.cfg.Access.AccessLevel("", pk)
logger.Debug("publickey handler", "level", al)
return al > backend.NoAccess
return s.cfg.Access.AccessLevel("", pk) > backend.NoAccess
}

// KeyboardInteractiveHandler handles keyboard interactive authentication.
Expand Down
13 changes: 6 additions & 7 deletions ui/pages/repo/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package repo

import (
"fmt"
"log"
"strings"
"time"

Expand Down Expand Up @@ -386,7 +385,7 @@ func (l *Log) StatusBarInfo() string {

func (l *Log) countCommitsCmd() tea.Msg {
if l.ref == nil {
log.Printf("ui: log: ref is nil")
logger.Debugf("ui: log: ref is nil")
return common.ErrorMsg(errNoRef)
}
r, err := l.repo.Repository()
Expand All @@ -395,7 +394,7 @@ func (l *Log) countCommitsCmd() tea.Msg {
}
count, err := r.CountCommits(l.ref)
if err != nil {
log.Printf("ui: error counting commits: %v", err)
logger.Debugf("ui: error counting commits: %v", err)
return common.ErrorMsg(err)
}
return LogCountMsg(count)
Expand All @@ -412,7 +411,7 @@ func (l *Log) updateCommitsCmd() tea.Msg {
}
}
if l.ref == nil {
log.Printf("ui: log: ref is nil")
logger.Debugf("ui: log: ref is nil")
return common.ErrorMsg(errNoRef)
}
items := make([]selector.IdentifiableItem, count)
Expand All @@ -426,7 +425,7 @@ func (l *Log) updateCommitsCmd() tea.Msg {
// CommitsByPage pages start at 1
cc, err := r.CommitsByPage(l.ref, page+1, limit)
if err != nil {
log.Printf("ui: error loading commits: %v", err)
logger.Debugf("ui: error loading commits: %v", err)
return common.ErrorMsg(err)
}
for i, c := range cc {
Expand All @@ -448,12 +447,12 @@ func (l *Log) selectCommitCmd(commit *git.Commit) tea.Cmd {
func (l *Log) loadDiffCmd() tea.Msg {
r, err := l.repo.Repository()
if err != nil {
log.Printf("ui: error loading diff repository: %v", err)
logger.Debugf("ui: error loading diff repository: %v", err)
return common.ErrorMsg(err)
}
diff, err := r.Diff(l.selectedCommit)
if err != nil {
log.Printf("ui: error loading diff: %v", err)
logger.Debugf("ui: error loading diff: %v", err)
return common.ErrorMsg(err)
}
return LogDiffMsg(diff)
Expand Down
7 changes: 3 additions & 4 deletions ui/pages/repo/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repo
import (
"errors"
"fmt"
"log"
"sort"
"strings"

Expand Down Expand Up @@ -182,7 +181,7 @@ func (r *Refs) updateItemsCmd() tea.Msg {
}
refs, err := rr.References()
if err != nil {
log.Printf("ui: error getting references: %v", err)
logger.Debugf("ui: error getting references: %v", err)
return common.ErrorMsg(err)
}
for _, ref := range refs {
Expand Down Expand Up @@ -228,10 +227,10 @@ func UpdateRefCmd(repo backend.Repository) tea.Cmd {
if bs, err := r.Branches(); err != nil && len(bs) == 0 {
return EmptyRepoMsg{}
}
log.Printf("ui: error getting HEAD reference: %v", err)
logger.Debugf("ui: error getting HEAD reference: %v", err)
return common.ErrorMsg(err)
}
log.Printf("HEAD: %s", ref.Name())
logger.Debugf("HEAD: %s", ref.Name())
return RefMsg(ref)
}
}
5 changes: 5 additions & 0 deletions ui/pages/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/git"
"github.com/charmbracelet/soft-serve/server/backend"
"github.com/charmbracelet/soft-serve/ui/common"
Expand All @@ -17,6 +18,10 @@ import (
"github.com/charmbracelet/soft-serve/ui/components/tabs"
)

var (
logger = log.WithPrefix("ui.repo")
)

type state int

const (
Expand Down
8 changes: 6 additions & 2 deletions ui/pages/selection/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ package selection

import (
"fmt"
"log"
"sort"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/ui/common"
"github.com/charmbracelet/soft-serve/ui/components/code"
"github.com/charmbracelet/soft-serve/ui/components/selector"
"github.com/charmbracelet/soft-serve/ui/components/tabs"
)

var (
logger = log.WithPrefix("ui.selection")
)

type pane int

const (
Expand Down Expand Up @@ -191,7 +195,7 @@ func (s *Selection) Init() tea.Cmd {
for _, r := range repos {
item, err := NewItem(r, cfg)
if err != nil {
log.Printf("ui: failed to create item for %s: %v", r.Name(), err)
logger.Debugf("ui: failed to create item for %s: %v", r.Name(), err)
continue
}
sortedItems = append(sortedItems, item)
Expand Down
10 changes: 7 additions & 3 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package ui

import (
"errors"
"log"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/server/backend"
"github.com/charmbracelet/soft-serve/ui/common"
"github.com/charmbracelet/soft-serve/ui/components/footer"
Expand All @@ -17,6 +17,10 @@ import (
"github.com/charmbracelet/soft-serve/ui/pages/selection"
)

var (
logger = log.WithPrefix("ui")
)

type page int

const (
Expand Down Expand Up @@ -164,7 +168,7 @@ func (ui *UI) IsFiltering() bool {

// Update implements tea.Model.
func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
log.Printf("msg received: %T", msg)
logger.Debugf("msg received: %T", msg)
cmds := make([]tea.Cmd, 0)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
Expand Down Expand Up @@ -294,7 +298,7 @@ func (ui *UI) openRepo(rn string) (backend.Repository, error) {
}
repos, err := cfg.Backend.Repositories()
if err != nil {
log.Printf("ui: failed to list repos: %v", err)
logger.Debugf("ui: failed to list repos: %v", err)
return nil, err
}
for _, r := range repos {
Expand Down

0 comments on commit f819896

Please sign in to comment.