-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
174 lines (149 loc) · 3.93 KB
/
main.go
File metadata and controls
174 lines (149 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
_ "embed"
"flag"
"fmt"
"log"
"os"
"os/signal"
"path"
"strings"
"syscall"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"github.com/roffe/txlogger/pkg/cangw"
"github.com/roffe/txlogger/pkg/common"
"github.com/roffe/txlogger/pkg/debug"
"github.com/roffe/txlogger/pkg/ipc"
"github.com/roffe/txlogger/pkg/presets"
"github.com/roffe/txlogger/pkg/theme"
"github.com/roffe/txlogger/pkg/windows"
// _ "net/http/pprof"
)
var (
workDirectory string
allowMultipleInstances = false
)
func init() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.StringVar(&workDirectory, "d", "", "working directory")
flag.BoolVar(&allowMultipleInstances, "m", false, "allow multiple instances")
flag.Parse()
}
// Unfortunately Fyne installs its own signal handler that needs to be overridden to allow graceful shutdown on SIGINT/SIGTERM.
func signalHandler(mw *windows.MainWindow) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
//debug.Log("installed signal handler")
sig := make(chan os.Signal, 2)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
debug.Log("caught:" + s.String())
fyne.DoAndWait(mw.Close)
//fyne.CurrentApp().Driver().Quit()
}
func main() {
if os.Getenv("FP") == "1" {
runFileChild()
return
}
InitConsole()
// create txlogger dir in user home for debug log and such
if err := common.CreatetxloggerDir(); err != nil {
log.Printf("error creating txlogger dir in user home: %v", err)
}
// change working directory if requested
if workDirectory != "" {
debug.Log("changing working directory to " + workDirectory)
if err := os.Chdir(workDirectory); err != nil {
debug.Log(fmt.Sprintf("failed to change working directory to %s: %v", workDirectory, err))
}
}
//startpprof()
defer debug.Close()
defer debug.Log("txlogger exit")
// if another instance is running, just show its window and exit
if ipc.IsRunning() && !allowMultipleInstances {
return
}
// start cangateway if not already running
p, err := cangw.Start()
if p != nil {
defer killProcess(p)
}
if err != nil {
debug.Log("cangateway is not ready: " + err.Error())
}
// create app
tx := app.NewWithID("com.roffe.txlogger")
tx.Settings().SetTheme(&theme.TxTheme{})
// load presets
if err := presets.Load(tx); err != nil {
debug.Log("failed to load presets: " + err.Error())
}
// log version info
metadata := tx.Metadata()
debug.Log(fmt.Sprintf("starting txlogger v%s build %d", metadata.Version, metadata.Build))
// create main window
mw := windows.NewMainWindow(tx)
// install our own signal handler
fyne.CurrentApp().Lifecycle().SetOnStarted(func() {
go signalHandler(mw)
})
// start IPC server
sockServ, err := ipc.NewServer(ipc.CreateIPCRouter(mw))
if err != nil {
debug.Log("error: " + err.Error())
} else {
defer sockServ.Close()
}
// handle command line arguments
handleArgs(mw, tx)
// show main window
mw.ShowAndRun()
}
/*
func startpprof() {
go func() {
debug.Log(http.ListenAndServe("localhost:6060", nil))
}()
}
*/
func killProcess(p *os.Process) {
if p != nil {
p.Kill()
p.Wait()
}
}
func handleArgs(mw *windows.MainWindow, tx fyne.App) {
var loadedSymbols bool
if filename := flag.Arg(0); filename != "" {
switch strings.ToLower(path.Ext(filename)) {
case ".bin":
if err := mw.LoadSymbolsFromFile(filename); err != nil {
mw.Error(err)
} else {
loadedSymbols = true
}
case ".t5l", ".t7l", ".t8l", ".csv":
f, err := os.Open(filename)
if err != nil {
mw.Error(err)
}
defer f.Close()
sz := mw.Canvas().Size()
mw.LoadLogfile(filename, f, fyne.Position{X: sz.Width / 2, Y: sz.Height / 2})
}
}
if filename := tx.Preferences().String("lastBinFile"); filename != "" && !loadedSymbols {
if fileExists(filename) {
if err := mw.LoadSymbolsFromFile(filename); err != nil {
mw.Error(err)
}
}
}
}
func fileExists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}