mirror of
https://github.com/Mikescher/kpsync.git
synced 2025-08-25 08:38:03 +02:00
fifo logs
This commit is contained in:
parent
83d6aa10cb
commit
fb5d408a01
28
TODO.md
28
TODO.md
@ -1,21 +1,21 @@
|
||||
|
||||
|
||||
- log to linux fd (pipe) to open terminal
|
||||
X - log to linux fd (pipe) to open terminal
|
||||
|
||||
- show state in tray
|
||||
X - show state in tray
|
||||
|
||||
- tray menu
|
||||
- force sync
|
||||
- ~ current Etag
|
||||
- ~ current SHA
|
||||
- ~ last sync ts
|
||||
- check sync
|
||||
- quit
|
||||
- show log (open terminal /w log)
|
||||
X - tray menu
|
||||
X - force sync
|
||||
X - ~ current Etag
|
||||
X - ~ current SHA
|
||||
X - ~ last sync ts
|
||||
X - check sync
|
||||
X - quit
|
||||
X - show log (open terminal /w log)
|
||||
|
||||
- config via json + params override
|
||||
X - config via json + params override
|
||||
|
||||
- colorful log
|
||||
- download/upload progress log
|
||||
X - colorful log
|
||||
X - download/upload progress log
|
||||
|
||||
- logfile in workdir
|
||||
X - logfile in workdir
|
@ -22,7 +22,8 @@ type Application struct {
|
||||
|
||||
logLock sync.Mutex
|
||||
logFile *os.File // file to write logs to, if set
|
||||
logList []dataext.Triple[string, string, func(string) string]
|
||||
logList []LogMessage
|
||||
logBroadcaster *dataext.PubSub[string, LogMessage]
|
||||
|
||||
config Config
|
||||
|
||||
@ -55,7 +56,8 @@ func NewApplication() *Application {
|
||||
app := &Application{
|
||||
masterLock: sync.Mutex{},
|
||||
logLock: sync.Mutex{},
|
||||
logList: make([]dataext.Triple[string, string, func(string) string], 0, 1024),
|
||||
logList: make([]LogMessage, 0, 1024),
|
||||
logBroadcaster: dataext.NewPubSub[string, LogMessage](128),
|
||||
uploadRunning: syncext.NewAtomicBool(false),
|
||||
trayReady: syncext.NewAtomicBool(false),
|
||||
syncLoopRunning: syncext.NewAtomicBool(false),
|
||||
|
@ -21,6 +21,7 @@ type Config struct {
|
||||
WorkDir string `json:"work_dir"`
|
||||
|
||||
ForceColors bool `json:"force_colors"`
|
||||
TerminalEmulator string `json:"terminal_emulator"`
|
||||
|
||||
Debounce int `json:"debounce"`
|
||||
}
|
||||
@ -44,12 +45,15 @@ func (app *Application) loadConfig() (Config, string) {
|
||||
var workDir string
|
||||
flag.StringVar(&workDir, "work_dir", "", "Temporary working directory")
|
||||
|
||||
var debounce int
|
||||
flag.IntVar(&debounce, "debounce", 0, "Debounce before sync (in seconds)")
|
||||
|
||||
var forceColors bool
|
||||
flag.BoolVar(&forceColors, "color", false, "Force color-output (default: auto-detect)")
|
||||
|
||||
var terminalEmulator string
|
||||
flag.StringVar(&terminalEmulator, "terminal_emulator", "", "Command to start terminal-emulator, e.g. 'konsole -e'")
|
||||
|
||||
var debounce int
|
||||
flag.IntVar(&debounce, "debounce", 0, "Debounce before sync (in seconds)")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if strings.HasPrefix(configPath, "~") {
|
||||
@ -63,6 +67,20 @@ func (app *Application) loadConfig() (Config, string) {
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) && configPath != "" {
|
||||
|
||||
te := ""
|
||||
if commandExists("konsole") {
|
||||
te = "konsole -e"
|
||||
} else if commandExists("gnome-terminal") {
|
||||
te = "gnome-terminal --"
|
||||
} else if commandExists("xterm") {
|
||||
te = "xterm -e"
|
||||
} else if commandExists("x-terminal-emulator") {
|
||||
te = "x-terminal-emulator -e"
|
||||
} else {
|
||||
app.LogError("Failed to determine terminal-emulator", nil)
|
||||
}
|
||||
|
||||
_ = os.WriteFile(configPath, langext.Must(json.MarshalIndent(Config{
|
||||
WebDAVURL: "https://your-nextcloud-domain.example/remote.php/dav/files/keepass.kdbx",
|
||||
WebDAVUser: "",
|
||||
@ -71,6 +89,7 @@ func (app *Application) loadConfig() (Config, string) {
|
||||
WorkDir: "/tmp/kpsync",
|
||||
Debounce: 3500,
|
||||
ForceColors: false,
|
||||
TerminalEmulator: te,
|
||||
}, "", " ")), 0644)
|
||||
}
|
||||
|
||||
@ -106,6 +125,9 @@ func (app *Application) loadConfig() (Config, string) {
|
||||
if forceColors {
|
||||
cfg.ForceColors = forceColors
|
||||
}
|
||||
if terminalEmulator != "" {
|
||||
cfg.TerminalEmulator = terminalEmulator
|
||||
}
|
||||
|
||||
return cfg, configPath
|
||||
}
|
||||
|
122
app/logger.go
122
app/logger.go
@ -1,14 +1,26 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/dataext"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/termext"
|
||||
)
|
||||
|
||||
type LogMessage struct {
|
||||
Line int
|
||||
Prefix string
|
||||
Message string
|
||||
ColorFunc func(string) string
|
||||
}
|
||||
|
||||
func colDefault(v string) string {
|
||||
return v
|
||||
}
|
||||
@ -58,23 +70,25 @@ func (app *Application) logInternal(pf string, msg string, c func(_ string) stri
|
||||
|
||||
for i, s := range strings.Split(msg, "\n") {
|
||||
if i == 0 {
|
||||
println(c(pf + s))
|
||||
app.logList = append(app.logList, dataext.NewTriple(strings.TrimSpace(pf), s, c))
|
||||
println(c(pf + " " + s))
|
||||
app.logList = append(app.logList, LogMessage{i, pf, s, c})
|
||||
if app.logFile != nil {
|
||||
_, err := app.logFile.WriteString(pf + s + "\n")
|
||||
_, err := app.logFile.WriteString(pf + " " + s + "\n")
|
||||
if err != nil {
|
||||
app.fallbackLog("[!] Failed to write logfile: " + err.Error())
|
||||
}
|
||||
}
|
||||
app.logBroadcaster.Publish("", LogMessage{i, pf, s, c})
|
||||
} else {
|
||||
println(c(langext.StrRepeat(" ", len(pf)) + s))
|
||||
app.logList = append(app.logList, dataext.NewTriple(strings.TrimSpace(pf), s, c))
|
||||
println(c(langext.StrRepeat(" ", len(pf)+1) + s))
|
||||
app.logList = append(app.logList, LogMessage{i, pf, s, c})
|
||||
if app.logFile != nil {
|
||||
_, err := app.logFile.WriteString(langext.StrRepeat(" ", len(pf)) + s + "\n")
|
||||
_, err := app.logFile.WriteString(langext.StrRepeat(" ", len(pf)+1) + s + "\n")
|
||||
if err != nil {
|
||||
app.fallbackLog("[!] Failed to write logfile: " + err.Error())
|
||||
}
|
||||
}
|
||||
app.logBroadcaster.Publish("", LogMessage{i, pf, s, c})
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,7 +117,9 @@ func (app *Application) LogLine() {
|
||||
}
|
||||
}
|
||||
|
||||
app.logList = append(app.logList, dataext.NewTriple("", "", func(v string) string { return v }))
|
||||
app.logList = append(app.logList, LogMessage{0, "", "", func(v string) string { return v }})
|
||||
|
||||
app.logBroadcaster.Publish("", LogMessage{0, "", "", func(v string) string { return v }})
|
||||
}
|
||||
|
||||
func (app *Application) fallbackLog(s string) {
|
||||
@ -119,7 +135,7 @@ func (app *Application) writeOutStartupLogs() {
|
||||
defer app.logLock.Unlock()
|
||||
|
||||
for _, v := range app.logList {
|
||||
_, err := app.logFile.WriteString(v.V1 + " " + v.V2 + "\n")
|
||||
_, err := app.logFile.WriteString(v.Prefix + " " + v.Message + "\n")
|
||||
if err != nil {
|
||||
app.fallbackLog("[!] Failed to write logfile: " + err.Error())
|
||||
}
|
||||
@ -130,3 +146,91 @@ func (app *Application) writeOutStartupLogs() {
|
||||
app.fallbackLog("[!] Failed to flush logfile: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Application) openLogFile() {
|
||||
err := exec.Command("xdg-open", path.Join(app.config.WorkDir, "kpsync.log")).Start()
|
||||
if err != nil {
|
||||
app.LogError("Failed to open log file with xdg-open", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Application) openLogFifo() {
|
||||
filePath := path.Join(app.config.WorkDir, fmt.Sprintf("kpsync.%s.fifo", langext.RandBase62(8)))
|
||||
|
||||
app.LogDebug(fmt.Sprintf("Creating fifo file at '%s'", filePath))
|
||||
err := syscall.Mkfifo(filePath, 0640)
|
||||
if err != nil {
|
||||
app.LogError("Failed to create fifo file", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer func() { _ = syscall.Unlink(filePath) }()
|
||||
|
||||
listernerStopSig := make(chan bool, 8)
|
||||
|
||||
go func() {
|
||||
|
||||
app.LogDebug(fmt.Sprintf("Opening fifo file '%s'", filePath))
|
||||
|
||||
f, err := os.OpenFile(filePath, os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
app.LogError("Failed to open fifo file", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
app.LogDebug(fmt.Sprintf("Initializing fifo with %d past entries", len(app.logList)))
|
||||
app.logLock.Lock()
|
||||
for _, item := range app.logList {
|
||||
if item.Line == 0 {
|
||||
_, _ = f.WriteString(item.ColorFunc(item.Prefix+" "+item.Message) + "\n")
|
||||
} else {
|
||||
_, _ = f.WriteString(item.ColorFunc(langext.StrRepeat(" ", len(item.Prefix)+1)+item.Message) + "\n")
|
||||
}
|
||||
}
|
||||
app.logLock.Unlock()
|
||||
|
||||
sub := app.logBroadcaster.SubscribeByCallback("", func(msg LogMessage) {
|
||||
if msg.Line == 0 {
|
||||
_, _ = f.WriteString(msg.ColorFunc(msg.Prefix+" "+msg.Message) + "\n")
|
||||
} else {
|
||||
_, _ = f.WriteString(msg.ColorFunc(langext.StrRepeat(" ", len(msg.Prefix)+1)+msg.Message) + "\n")
|
||||
}
|
||||
})
|
||||
defer sub.Unsubscribe()
|
||||
|
||||
app.LogDebug(fmt.Sprintf("Starting fifo log listener"))
|
||||
|
||||
<-listernerStopSig
|
||||
|
||||
app.LogDebug(fmt.Sprintf("Finished fifo log listener"))
|
||||
app.LogLine()
|
||||
|
||||
}()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
te := strings.Split(app.config.TerminalEmulator, " ")[0]
|
||||
tc := strings.Split(app.config.TerminalEmulator, " ")[1:]
|
||||
tc = append(tc, fmt.Sprintf("cat \"%s\"", filePath))
|
||||
//tc = append(tc, "bash")
|
||||
|
||||
proc := exec.Command(te, tc...)
|
||||
|
||||
app.LogDebug(fmt.Sprintf("Starting terminal-emulator '%s' [%v]", te, tc))
|
||||
err = proc.Start()
|
||||
if err != nil {
|
||||
app.LogError("Failed to start terminal emulator", err)
|
||||
return
|
||||
}
|
||||
|
||||
app.LogDebug("Terminal-emulator started - waiting for exit")
|
||||
app.LogLine()
|
||||
|
||||
_ = proc.Wait()
|
||||
|
||||
app.LogDebug("Terminal-emulator exited - stopping fifo pipe")
|
||||
|
||||
listernerStopSig <- true
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ func (app *Application) showSuccessNotification(msg string, body string) {
|
||||
|
||||
res, err := cmdext.
|
||||
Runner("notify-send").
|
||||
Arg("--urgency=critical").
|
||||
Arg("--urgency=normal").
|
||||
Arg("--app-name=kpsync").
|
||||
Arg("--print-id").
|
||||
Arg(msg).
|
||||
|
@ -63,6 +63,10 @@ func (app *Application) initSync() (InitSyncResponse, error) {
|
||||
app.LogLine()
|
||||
needsDownload = false
|
||||
|
||||
err = app.saveState(state.ETag, state.LastModified, state.Checksum, state.Size)
|
||||
if err != nil {
|
||||
app.LogError("Failed to save state", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -412,7 +416,7 @@ func (app *Application) runExplicitSync(force bool) {
|
||||
app.LogDebug(fmt.Sprintf("ETag (local) := %s", state.ETag))
|
||||
app.LogDebug(fmt.Sprintf("ETag (remote) := %s", remoteETag))
|
||||
|
||||
app.showErrorNotification("KeePassSync", "No sync necessary - file is up-to-date with remote")
|
||||
app.showSuccessNotification("KeePassSync", "No sync necessary - file is up-to-date with remote")
|
||||
return
|
||||
}
|
||||
|
||||
|
18
app/tray.go
18
app/tray.go
@ -21,7 +21,8 @@ func (app *Application) initTray() {
|
||||
|
||||
miSync := systray.AddMenuItem("Sync Now (checked)", "")
|
||||
miSyncForce := systray.AddMenuItem("Sync Now (forced)", "")
|
||||
miShowLog := systray.AddMenuItem("Show Log", "")
|
||||
miShowLogFifo := systray.AddMenuItem("Show Log (fifo)", "")
|
||||
miShowLogFile := systray.AddMenuItem("Show Log (file)", "")
|
||||
systray.AddMenuItem("", "")
|
||||
app.trayItemChecksum = systray.AddMenuItem("Checksum: {...}", "")
|
||||
app.trayItemETag = systray.AddMenuItem("ETag: {...}", "")
|
||||
@ -37,18 +38,27 @@ func (app *Application) initTray() {
|
||||
select {
|
||||
case <-miSync.ClickedCh:
|
||||
app.LogDebug("SysTray: [Sync Now (checked)] clicked")
|
||||
app.LogLine()
|
||||
go func() { app.runExplicitSync(false) }()
|
||||
case <-miSyncForce.ClickedCh:
|
||||
app.LogDebug("SysTray: [Sync Now (forced)] clicked")
|
||||
app.LogLine()
|
||||
go func() { app.runExplicitSync(true) }()
|
||||
case <-miShowLog.ClickedCh:
|
||||
app.LogDebug("SysTray: [Show Log] clicked")
|
||||
//TODO
|
||||
case <-miShowLogFifo.ClickedCh:
|
||||
app.LogDebug("SysTray: [Show Log Fifo] clicked")
|
||||
app.LogLine()
|
||||
go func() { app.openLogFifo() }()
|
||||
case <-miShowLogFile.ClickedCh:
|
||||
app.LogDebug("SysTray: [Show Log File] clicked")
|
||||
app.LogLine()
|
||||
go func() { app.openLogFile() }()
|
||||
case <-miQuit.ClickedCh:
|
||||
app.LogDebug("SysTray: [Quit] clicked")
|
||||
app.LogLine()
|
||||
app.sigManualStopChan <- true
|
||||
case <-sigBGStop:
|
||||
app.LogDebug("SysTray: Click-Listener goroutine stopped")
|
||||
app.LogLine()
|
||||
return
|
||||
|
||||
}
|
||||
|
@ -4,11 +4,13 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/cryptext"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/exerr"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/langext"
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/timeext"
|
||||
"github.com/shirou/gopsutil/v3/process"
|
||||
)
|
||||
@ -68,7 +70,7 @@ func (app *Application) saveState(eTag string, lastModified time.Time, checksum
|
||||
}
|
||||
|
||||
if app.trayItemChecksum != nil {
|
||||
app.trayItemChecksum.SetTitle(fmt.Sprintf("Checksum: %s", checksum))
|
||||
app.trayItemChecksum.SetTitle(fmt.Sprintf("Checksum: %s", langext.StrLimit(checksum, 16, "")))
|
||||
}
|
||||
if app.trayItemETag != nil {
|
||||
app.trayItemETag.SetTitle(fmt.Sprintf("ETag: %s", eTag))
|
||||
@ -109,3 +111,8 @@ func (app *Application) isKeepassRunning() bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func commandExists(cmd string) bool {
|
||||
_, err := exec.LookPath(cmd)
|
||||
return err == nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user