-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
60 lines (50 loc) · 1.46 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
package main
import (
"fmt"
"github.com/patfair/frc-radio-api/radio"
"github.com/patfair/frc-radio-api/web"
"log"
"os"
)
const (
// Path of the current log file.
logFilePath = "/root/frc-radio-api.log"
// Path of the old log file, which is rotated when the current log file gets too big.
oldLogFilePath = "/root/frc-radio-api.log.old"
// Maximum size of the current log file in bytes.
logFileMaxSizeBytes = 3 * 1 << 19 // 1.5 MB
)
func main() {
logFile := setupLogging()
log.Println("Starting FRC Radio API...")
if logFile != nil {
defer logFile.Close()
}
radio := radio.NewRadio()
fmt.Println("created radio")
// Launch the web server in a separate thread.
webServer := web.NewWebServer(radio)
fmt.Println("created webserver")
go webServer.Run()
// Run the radio event loop in the main thread.
radio.Run()
}
// setupLogging sets up logging to a file, or to stdout if the file can't be opened.
func setupLogging() *os.File {
// Rotate the log file if the current one is too big.
if fileInfo, err := os.Stat(logFilePath); err == nil {
if fileInfo.Size() >= logFileMaxSizeBytes {
if err := os.Rename(logFilePath, oldLogFilePath); err != nil {
log.Printf("error rotating log file: %v", err)
}
}
}
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err == nil {
log.SetOutput(logFile)
return logFile
} else {
log.Printf("error opening log file; logging to stdout instead: %v", err)
return nil
}
}