diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-26 10:23:10 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-26 11:00:50 +1000 |
commit | 077f11361805417c15234c62a9f9aa022f913d43 (patch) | |
tree | 2cf25beba4f96b55392f09d733a14c25c149de07 /pkg | |
parent | 0c6cbe7746660155df0645f01d37150099d21f49 (diff) |
add in-built logging support for a better dev experience
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/app/app.go | 47 | ||||
-rw-r--r-- | pkg/commands/commit_list_builder.go | 2 | ||||
-rw-r--r-- | pkg/commands/git.go | 4 | ||||
-rw-r--r-- | pkg/config/app_config.go | 17 | ||||
-rw-r--r-- | pkg/gui/app_status_manager.go | 1 | ||||
-rw-r--r-- | pkg/gui/cherry_picking.go | 2 | ||||
-rw-r--r-- | pkg/gui/context.go | 1 | ||||
-rw-r--r-- | pkg/gui/file_watching.go | 7 | ||||
-rw-r--r-- | pkg/gui/gui.go | 2 | ||||
-rw-r--r-- | pkg/gui/layout.go | 2 | ||||
-rw-r--r-- | pkg/gui/list_context.go | 1 | ||||
-rw-r--r-- | pkg/gui/merge_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/view_helpers.go | 47 | ||||
-rw-r--r-- | pkg/i18n/dutch.go | 3 | ||||
-rw-r--r-- | pkg/i18n/english.go | 3 | ||||
-rw-r--r-- | pkg/i18n/polish.go | 3 | ||||
-rw-r--r-- | pkg/tasks/tasks.go | 18 |
17 files changed, 110 insertions, 52 deletions
diff --git a/pkg/app/app.go b/pkg/app/app.go index e9ba81f59..31144cc11 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -6,18 +6,20 @@ import ( "fmt" "io" "io/ioutil" + "log" "os" + "os/exec" "path/filepath" "regexp" "strconv" "strings" + "github.com/aybabtme/humanlog" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/gui" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/updates" - "github.com/shibukawa/configdir" "github.com/sirupsen/logrus" ) @@ -47,12 +49,6 @@ func newProductionLogger(config config.AppConfigurer) *logrus.Logger { return log } -func globalConfigDir() string { - configDirs := configdir.New("jesseduffield", "lazygit") - configDir := configDirs.QueryFolders(configdir.Global)[0] - return configDir.Path -} - func getLogLevel() logrus.Level { strLevel := os.Getenv("LOG_LEVEL") level, err := logrus.ParseLevel(strLevel) @@ -62,10 +58,10 @@ func getLogLevel() logrus.Level { return level } -func newDevelopmentLogger(config config.AppConfigurer) *logrus.Logger { +func newDevelopmentLogger(configurer config.AppConfigurer) *logrus.Logger { log := logrus.New() log.SetLevel(getLogLevel()) - file, err := os.OpenFile(filepath.Join(globalConfigDir(), "development.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + file, err := os.OpenFile(config.LogPath(), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { panic("unable to log to file") // TODO: don't panic (also, remove this call to the `panic` function) } @@ -281,3 +277,36 @@ func (app *App) KnownError(err error) (string, bool) { } return "", false } + +func TailLogs() { + logFilePath := config.LogPath() + + fmt.Printf("Tailing log file %s\n\n", logFilePath) + + _, err := os.Stat(logFilePath) + if err != nil { + if os.IsNotExist(err) { + log.Fatal("Log file does not exist. Run `lazygit --debug` first to create the log file") + } + log.Fatal(err) + } + + cmd := exec.Command("tail", "-f", logFilePath) + + stdout, _ := cmd.StdoutPipe() + if err := cmd.Start(); err != nil { + log.Fatal(err) + } + + opts := humanlog.DefaultOptions + opts.Truncates = false + if err := humanlog.Scanner(stdout, os.Stdout, opts); err != nil { + log.Fatal(err) + } + + if err := cmd.Wait(); err != nil { + log.Fatal(err) + } + + os.Exit(0) +} diff --git a/pkg/commands/commit_list_builder.go b/pkg/commands/commit_list_builder.go index ccf253a54..ec988ec62 100644 --- a/pkg/commands/commit_list_builder.go +++ b/pkg/commands/commit_list_builder.go @@ -248,7 +248,7 @@ func (c *CommitListBuilder) getNormalRebasingCommits() ([]*Commit, error) { func (c *CommitListBuilder) getInteractiveRebasingCommits() ([]*Commit, error) { bytesContent, err := ioutil.ReadFile(fmt.Sprintf("%s/rebase-merge/git-rebase-todo", c.GitCommand.DotGitDir)) if err != nil { - c.Log.Info(fmt.Sprintf("error occurred reading git-rebase-todo: %s", err.Error())) + c.Log.Error(fmt.Sprintf("error occurred reading git-rebase-todo: %s", err.Error())) // we assume an error means the file doesn't exist so we just return return nil, nil } diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 3f671b9ff..7b37968ed 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -246,7 +246,7 @@ func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File { for _, statusString := range statusStrings { if strings.HasPrefix(statusString, "warning") { - c.Log.Warning(statusString) + c.Log.Warningf("warning when calling git status: %s", statusString) continue } change := statusString[0:2] @@ -792,8 +792,8 @@ func (c *GitCommand) WorktreeFileDiffCmdStr(file *File, plain bool, cached bool) } func (c *GitCommand) ApplyPatch(patch string, flags ...string) error { - c.Log.Warn(patch) filepath := filepath.Join(c.Config.GetUserConfigDir(), utils.GetCurrentRepoName(), time.Now().Format("Jan _2 15.04.05.000000000")+".patch") + c.Log.Infof("saving temporary patch to %s", filepath) if err := c.OSCommand.CreateFileWithContent(filepath, patch); err != nil { return err } diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index c79907c64..5df706ea0 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -410,11 +410,12 @@ func getDefaultAppState() []byte { `) } -// // commenting this out until we use it again -// func homeDirectory() string { -// usr, err := user.Current() -// if err != nil { -// log.Fatal(err) -// } -// return usr.HomeDir -// } +func globalConfigDir() string { + configDirs := configdir.New("jesseduffield", "lazygit") + configDir := configDirs.QueryFolders(configdir.Global)[0] + return configDir.Path +} + +func LogPath() string { + return filepath.Join(globalConfigDir(), "development.log") +} diff --git a/pkg/gui/app_status_manager.go b/pkg/gui/app_status_manager.go index 9d8ac3725..39f2940e2 100644 --- a/pkg/gui/app_status_manager.go +++ b/pkg/gui/app_status_manager.go @@ -62,7 +62,6 @@ func (gui *Gui) WithWaitingStatus(name string, f func() error) error { defer ticker.Stop() for range ticker.C { appStatus := gui.statusManager.getStatusString() - gui.Log.Warn(appStatus) if appStatus == "" { return } diff --git a/pkg/gui/cherry_picking.go b/pkg/gui/cherry_picking.go index 04e7517b7..4b853d637 100644 --- a/pkg/gui/cherry_picking.go +++ b/pkg/gui/cherry_picking.go @@ -182,7 +182,7 @@ func (gui *Gui) rerenderContextViewIfPresent(contextKey string) error { view, err := gui.g.View(viewName) if err != nil { - gui.Log.Warn(err) + gui.Log.Error(err) return nil } diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 937106953..a7b887199 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -610,7 +610,6 @@ func (gui *Gui) onViewFocusLost(v *gocui.View, newView *gocui.View) error { } } - gui.Log.Info(v.Name() + " focus lost") return nil } diff --git a/pkg/gui/file_watching.go b/pkg/gui/file_watching.go index 7e5e20a76..81a0c97fc 100644 --- a/pkg/gui/file_watching.go +++ b/pkg/gui/file_watching.go @@ -59,15 +59,14 @@ func (w *fileWatcher) popOldestFilename() { w.WatchedFilenames = w.WatchedFilenames[1:] if err := w.Watcher.Remove(oldestFilename); err != nil { // swallowing errors here because it doesn't really matter if we can't unwatch a file - w.Log.Warn(err) + w.Log.Error(err) } } func (w *fileWatcher) watchFilename(filename string) { - w.Log.Warn(filename) if err := w.Watcher.Add(filename); err != nil { // swallowing errors here because it doesn't really matter if we can't watch a file - w.Log.Warn(err) + w.Log.Error(err) } // assume we're watching it now to be safe @@ -138,7 +137,7 @@ func (gui *Gui) watchFilesForChanges() { // watch for errors case err := <-gui.fileWatcher.Watcher.Errors: if err != nil { - gui.Log.Warn(err) + gui.Log.Error(err) } } } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 858bbef5f..db6624ec0 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -450,7 +450,7 @@ func (gui *Gui) Run() error { g.SetManager(gocui.ManagerFunc(gui.layout), gocui.ManagerFunc(gui.getFocusLayout())) - gui.Log.Warn("starting main loop") + gui.Log.Info("starting main loop") err = g.MainLoop() return err diff --git a/pkg/gui/layout.go b/pkg/gui/layout.go index 583e50071..a52f95ba5 100644 --- a/pkg/gui/layout.go +++ b/pkg/gui/layout.go @@ -327,7 +327,7 @@ func (gui *Gui) layout(g *gocui.Gui) error { } // here is a good place log some stuff - // if you download humanlog and do tail -f development.log | humanlog + // if you run `lazygit --logs` // this will let you see these branches as prettified json // gui.Log.Info(utils.AsJson(gui.State.Branches[0:4])) return gui.resizeCurrentPopupPanel() diff --git a/pkg/gui/list_context.go b/pkg/gui/list_context.go index e437337d1..1ba7da80a 100644 --- a/pkg/gui/list_context.go +++ b/pkg/gui/list_context.go @@ -412,7 +412,6 @@ func (gui *Gui) subCommitsListContext() *ListContext { ResetMainViewOriginOnFocus: true, Kind: SIDE_CONTEXT, GetDisplayStrings: func() [][]string { - gui.Log.Warn("getting display strings for sub commits") return presentation.GetCommitListDisplayStrings(gui.State.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Modes.Diffing.Ref) }, SelectedItem: func() (ListItem, bool) { diff --git a/pkg/gui/merge_panel.go b/pkg/gui/merge_panel.go index c19987585..f09b77d2d 100644 --- a/pkg/gui/merge_panel.go +++ b/pkg/gui/merge_panel.go @@ -30,7 +30,6 @@ func (gui *Gui) findConflicts(content string) []commands.Conflict { var newConflict commands.Conflict for i, line := range utils.SplitLines(content) { trimmedLine := strings.TrimPrefix(line, "++") - gui.Log.Info(trimmedLine) if trimmedLine == "<<<<<<< HEAD" || trimmedLine == "<<<<<<< MERGE_HEAD" || trimmedLine == "<<<<<<< Updated upstream" || trimmedLine == "<<<<<<< ours" { newConflict = commands.Conflict{Start: i} } else if trimmedLine == "=======" { @@ -140,7 +139,6 @@ func (gui *Gui) resolveConflict(conflict commands.Conflict, pick string) error { output += line } } - gui.Log.Info(output) return ioutil.WriteFile(gitFile.Name, []byte(output), 0644) } diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 796b7dca6..d7eab73ba 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -27,6 +27,39 @@ const ( STATUS ) +func getScopeNames(scopes []int) []string { + scopeNameMap := map[int]string{ + COMMITS: "commits", + BRANCHES: "branches", + FILES: "files", + STASH: "stash", + REFLOG: "reflog", + TAGS: "tags", + REMOTES: "remotes", + STATUS: "status", + } + + scopeNames := make([]string, len(scopes)) + for i, scope := range scopes { + scopeNames[i] = scopeNameMap[scope] + } + + return scopeNames +} + +func getModeName(mode int) string { + switch mode { + case SYNC: + return "sync" + case ASYNC: + return "async" + case BLOCK_UI: + return "block-ui" + default: + return "unknown mode" + } +} + const ( SYNC = iota // wait until everything is done before returning ASYNC // return immediately, allowing each independent thing to update itself @@ -48,6 +81,19 @@ func intArrToMap(arr []int) map[int]bool { } func (gui *Gui) refreshSidePanels(options refreshOptions) error { + if options.scope == nil { + gui.Log.Infof( + "refreshing all scopes in %s mode", + getModeName(options.mode), + ) + } else { + gui.Log.Infof( + "refreshing the following scopes in %s mode: %s", + getModeName(options.mode), + strings.Join(getScopeNames(options.scope), ","), + ) + } + wg := sync.WaitGroup{} f := func() { @@ -283,7 +329,6 @@ func (gui *Gui) resizePopupPanel(v *gocui.View) error { if vx0 == x0 && vy0 == y0 && vx1 == x1 && vy1 == y1 { return nil } - gui.Log.Info(gui.Tr.SLocalize("resizingPopupPanel")) _, err := gui.g.SetView(v.Name(), x0, y0, x1, y1, 0) return err } diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index 41954c78f..c90017896 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -329,9 +329,6 @@ func addDutch(i18nObject *i18n.Bundle) error { ID: "Error", Other: "Foutmelding", }, &i18n.Message{ - ID: "resizingPopupPanel", - Other: "resizen popup paneel", - }, &i18n.Message{ ID: "RunningSubprocess", Other: "subprocess lopend", }, &i18n.Message{ diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 50b170462..2628c8fab 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -337,9 +337,6 @@ func addEnglish(i18nObject *i18n.Bundle) error { ID: "Error", Other: "Error", }, &i18n.Message{ - ID: "resizingPopupPanel", - Other: "resizing popup panel", - }, &i18n.Message{ ID: "RunningSubprocess", Other: "running subprocess", }, &i18n.Message{ diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index d5e272772..5d1ff7373 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -270,9 +270,6 @@ func addPolish(i18nObject *i18n.Bundle) error { ID: "Error", Other: "Błąd", }, &i18n.Message{ - ID: "resizingPopupPanel", - Other: "skalowanie wyskakującego panelu", - }, &i18n.Message{ ID: "RunningSubprocess", Other: "uruchomiony podproces", }, &i18n.Message{ diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go index 388269de0..223d36c98 100644 --- a/pkg/tasks/tasks.go +++ b/pkg/tasks/tasks.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os/exec" + "strings" "sync" "time" @@ -50,7 +51,9 @@ func (m *ViewBufferManager) NewCmdTask(r io.Reader, cmd *exec.Cmd, linesToRead i go func() { <-stop if err := commands.Kill(cmd); err != nil { - m.Log.Warn(err) + if !strings.Contains(err.Error(), "process already finished") { + m.Log.Errorf("error when running cmd task: %v", err) + } } if onDone != nil { onDone() @@ -120,7 +123,10 @@ func (m *ViewBufferManager) NewCmdTask(r io.Reader, cmd *exec.Cmd, linesToRead i } if err := cmd.Wait(); err != nil { - m.Log.Warn(err) + // it's fine if we've killed this program ourselves + if !strings.Contains(err.Error(), "signal: killed") { + m.Log.Error(err) + } } m.refreshView() @@ -170,15 +176,12 @@ func (m *ViewBufferManager) NewTask(f func(stop chan struct{}) error) error { m.taskIDMutex.Lock() m.newTaskId++ taskID := m.newTaskId - m.Log.Infof("starting task %d", taskID) m.taskIDMutex.Unlock() m.waitingMutex.Lock() defer m.waitingMutex.Unlock() - m.Log.Infof("done waiting") if taskID < m.newTaskId { - m.Log.Infof("returning cos the task is obsolete") return } @@ -186,9 +189,7 @@ func (m *ViewBufferManager) NewTask(f func(stop chan struct{}) error) error { notifyStopped := make(chan struct{}) if m.currentTask != nil { - m.Log.Info("asking task to stop") m.currentTask.Stop() - m.Log.Info("task stopped") } m.currentTask = &Task{ @@ -203,7 +204,6 @@ func (m *ViewBufferManager) NewTask(f func(stop chan struct{}) error) error { m.Log.Error(err) // might need an onError callback } - m.Log.Infof("returning from task %d", taskID) close(notifyStopped) }() }() @@ -218,8 +218,6 @@ func (t *Task) Stop() { return } close(t.stop) - t.Log.Info("closed stop channel, waiting for notifyStopped message") <-t.notifyStopped - t.Log.Info("received notifystopped message") t.stopped = true } |