From 8073768c6136e9b8b00dcefbc09e69033ad5b308 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Thu, 11 Apr 2019 11:13:36 +0200 Subject: ui: implement export log action Signed-off-by: Alexander Neumann --- ui/manage_tunnels.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++--- ui/ui.go | 9 +++++++- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/ui/manage_tunnels.go b/ui/manage_tunnels.go index 9e3711b6..fb4a493f 100644 --- a/ui/manage_tunnels.go +++ b/ui/manage_tunnels.go @@ -9,6 +9,7 @@ import ( "archive/zip" "fmt" "io/ioutil" + "os" "path/filepath" "strings" "time" @@ -16,6 +17,7 @@ import ( "github.com/lxn/walk" "github.com/lxn/win" "golang.zx2c4.com/wireguard/windows/conf" + "golang.zx2c4.com/wireguard/windows/ringlogger" "golang.zx2c4.com/wireguard/windows/service" "golang.zx2c4.com/wireguard/windows/ui/syntax" ) @@ -25,6 +27,7 @@ type ManageTunnelsWindow struct { icon *walk.Icon + logger *ringlogger.Ringlogger tunnelTracker *TunnelTracker tunnelsView *TunnelsView confView *ConfView @@ -32,11 +35,12 @@ type ManageTunnelsWindow struct { tunnelDeletedPublisher walk.StringEventPublisher } -func NewManageTunnelsWindow(icon *walk.Icon) (*ManageTunnelsWindow, error) { +func NewManageTunnelsWindow(icon *walk.Icon, logger *ringlogger.Ringlogger) (*ManageTunnelsWindow, error) { var err error mtw := &ManageTunnelsWindow{ - icon: icon, + icon: icon, + logger: logger, } mtw.MainWindow, err = walk.NewMainWindowWithName("WireGuard") if err != nil { @@ -99,7 +103,7 @@ func (mtw *ManageTunnelsWindow) setup() error { exportLogAction := walk.NewAction() exportLogAction.SetText("Export log to file...") - // TODO: Triggered().Attach() + exportLogAction.Triggered().Attach(mtw.onExportLog) exportTunnelAction := walk.NewAction() exportTunnelAction.SetText("Export tunnels to zip...") @@ -460,6 +464,27 @@ func (mtw *ManageTunnelsWindow) TunnelDeleted() *walk.StringEvent { return mtw.tunnelDeletedPublisher.Event() } +func (mtw *ManageTunnelsWindow) exportLog(filePath string, overwriteExisting bool) (fileExists bool, err error) { + var file *os.File + + if overwriteExisting { + if file, err = os.Create(filePath); err != nil { + return false, fmt.Errorf("exportLog: os.Create failed: %v", err) + } + } else { + if file, err = os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600); err != nil { + return os.IsExist(err), fmt.Errorf("exportLog: os.OpenFile failed: %v", err) + } + } + defer file.Close() + + if _, err := mtw.logger.WriteTo(file); err != nil { + return false, fmt.Errorf("exportLog: Ringlogger.WriteTo failed: %v", err) + } + + return false, nil +} + // Handlers func (mtw *ManageTunnelsWindow) onEditTunnel() { @@ -517,3 +542,35 @@ func (mtw *ManageTunnelsWindow) onImport() { mtw.importFiles(dlg.FilePaths) } + +func (mtw *ManageTunnelsWindow) onExportLog() { + dlg := walk.FileDialog{ + Filter: "Log Files (*.log)|*.log|Text Files (*.txt)|*.txt|All Files (*.*)|*.*", + Title: "Export log to file", + } + + if ok, _ := dlg.ShowSave(mtw); !ok { + return + } + + extensions := []string{".log", ".txt"} + if dlg.FilterIndex < 3 && !strings.HasSuffix(dlg.FilePath, extensions[dlg.FilterIndex-1]) { + dlg.FilePath = dlg.FilePath + extensions[dlg.FilterIndex-1] + } + + if fileExists, err := mtw.exportLog(dlg.FilePath, false); err != nil { + if fileExists { + if walk.DlgCmdNo == walk.MsgBox(mtw, "Export log", fmt.Sprintf(`File "%s" already exists. + +Do you want to overwrite it?`, dlg.FilePath), walk.MsgBoxYesNo|walk.MsgBoxDefButton2|walk.MsgBoxIconWarning) { + return + } + + _, err = mtw.exportLog(dlg.FilePath, true) + } + + if err != nil { + walk.MsgBox(mtw, "Export log failed", fmt.Sprintf(`An error occurred while exporting the log to file "%s": %v`, dlg.FilePath, err), walk.MsgBoxIconError) + } + } +} diff --git a/ui/ui.go b/ui/ui.go index 8ba8725d..c068795f 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -12,6 +12,7 @@ import ( "time" "github.com/lxn/walk" + "golang.zx2c4.com/wireguard/windows/ringlogger" "golang.zx2c4.com/wireguard/windows/service" ) @@ -33,6 +34,12 @@ func nag() { func RunUI() { runtime.LockOSThread() + logger, err := ringlogger.NewRingloggerFromInheritedMappingHandle(os.Args[5], "GUI") + if err != nil { + walk.MsgBox(nil, "Unable to initialize logging", fmt.Sprint(err), walk.MsgBoxIconError) + return + } + tunnelTracker := new(TunnelTracker) icon, err := walk.NewIconFromResourceId(1) @@ -41,7 +48,7 @@ func RunUI() { } defer icon.Dispose() - mtw, err := NewManageTunnelsWindow(icon) + mtw, err := NewManageTunnelsWindow(icon, logger) if err != nil { panic(err) } -- cgit v1.2.3-59-g8ed1b