-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
235 lines (209 loc) · 5.75 KB
/
main.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"fmt"
"runtime"
// "git.wow.st/gmp/jni"
leprechaun "github.com/michaellormann/leprechaun/core"
_ "github.com/michaellormann/leprechaun/plugins" // Load analysis plugins
"log"
"os"
"path/filepath"
"strings"
"time"
ui "github.com/michaellormann/leprechaun/material"
"gioui.org/font/gofont"
"gioui.org/font/opentype"
"github.com/jrick/logrotate/rotator"
"gioui.org/app"
_ "gioui.org/app/permission/storage"
"gioui.org/text"
"gioui.org/widget/material"
)
// App ....
type App struct {
name string
logRotators map[string]*rotator.Rotator
logBackends map[string]*log.Logger
subsystems []string
config *leprechaun.Configuration
fontFile string
dir string
cwd string
test bool
font []text.FontFace
theme *material.Theme
win *ui.Window
isAndroid bool
}
func newApp(test bool) *App {
wd, _ := os.Getwd()
isAndroid := false
if runtime.GOOS == "android" {
isAndroid = true
}
return &App{name: "Leprechaun",
test: test,
logBackends: map[string]*log.Logger{},
logRotators: map[string]*rotator.Rotator{},
fontFile: "./assets/fonts/source_sans_pro_semibold.otf",
config: new(leprechaun.Configuration),
cwd: wd,
subsystems: []string{
"bot",
"startup",
// "ui"
},
isAndroid: isAndroid,
}
}
// TODO:: Update android support library (javac, etc) to use notify and other android extensions
func main() {
myApp := newApp(true)
d, err := app.DataDir()
if err != nil {
myApp.Errorln("could not find user data directory")
}
myApp.dir = d
// create different log backends for the bot,
// the startup (main.go) (and the ui)?
for _, sys := range myApp.subsystems {
myApp.InitLogBackends(sys)
}
// ensure they are closed when the app.Main function
// returns
defer myApp.CloseLogFiles()
// load user settings from file
myApp.LoadConfig()
theme := myApp.Theme()
myApp.win = ui.CreateWindow(theme, myApp.config)
myApp.win.InitBackends(myApp.logBackends)
go func() {
if err := myApp.win.Loop(); err != nil {
// TODO (michael): Send shutdown signal
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
// Load Fonts.
func (a *App) loadFont() (err error) {
source, err := os.Open(a.fontFile)
if err != nil {
log.Printf("Failed to load font: %v", err)
a.font = gofont.Collection()
} else {
stat, err := source.Stat()
if err != nil {
log.Println(err)
}
bytes := make([]byte, stat.Size())
source.Read(bytes)
fnt, err := opentype.Parse(bytes)
if err != nil {
log.Println(err)
}
a.font = append(a.font, text.FontFace{Font: text.Font{}, Face: fnt})
}
return nil
}
// Theme loads the custom font if any
func (a *App) Theme() *material.Theme {
err := a.loadFont()
if err != nil || len(a.font) == 0 {
a.font = gofont.Collection()
}
return material.NewTheme(a.font)
}
// InitLogBackends creates a unique log rotator object
// for the provided subsystem.
func (a *App) InitLogBackends(subsystem string) {
a.config.LogDir = filepath.Join(a.dir, "Leprechaun", "logs", "bot")
logFile := filepath.Join(a.dir, "Leprechaun", "logs", subsystem, "log.txt")
prefix := fmt.Sprintf("Leprechaun %s - ", strings.ToTitle(subsystem))
// initialize the log rotator
a.InitLogRotator(logFile, subsystem, 30) // backup logs for 30 days.
logBackend := &log.Logger{}
logBackend.SetFlags(log.LstdFlags)
logBackend.SetOutput(a.logRotators[subsystem])
logBackend.SetPrefix(prefix)
logFile, _ = filepath.Abs(logFile)
log.Printf(`%s log file==> "%s"`, subsystem, logFile)
a.logBackends[subsystem] = logBackend
return
}
// CloseLogFiles ensures that the loggers of all subsytems are
// closed before the program exits.
func (a *App) CloseLogFiles() {
var err error
defer func() {
if p := recover(); p != nil {
err = fmt.Errorf("internal error: %v", p)
log.Println(err)
}
}()
for _, r := range a.logRotators {
r.Close()
}
}
// InitLogRotator creates a log rotator for each subsystem.
func (a *App) InitLogRotator(logFile, subsytem string, maxRolls int) {
logDir, _ := filepath.Split(logFile)
if _, err := os.Stat(logDir); os.IsNotExist(err) {
_ = os.MkdirAll(logDir, 0755)
}
r, err := rotator.New(logFile, 512, false, maxRolls)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err)
os.Exit(1)
}
a.logRotators[subsytem] = r
}
// LoadConfig loads saved user settings from file. Default settings
// are used if no settings are found on file.
// If the *App.test = true, my custom API creds are used.
func (a *App) LoadConfig() {
a.config.SetAppDir(a.dir)
if a.test {
err := a.config.TestConfig(a.dir)
if err != nil {
a.Errorln("Could not load test configuration: ", err)
}
} else {
err := a.config.LoadConfig(a.dir)
if err != nil {
// settings have not been loaded. This might be the first run.
// use default settings instead.
log.Println("could not load saved settings. reverting to default settings.")
err = a.config.DefaultSettings(a.dir)
if err != nil {
a.Errorln("Leprechaun: could not initialize default configuration.", err)
}
}
}
a.config.ExportAPIVars(a.config.APIKeyID, a.config.APIKeySecret)
}
// Errorf writes a formatted error message to the startup log file
// and stderr.
func (a *App) Errorf(format string, args ...interface{}) {
if startupLog, ok := a.logBackends["startup"]; ok {
startupLog.Printf(format+"/n", args...)
}
log.SetOutput(os.Stderr)
log.Printf(format+"\n", args...)
go a.CloseLogFiles()
time.Sleep(5 * time.Second)
os.Exit(1)
}
// Errorln writes an error message to the startup log file
// and stderr.
func (a *App) Errorln(args ...interface{}) {
if startupLog, ok := a.logBackends["startup"]; ok {
startupLog.Println(args...)
}
log.SetOutput(os.Stderr)
log.Println(args...)
go a.CloseLogFiles()
time.Sleep(5 * time.Second)
os.Exit(1)
}