mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-03-05 15:15:49 +02:00
🎨 split out platform-dependent logging for compile-time selection
This commit is contained in:
parent
2b4048ebff
commit
2ac33bb83d
105
pkg/app/app.go
105
pkg/app/app.go
@ -4,14 +4,12 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/aybabtme/humanlog"
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands"
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||||
"github.com/jesseduffield/lazygit/pkg/config"
|
"github.com/jesseduffield/lazygit/pkg/config"
|
||||||
"github.com/jesseduffield/lazygit/pkg/env"
|
"github.com/jesseduffield/lazygit/pkg/env"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui"
|
"github.com/jesseduffield/lazygit/pkg/gui"
|
||||||
"github.com/jesseduffield/lazygit/pkg/i18n"
|
"github.com/jesseduffield/lazygit/pkg/i18n"
|
||||||
"github.com/jesseduffield/lazygit/pkg/secureexec"
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/updates"
|
"github.com/jesseduffield/lazygit/pkg/updates"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"io"
|
"io"
|
||||||
@ -20,10 +18,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// App struct
|
// App struct
|
||||||
@ -310,104 +306,3 @@ func (app *App) KnownError(err error) (string, bool) {
|
|||||||
}
|
}
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
func TailLogs() {
|
|
||||||
logFilePath, err := config.LogPath()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Tailing log file %s\n\n", logFilePath)
|
|
||||||
|
|
||||||
opts := humanlog.DefaultOptions
|
|
||||||
opts.Truncates = false
|
|
||||||
|
|
||||||
_, 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
TailLogsNative(logFilePath, opts)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := secureexec.Command("tail", "-f", logFilePath)
|
|
||||||
|
|
||||||
stdout, _ := cmd.StdoutPipe()
|
|
||||||
if err := cmd.Start(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TailLogsNative(logFilePath string, opts *humanlog.HandlerOptions) {
|
|
||||||
var lastModified int64 = 0
|
|
||||||
var lastOffset int64 = 0
|
|
||||||
for {
|
|
||||||
stat, err := os.Stat(logFilePath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if stat.ModTime().Unix() > lastModified {
|
|
||||||
err = TailFrom(lastOffset, logFilePath, opts)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lastOffset = stat.Size()
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func OpenAndSeek(filepath string, offset int64) (*os.File, error) {
|
|
||||||
file, err := os.Open(filepath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = file.Seek(offset, 0)
|
|
||||||
if err != nil {
|
|
||||||
_ = file.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return file, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TailFrom(lastOffset int64, logFilePath string, opts *humanlog.HandlerOptions) error {
|
|
||||||
file, err := OpenAndSeek(logFilePath, lastOffset)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fileScanner := bufio.NewScanner(file)
|
|
||||||
var lines []string
|
|
||||||
for fileScanner.Scan() {
|
|
||||||
lines = append(lines, fileScanner.Text())
|
|
||||||
}
|
|
||||||
file.Close()
|
|
||||||
lineCount := len(lines)
|
|
||||||
lastTen := lines
|
|
||||||
if lineCount > 10 {
|
|
||||||
lastTen = lines[lineCount-10:]
|
|
||||||
}
|
|
||||||
for _, line := range lastTen {
|
|
||||||
reader := strings.NewReader(line)
|
|
||||||
if err := humanlog.Scanner(reader, os.Stdout, opts); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
49
pkg/app/logging.go
Normal file
49
pkg/app/logging.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/aybabtme/humanlog"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/config"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/secureexec"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TailLogs1() {
|
||||||
|
logFilePath, err := config.LogPath()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Tailing log file %s\n\n", logFilePath)
|
||||||
|
|
||||||
|
opts := humanlog.DefaultOptions
|
||||||
|
opts.Truncates = false
|
||||||
|
|
||||||
|
_, 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 := secureexec.Command("tail", "-f", logFilePath)
|
||||||
|
|
||||||
|
stdout, _ := cmd.StdoutPipe()
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
95
pkg/app/logging_windows.go
Normal file
95
pkg/app/logging_windows.go
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"github.com/aybabtme/humanlog"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/config"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TailLogs() {
|
||||||
|
logFilePath, err := config.LogPath()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Tailing log file %s\n\n", logFilePath)
|
||||||
|
|
||||||
|
opts := humanlog.DefaultOptions
|
||||||
|
opts.Truncates = false
|
||||||
|
|
||||||
|
_, 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
TailLogsNative(logFilePath, opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TailLogsNative(logFilePath string, opts *humanlog.HandlerOptions) {
|
||||||
|
var lastModified int64 = 0
|
||||||
|
var lastOffset int64 = 0
|
||||||
|
for {
|
||||||
|
stat, err := os.Stat(logFilePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if stat.ModTime().Unix() > lastModified {
|
||||||
|
err = TailFrom(lastOffset, logFilePath, opts)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastOffset = stat.Size()
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpenAndSeek(filepath string, offset int64) (*os.File, error) {
|
||||||
|
file, err := os.Open(filepath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = file.Seek(offset, 0)
|
||||||
|
if err != nil {
|
||||||
|
_ = file.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return file, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TailFrom(lastOffset int64, logFilePath string, opts *humanlog.HandlerOptions) error {
|
||||||
|
file, err := OpenAndSeek(logFilePath, lastOffset)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fileScanner := bufio.NewScanner(file)
|
||||||
|
var lines []string
|
||||||
|
for fileScanner.Scan() {
|
||||||
|
lines = append(lines, fileScanner.Text())
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
lineCount := len(lines)
|
||||||
|
lastTen := lines
|
||||||
|
if lineCount > 10 {
|
||||||
|
lastTen = lines[lineCount-10:]
|
||||||
|
}
|
||||||
|
for _, line := range lastTen {
|
||||||
|
reader := strings.NewReader(line)
|
||||||
|
if err := humanlog.Scanner(reader, os.Stdout, opts); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user